reader count with options menu

This commit is contained in:
cupcakearmy 2019-06-01 00:17:57 +02:00
parent 04c9ab3b6e
commit cf0ecb26a0
3 changed files with 38 additions and 8 deletions

View File

@ -16,7 +16,7 @@ add_action( 'wp_enqueue_scripts', function () {
add_action( 'admin_init', function () { add_action( 'admin_init', function () {
add_settings_section( 'section', 'All Settings', null, 'theme-options' ); add_settings_section( 'section-link', 'Links', null, 'theme-options' );
global $links; global $links;
foreach ( $links as $link ) { foreach ( $links as $link ) {
@ -24,11 +24,19 @@ add_action( 'admin_init', function () {
$id = $link . '_url'; $id = $link . '_url';
add_settings_field( $id, ucfirst( $link ) . ' Url', function () use ( $id ) { ?> add_settings_field( $id, ucfirst( $link ) . ' Url', function () use ( $id ) { ?>
<input type="text" name="<?= $id ?>" id="<?= $id ?>" value="<?= get_option( $id ); ?>"/> <input type="text" name="<?= $id ?>" id="<?= $id ?>" value="<?= get_option( $id ); ?>"/>
<?php }, 'theme-options', 'section' ); <?php }, 'theme-options', 'section-link' );
register_setting( 'section', $id ); register_setting( 'section-link', $id );
} }
add_settings_section( 'section-views', 'View counter', null, 'theme-options' );
add_settings_field( 'view_min', 'Show view counter if views are above:', function () { ?>
<input type="text" name="view_min" id="view_min" value="<?= (int) get_option( 'view_min' ) ?>"/>
<?php }, 'theme-options', 'section-views' );
register_setting( 'section-views', 'view_min' );
} ); } );
function theme_settings_page() { ?> function theme_settings_page() { ?>
@ -36,7 +44,8 @@ function theme_settings_page() { ?>
<h1>Theme Panel</h1> <h1>Theme Panel</h1>
<form method="post" action="options.php"> <form method="post" action="options.php">
<?php <?php
settings_fields( 'section' ); settings_fields( 'section-link' );
settings_fields( 'section-views' );
do_settings_sections( 'theme-options' ); do_settings_sections( 'theme-options' );
submit_button(); submit_button();
?> ?>

View File

@ -21,3 +21,23 @@ function getDates() {
'different' => $created != $modified, 'different' => $created != $modified,
]; ];
} }
function getCurrentPageViews() {
if ( ! is_single() ) {
return null;
}
$total = (int) do_shortcode( '[wpstatistics stat=pagevisits time=total]' );
$min = (int) get_option( 'view_min' );
if ( $total < $min ) {
return null;
}
$approximated = round( $total / 1000, 1 );
?>
<div id="views">
<b>~<?= $approximated ?>k readers</b>
</div>
<?
}

View File

@ -38,8 +38,9 @@ $dates = getDates();
<?php } ?> <?php } ?>
</div> </div>
<div class="flex item grow"></div> <div class="flex item grow"></div>
<div> <div class="text-align-right">
<b>~<?= do_shortcode( '[rt_reading_time]' ); ?> min</b> <b>~<?= do_shortcode( '[rt_reading_time]' ); ?> min</b>
<?= getCurrentPageViews() ?>
</div> </div>
</div> </div>
<br><br> <br><br>