Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* visit us - https://wpgenie.org */
- add_action( 'wp_head', 'slug_log_post_view' );
- function slug_log_post_view() {
- //set the ID of the post you want to track here:
- $post_to_track = 42;
- //get the current post's ID & make sure it's a valid ID and it matches our ID
- $id = get_queried_object_id();
- if ( intval( $id ) > 0 && $post_to_track === $id ) {
- //check if current user is logged in and if so get the user ID
- if ( is_user_logged_in() ) {
- $user_id = get_current_user_id();
- $user_id = 'a user with the ID ' . $user_id;
- }
- else{
- $user_id = 'a user that was not logged in.';
- }
- //log the information
- error_log( "The post {$post_to_track} was accessed by {$user_id}" );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement