Problem with add_filter while developing a plugin
-
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)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Problem with add_filter while developing a plugin’ is closed to new replies.