• Resolved jvagner

    (@jvagner)


    …any posts in the result output of a WPP short code? For instance, if I build a short code to return the 10 most popular posts from post type in the last week, could WPP also add a “Popular” tag to each of those posts? This would aid in leveraging auto content output feature of MailPoet that won’t work on pages with shortcodes.

    Thanks!

    • This topic was modified 6 months, 1 week ago by jvagner.
    • This topic was modified 6 months, 1 week ago by jvagner.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hey @jvagner, good day!

    No, WordPress Popular Posts doesn’t have the ability to assign a category or tag to posts automatically.

    While it might be possible, you could for example use one of the hooks provided by the plugin to programmatically add/remove a specific post/tag (eg. “Popular”) to popular posts, in terms of performance it might not be a great idea to do so due to the dynamic feature of your popular posts list – especially if your site has a lot of posts and/or is a high-traffic one since this could result in reduced site performance because of the constant database write operations.

    I had a quick look at MailPoet’s knowledge base and it seems you can use custom shortcodes with it. Maybe you could leverage that instead to have your newsletter template render your popular posts?

    Thread Starter jvagner

    (@jvagner)

    That was a nice suggestion, and I was able to get the following code working, but while it output the expected header, there were no posts below the header (per the limit=3). Not sure why though.. so I’ll leave the function I wrote here just in case anyone spots a problem with it :-).

    add_filter('mailpoet_newsletter_shortcode', 'mailpoet_custom_shortcode', 10, 6);
    
    function mailpoet_custom_shortcode($shortcode, $newsletter, $subscriber, $queue, $newsletter_body, $arguments) {
     // always return the shortcode if it doesn't match your own!
     if ($shortcode !== '[custom:table]') return $shortcode; 
     
     if (function_exists('wpp_get_mostpopular')) {
       $args = array(
           'header' => 'Popular Posts',
    	   'limit' => 3
       );
       ob_start();
       wpp_get_mostpopular($args);
       $output = ob_get_clean();
       return $output;
     }
     
     return '';
    }
    Plugin Author Hector Cabrera

    (@hcabrera)

    Hey @jvagner,

    So the reason why your custom MailPoet shortcode only renders the “Popular Posts” title is due to WPP loading posts via AJAX by default. MailPoet’s shortcode is probably -and for good reasons- stripping the inline script that WPP uses to load posts.

    To “temporarily” resolve this go to Settings > WordPress Popular Posts > Tools, then under Data set Load popular posts list via AJAX to Disabled and click on the Apply button to save changes. Your newsletter should load posts now 🙂

    Note though that the side effect of this is that if you’re using WPP on your site’s sidebar for example it will not load via AJAX which may or may not be important if you are (not) using a page caching plugin on your site. If you are using a page caching plugin (eg. W3 Total Cache) the your popular posts list will be cached by it and won’t update until the page cache expires.

    I’m thinking of adding a new parameter to the wpp_get_mostpopular() function to override the AJAX function so it can be disabled on a per instance basis regardless of what the global setting is set to. I’ll play around with this idea and see what happens.

    Thread Starter jvagner

    (@jvagner)

    Yes yes yes!

    This is what I needed. Thank you so much. I really appreciate your help and rapid feedback.

    I got to where I needed to be today… and your awesome plugin got me there.

    I can live with AJAX off for now, but your suggested enhancement would be very helpful as well.

    THANKS!

    Plugin Author Hector Cabrera

    (@hcabrera)

    Hey @jvagner!

    WordPress Popular Posts 6.4.0 is out and with it now we have the ability to turn off the AJAX feature for the shortcode 😀

    So:

    1. add the ajaxify=0 parameter to your shortcode
    2. go back to Settings > WordPress Popular Posts > Tools, then under Data set Load popular posts list via AJAX to Enabled and click on the Apply button to save change
    3. if you are using a page caching plugin (eg. W3 Total Cache), clear its page cache so these changes are applied to your site right away

    If you have any comments / questions please let me know.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.