Support » Plugin: Super Page Cache for Cloudflare » Customize what pages are related

  • Resolved andrewceharris

    (@andrewceharris)


    It would be awesome if we could have a filter hook for themes/plugins so we could tap into the process that determines which URLs are purged when a post/page is updated/created.

    We have a couple archive pages, which are combinations of different categories, etc as a new page, so they don’t get picked up by the default related cache busting. They are mostly static until a new post is added, but we either have to disable caching completely for the page resulting in slow page load speeds or manually cache bust every-time.

    Not sure if this is possible at the moment, but couldn’t see anything in the repository. Just something to hook in to the process of get_post_related_links for that page, would be great.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor iSaumya

    (@isaumya)

    Are you using the proper WordPress query loop to show details on those pages?

    Thread Starter andrewceharris

    (@andrewceharris)

    Hi Saumya,

    The pages use a custom template.php file in the theme.

    i.e. page-learn.php for the page /learn

    and inside they create a custom query using WP_QUERY to grab the relevant articles.

    
    <?php
    $learn_query = new WP_Query( $args );
    	
    if ( $learn_query->have_posts() ) :
      while ( $learn_query->have_posts() ) :
        $learn_query->the_post();
    ?>
         <!-- markup is here using query loop -->
    <?php 
      endwhile; 
    else :
    ?>
       Sorry we couldn't find any results for this query...
    <?php endif; ?>
    Plugin Contributor iSaumya

    (@isaumya)

    Hi,
    I totally get what you are saying but unfortunately there is no hook as of now to add more URLs in the related list.

    Moreover while looking at the get_post_related_links() inside /libs/cache_controller.class.php on line no. 1019 I don’t see a way to automatically figure out out the list of links where the post has been used in the loop.

    Maybe in a future version of WP will introduce some sort of function to do that. But I don’t see an automated way of doing it now.

    Thread Starter andrewceharris

    (@andrewceharris)

    Thanks for the quick reply!

    Just some way to filter that initial $listofurls array would be great. Then we could alter that list.

    i.e. in functions.php the logic could be figured out by the theme

    
    add_filter('wp_cloudflare_related_links', function( $listofurls, $postId ) { 
      // always bust
      $listofurls[] = "/learn";
    
      // bust only if postId includes, or even get post and bust based on content
      if ($postId == 2) {
        $listofurls[] = "/another-url";
      }
      return $listofurls;
    });
    

    then in the get_post_related_links() using apply_filters to get the added URLs.

    function get_post_related_links($postId) {
    
      $this->objects = $this->main_instance->get_objects();
    
      $listofurls = apply_filters(
        'wp_cloudflare_related_links',
        array(),
        $postId
      );
    
    

    One other thing i noticed is that the home page is added by default as one of the posts that gets busted, which in our case isn’t an archive. Is there a way to disable that url being added by default?

    line 1109 of /libs/cache_controller.class.php

    
    // Home Page and (if used) posts page
    array_push($listofurls, home_url('/'));
    $pageLink = get_permalink(get_option('page_for_posts'));
    if (is_string($pageLink) && !empty($pageLink) && get_option('show_on_front') == 'page') {
       array_push($listofurls, $pageLink);
    }
    Plugin Contributor iSaumya

    (@isaumya)

    Hi @andrewceharris,
    Can you try out this build and let me know how it works?

    In this build, I have added a filter swcfpc_post_related_url_init to set the list of related URLs that you can set at the time of initialization of the variable that holds all the related URLs that are supposed to be purged.

    The callback function basically has 2 arguments, $listofurls (array) and $postId. You can omit the $postId if not needed and the callback function is supposed to return an array holding the full URLs (e.g. https://example.com/some-example/) of the items you want to add in the related URL list.

    So, you can do something like this in your functions.php:

    <?php
    add_filter( 'swcfpc_post_related_url_init', function( $listofurls, $postId ) {
    	// Some items that will always be added to the related listofurls
    	$listofurls = [ get_permalink( 20 ), get_home_url( null, '/some-path' ), get_permalink( get_page_by_path( 'learn' ) ) ];
    	
    	// Now add related page based on condition
    	if( $postId == 2 ) {
    		array_push( $listofurls, get_permalink( get_page_by_path( 'another-url' ) ) );
    	}
    
    	return $listofurls;
    } );

    Finally, I have also added a PHP constant in case your home page does not include any latest post and need not be get purged when a post is updated. The constant name is SWCFPC_HOME_PAGE_SHOWS_POSTS. The default value of this constant is true (bool).

    If you want to overwrite it, you can add the following in your wp-config.php:

    define( 'SWCFPC_HOME_PAGE_SHOWS_POSTS', false );

    Run the tests and let me know if the build works. Only after your confirmation, I can think about pushing it to the main plugin.

    Thread Starter andrewceharris

    (@andrewceharris)

    That works!

    Thank you,

    just to warn you though, if someone doesn’t use the filter it’s returning an error when you save/create a post. The error relates to trying to array push to a string.

    i updated this line

    
    $listofurls = apply_filters( 'swcfpc_post_related_url_init', '__return_empty_array', $postId );
    

    to this

    
    $listofurls = apply_filters( 'swcfpc_post_related_url_init', __return_empty_array(), $postId );
    

    and it worked again.

    Also little note to anyone else, but in order to use the second param $pageId, you will need to make sure you put that in the add_filter call, i.e. add_filter('swcfpc_post_related_url_init', 'callback', 10, 2);

    The last 2 makes it so the filter callback can receive 2 arguments.

    Plugin Contributor iSaumya

    (@isaumya)

    Thanks a ton, @andrewceharris for your test and feedback. I’ve made the changes in the code and the future version of the code will have this feature in it. Thanks again for testing it and suggesting it.

    Also yes, anytime you have more than 1 argument you must need to provide the number of arguments at the end of the function call as you mentioned above.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Customize what pages are related’ is closed to new replies.