• Resolved Oliver

    (@oliverraduner)


    Hi there,
    As I use WP-Optimize to cache my pages, I found an issue with dynamic Post Views & Likes displayed using the Theme I am using.

    Hence I think this Plugin can solve to exclude those parts from being cached. But I do not really understand, how I need to set this up, as the documentation is lacking examples of exactly this point where it just says “Add to that element all the content that you want to exclude from the cache” ?

    I want to exclude the HTML part of my blog to not be cached anymore:

    <ul class="bwp-post-counters list-unstyled clearfix">
    	<li class="bwp-views-count">
    		<a href="#"><i class="fa fa-eye"></i>21</a>
    	</li>
    	<li class="bwp-likes-count">
    		<span class="jm-post-like"><a href="#" data-post_id="9773"><span class="like"><i class="fa fa-heart-o"></i></span><span class="count">0</span></a></span>
    	</li>
    </ul>

    Questions

    1. For the given case, what do I need to put in the “content” field of a new Content No Cache-snippet?
    2. Do I put the Content No Cache-snippet into the PHP code of the function where the given Views & Likes are rendered, or do I need to render the Shortcode into the HTML (and it will be replaced via JavaScript then from the Plugin)?

    Thanks for some further explanation on how to use the Plugin for this case – or if I might be wrong and it can’t help for this.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Jose

    (@giuse)

    Hi @oliverraduner

    the plugin does what is written in the description, not more than that.
    It works only with the content, not with code. If the code that you posted is written in a file, it will not work if you don’t write your custom code.

    If it’s possible for you, you should write your code

    <ul class="bwp-post-counters list-unstyled clearfix"> <li class="bwp-views-count"> <a href="#"><i class="fa fa-eye"></i>21</a> </li> <li class="bwp-likes-count"> <span class="jm-post-like"><a href="#" data-post_id="9773"><span class="like"><i class="fa fa-heart-o"></i></span><span class="count">0</span></a></span> </li> </ul>

    in a Content No Cache element, and then where you had that code you should replace it with:

    echo do_shortcode( '[content_no_cache id="xxx"]' );

    Where xxx is the ID of the Content No Cache custom post.

    Be careful about the double quotes. This forum my replace them with non-valid characters. If you copy/paste replace them with manually written double quotes.

    I hope it helps.

    Have a great day!

    Jose

    Thread Starter Oliver

    (@oliverraduner)

    Thanks for the quick reply @giuse , that’s helpful.

    To better understand how you populate the „Content“ area of a No Cache-snippet for dynamic contents, could you share what you put in the corresponding field that dynamically generates a random number from PHP in the example blog page from the Details?

    EXAMPLE

    You can see Content No Cache in action visiting the blog post Exclude specific content from being cached.
    You will see a number that is always different when you refresh the page. But the page is served by full page cache.
    In the example it’s just a number, but you can output whatever content you want.

    Thread Starter Oliver

    (@oliverraduner)

    @giuse I tried an approach with shortcodes within a Content No Cache-snippet (id=1234):

    <!-- Content No Code-Snippet contents -->
    [post_views]

    Whereas I have a function in my child-theme as follows:

    add_shortcode('post_views', 'get_views_shortcode');
    function get_views_shortcode( ) {
        global $post;
        $post_id = $post->ID ?? 0;
    
        // get views
        $views =  ( empty($post_id) ? 0 : get_views( $post_id ) );
    
        // return output
        return $views;
    
    }

    And finally, in the view rendering function:

    <li class="bwp-views-count"><i class="fa fa-eye"></i><?php echo do_shortcode( '[content_no_cache id="1234"]' ); ?></li>

    This «works» but I always get “0” – as the $post->ID cannot be resolved.

    But as you might grasp, I’d nee to be able to – dynamically – fetch the Post’s ID in the Content No Cache-snippet & pass it on to the function used for the shortcode.

    Is this somehow possible?? Example:

    <!-- Content No Code-Snippet contents -->
    [post_views postid={$post->ID}]

    Much appreciate your thoughts / feedback on this. Probably there is a workaround to fiddle directly with the Content No Cache’s code and extend it to pass along a (dynamic) ID retrieved from the HTML of the current Post? I have the feeling for what I want to do – per Post basis with 1 No Cache-snippet – I might have hit a dead end…

    • This reply was modified 11 months, 2 weeks ago by Oliver.
    Thread Starter Oliver

    (@oliverraduner)

    OMG I got it working! What a hack…

    Simply put: I rely on the Content No Cache’s AJAX-request referrer header, to find out what post path – and hence which Post-ID – the shortcode was called from.

    add_shortcode('post_views', 'get_views_shortcode');
    function get_views_shortcode(  ) {
    
    	// Get Post ID from AJAX-Referrer > Slug-to-Post resolver
    	$slug = basename( wp_get_referer() );
    	$path = '/' . $slug . '/';
    	$post_id = url_to_postid($path);
    
    	// Get Views (Post Meta)
    	$views = intval( ( empty($post_id) ? 0 : get_post_meta( $post_id, '_post_views_count', true ) ) );
    
    	// Return views
    	return ( !empty($views) ? $views : 0 );
    
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How can I exclude my Posts views & like counter using this plugin?’ is closed to new replies.