Support » Plugins » Problem with add_filter while developing a plugin

  • Resolved valireds

    (@valireds)


    Good evening and nice to meet you!

    I’m trying to develop my first simple plugin for WordPress. The idea is to count the views of single posts and store them in a post_meta using filters and actions.
    When I view the last post of my blog, the plugin increases the count and stores it correctly in the wp_postmeta table.
    When I view other posts, the plugin increases the count of the post I’m viewing and it stores it correctly in the table…but it also increases the views of the next post (on a time basis).

    Here’s my code:

    function update_views(){
      if(is_single()){
        global $post, $wpdb, $single;
    
            $views = get_post_meta(intval($post->ID),'views',true);
            if($views==""){
              $views = 1;
              add_post_meta(intval($post->ID),'views','1',true);
            }
            else{
              $views = intval($views)+1;
              update_post_meta(intval($post->ID),'views',"$views");
            }
    
      }
    
    }
    
    function view_views($text){
      if(is_single()){
        $id_post = get_the_ID();
        $views = get_post_meta($id_post,'views',true);
          if($views==""){
            $views = 1;
          }
          else{
            $views = intval($views);
          }
          $div_test = "<div id='views' style='height: 150px; width:150px; border-style: solid;border-width: 1px 1px 1px 1px;border-color: green; float:right; margin-left:5px;margin-bottom:5px;text-align:left;'>$visite views!</div>";  
    
          $text = $div_test.$text;
      }
    
      return $text;
    }
    
    add_filter('the_content','view_views');
    
    add_action('get_header','update_views',999999);

    Is there a sort of automatic pre-loading? How can I change the code?

    Thanks in advance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter valireds

    (@valireds)

    UPDATE
    I’ve tried using a flag variable in session in order to execute the update_views only once. But it didn’t resolve the problem.

    Can you please run my code on your WordPress installation? By doing this, I’ll figure if it’s a configuration or coding problem.

    (sorry for my poor English ^^”’ )

    Thread Starter valireds

    (@valireds)

    RESOLVED
    The problem was the default action:

    add_action(‘wp_head’, ‘adjacent_posts_rel_link’, 10, 0);

    in the file default-filters.php

    Inside the plugin code I’ve added:

    function remove_next_link(){
      remove_action('wp_head', 'adjacent_posts_rel_link');
    }
    
    add_action('init','remove_next_link',1);

    Now, why this strange behaviour?

    FYI:
    in addition to messing up obvious ways of generating page view counts, this is a significant performance hit (single.php loaded twice on nearly every post)

    Spent some time debugging.

    Tracked it to adjacent_posts_rel_link->get_permalink with permalinks set to ‘Month and name’. Inside get_permalink, the call to get_option(‘home’) appears to be the culprit.

    If permalinks are set to default, this does not happen.

    WP version 2.8.5

    Permalinks set to anything other than the default will trigger this action. I’m also seeing the same issue. Pre-loading an entire post just to get the permalink seems a little overkill when WordPress could get that from the wp_posts table using a query.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Problem with add_filter while developing a plugin’ is closed to new replies.