| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| function track_post_views($post_id) {
|
| if (!is_single() || (is_user_logged_in() && current_user_can('manage_options'))) {
|
| return;
|
| }
|
|
|
| $key = 'post_views_counting';
|
| $current_views = get_post_meta($post_id, $key, true);
|
| $current_views = $current_views ? $current_views : 0;
|
|
|
|
|
| $cookie_name = 'post_viewed_' . $post_id;
|
| if (!isset($_COOKIE[$cookie_name])) {
|
| setcookie($cookie_name, 'true', time() + 3600, '/');
|
| update_post_meta($post_id, $key, $current_views + 1);
|
| }
|
| }
|
|
|
|
|
| function track_views_on_single_post() {
|
| if (is_single()) {
|
| global $post;
|
| track_post_views($post->ID);
|
| }
|
| }
|
| add_action('wp', 'track_views_on_single_post');
|
|
|
|
|
| function get_post_views($post_id) {
|
| $key = 'post_views_counting';
|
| $views = get_post_meta($post_id, $key, true);
|
| return $views ? $views : 0;
|
| }
|
|
|
|
|
| function post_views_shortcode($atts) {
|
| global $post;
|
| return get_post_views($post->ID);
|
| }
|
| add_shortcode('post_views', 'post_views_shortcode');
|
|
|
|
|
| function add_views_column($columns) {
|
| $columns['post_views'] = 'Views';
|
| return $columns;
|
| }
|
| add_filter('manage_posts_columns', 'add_views_column');
|
|
|
| function display_views_column($column_name, $post_id) {
|
| if ($column_name === 'post_views') {
|
| echo get_post_views($post_id);
|
| }
|
| }
|
| add_action('manage_posts_custom_column', 'display_views_column', 10, 2);
|
|
|
|
|
| add_filter('manage_edit-post_sortable_columns', function($columns) {
|
| $columns['post_views'] = 'post_views_counting';
|
| return $columns;
|
| });
|
|
|
|
|
| add_action('pre_get_posts', function($query) {
|
| if (!is_admin() || !$query->is_main_query()) return;
|
|
|
| if ($query->get('orderby') === 'post_views_counting') {
|
| $query->set('meta_key', 'post_views_counting');
|
| $query->set('orderby', 'meta_value_num');
|
| }
|
| });
|
|
|
| add_action('admin_bar_menu', 'mb_add_post_views_to_admin_bar', 100);
|
| function mb_add_post_views_to_admin_bar($admin_bar) {
|
| if (!is_admin() && is_singular()) {
|
| global $post;
|
|
|
| if (!$post instanceof WP_Post) return;
|
|
|
|
|
| if (!current_user_can('edit_post', $post->ID)) return;
|
|
|
| $views = get_post_meta($post->ID, 'post_views_counting', true);
|
| $views = $views ? $views : 0;
|
|
|
| $admin_bar->add_node([
|
| 'id' => 'post_views_count',
|
| 'title' => '👁 Views: ' . number_format_i18n($views),
|
| 'meta' => ['title' => 'This post has been viewed ' . number_format_i18n($views) . ' times']
|
| ]);
|
| }
|
| }
|
|
|
|
|
| function mb_add_post_views_sorting_for_elementor() {
|
|
|
| if ( ! did_action('elementor/loaded') || ! function_exists('elementor_pro_load_plugin') ) {
|
| return;
|
| }
|
|
|
|
|
| add_action('elementor/query/views_sort', function($query) {
|
| $query->set('meta_key', 'post_views_counting');
|
| $query->set('orderby', 'meta_value_num');
|
| });
|
| }
|
| add_action('init', 'mb_add_post_views_sorting_for_elementor');
|
|
|
|
|
| function track_post_views($post_id) {
|
| if (!is_single() || (is_user_logged_in() && current_user_can('manage_options'))) {
|
| return;
|
| }
|
|
|
| $key = 'post_views_counting';
|
| $current_views = get_post_meta($post_id, $key, true);
|
| $current_views = $current_views ? $current_views : 0;
|
|
|
|
|
| $cookie_name = 'post_viewed_' . $post_id;
|
| if (!isset($_COOKIE[$cookie_name])) {
|
| setcookie($cookie_name, 'true', time() + 3600, '/');
|
| update_post_meta($post_id, $key, $current_views + 1);
|
| }
|
| }
|
|
|
|
|
| function track_views_on_single_post() {
|
| if (is_single()) {
|
| global $post;
|
| track_post_views($post->ID);
|
| }
|
| }
|
| add_action('wp', 'track_views_on_single_post');
|
|
|
|
|
| function get_post_views($post_id) {
|
| $key = 'post_views_counting';
|
| $views = get_post_meta($post_id, $key, true);
|
| return $views ? $views : 0;
|
| }
|
|
|
|
|
| function post_views_shortcode($atts) {
|
| global $post;
|
| return get_post_views($post->ID);
|
| }
|
| add_shortcode('post_views', 'post_views_shortcode');
|
|
|
|
|
| function add_views_column($columns) {
|
| $columns['post_views'] = 'Views';
|
| return $columns;
|
| }
|
| add_filter('manage_posts_columns', 'add_views_column');
|
|
|
| function display_views_column($column_name, $post_id) {
|
| if ($column_name === 'post_views') {
|
| echo get_post_views($post_id);
|
| }
|
| }
|
| add_action('manage_posts_custom_column', 'display_views_column', 10, 2);
|
|
|
|
|
| add_filter('manage_edit-post_sortable_columns', function($columns) {
|
| $columns['post_views'] = 'post_views_counting';
|
| return $columns;
|
| });
|
|
|
|
|
| add_action('pre_get_posts', function($query) {
|
| if (!is_admin() || !$query->is_main_query()) return;
|
|
|
| if ($query->get('orderby') === 'post_views_counting') {
|
| $query->set('meta_key', 'post_views_counting');
|
| $query->set('orderby', 'meta_value_num');
|
| }
|
| });
|
|
|
| add_action('admin_bar_menu', 'mb_add_post_views_to_admin_bar', 100);
|
| function mb_add_post_views_to_admin_bar($admin_bar) {
|
| if (!is_admin() && is_singular()) {
|
| global $post;
|
|
|
| if (!$post instanceof WP_Post) return;
|
|
|
|
|
| if (!current_user_can('edit_post', $post->ID)) return;
|
|
|
| $views = get_post_meta($post->ID, 'post_views_counting', true);
|
| $views = $views ? $views : 0;
|
|
|
| $admin_bar->add_node([
|
| 'id' => 'post_views_count',
|
| 'title' => '👁 Views: ' . number_format_i18n($views),
|
| 'meta' => ['title' => 'This post has been viewed ' . number_format_i18n($views) . ' times']
|
| ]);
|
| }
|
| }
|
|
|
|
|
| |
| |
Comments