I made a plugin to show the number of views from your plugin for pages and posts. I just don’t know how to let it edit the number. If you can help or add this to your plugin it would be great.
<?php
/**
* Plugin Name: Glen Custom View Count
* Description: Add a view count field to pages and posts.
* Version: 1.0
* Author: Glen Rowell
*/
// Function to get the post views count using WordPress Popular Posts
function custom_get_wpp_post_views($postID) {
return do_shortcode('[wpp_views_count post_id=' . $postID . ']');
}
// Function to add custom view count meta box to the right side of the post editor
function custom_view_count_metabox() {
add_meta_box(
'custom-view-count-metabox',
'Page/Post View Count',
'custom_view_count_metabox_callback',
'post',
'side',
'default'
);
}
add_action('add_meta_boxes_post', 'custom_view_count_metabox');
// Callback function to display the "WordPress Popular Posts" view count in the meta box
function custom_view_count_metabox_callback($post) {
$postID = $post->ID;
// Get the "WordPress Popular Posts" view count
$count_wpp = custom_get_wpp_post_views($postID);
// Remove commas and the word "views" from the count for the input field
$count_wpp_cleaned = str_replace([' ', ',', 'views'], '', $count_wpp);
// Output the view count field
?>
<label for="custom-view-count">Page/Post View Count:</label>
<input type="text" id="custom-view-count" name="custom_view_count" value="<?php echo esc_attr($count_wpp_cleaned); ?>">
<p><?php echo esc_html($count_wpp); ?></p>
<?php
}
// Save the custom view count when the post is saved
function save_custom_view_count($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (isset($_POST['custom_view_count'])) {
$view_count = absint($_POST['custom_view_count']);
update_post_meta($post_id, '_custom_view_count', $view_count);
}
}
add_action('save_post', 'save_custom_view_count');
?>