WordPress统计文章浏览量:remove_action作用及代码相关疑问
Hey there! Let’s break down your questions one by one, since you’re working on tracking WordPress post views without a plugin.
1. The Mystery Code Line: What It Does & What Happens If You Remove It
You didn’t share the exact line of code, but based on common tutorials for this use case, the most frequent line folks ask about falls into one of two categories:
Scenario 1: The Conditional Check Line
This is typically a line like:
if ( is_single() && !is_admin() && !wp_is_post_autosave( $post_id ) && !wp_is_post_revision( $post_id ) ) { // Logic to update post view count }
- What it does: It acts as a guard clause to ensure the view count only increments when:
- The user is on a single post page (
is_single()), not an archive, homepage, or other non-post page - The viewer isn’t an admin (
!is_admin()), so your own edits, previews, or dashboard visits don’t skew stats - The post isn’t an autosave or revision (
!wp_is_post_autosave()/!wp_is_post_revision()), so draft saves don’t add fake views
- The user is on a single post page (
- If you delete it: Every time WordPress loads the post (even in the admin dashboard, during autosaves, or on archive pages), the view count will jump—completely polluting your stats with invalid, non-user views.
Scenario 2: The remove_action Line for Prefetch Links
This is usually:
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
We’ll dive deeper into remove_action later, but in short: this line stops WordPress from adding preload tags for previous/next posts in your site header. If you remove this line, browsers might prefetch those adjacent posts in the background, triggering false view counts (more on that next).
2. What Does "Prefetching Adding Extra Views" Mean?
Prefetching is a browser optimization where, when you visit a page, the browser quietly loads linked content in the background to make future navigation faster.
By default, WordPress adds <link rel="next/prev"> tags for adjacent posts in the site’s <head>. Browsers often use these tags to preload those next/prev post pages automatically. Even though the user never actually clicked on those posts, your view tracking code runs when the browser preloads the page—so those background loads get counted as real views. That’s "prefetching adding extra views": inflated stats from browser preloads instead of actual user visits.
3. What Does remove_action Mean in WordPress View Tracking?
In WordPress, actions are hooks that let you run code at specific points during page load (like when the header is generated via the wp_head hook).
remove_action( $hook_name, $function_to_remove, $priority, $accepted_args ) is a core WordPress function that tells the system to stop executing a specific function that was hooked to an action.
In view tracking, it’s almost always used to fix the prefetching problem:
- The default WordPress function
adjacent_posts_rel_link_wp_head()adds those next/prev preload tags to thewp_headhook. - Using
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0)unhooks that function, so the prefetch tags aren’t added. This stops browsers from preloading adjacent posts and inflating your view counts.
You might also use remove_action if you wanted to disable a conflicting view-tracking function from a plugin or theme.
内容的提问来源于stack exchange,提问作者Mohammad Salehi

