• Resolved reachingout

    (@reachingout)


    Hi,

    When you turn on Page Caching, you basically cache all pages and you have the option to exclude certain pages by excluding URL Strings.

    I want to do the opposite and exclude all pages but one.

    How can I cache only one page using Page Caching?

    I want to cache only this page: https://www.example.com/login

    • This topic was modified 1 year, 9 months ago by reachingout.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @reachingout,

    I hope you’re doing well.

    We have forwarded your request to the developers, to check if there’s a workaround for changing the default exclusion list behavior.

    Please keep in mind that the reply may take longer, as they work on complex tasks. We’ll let you know when there’s an update.

    Best Regards,
    Dmytro

    Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @reachingout,

    Please use the following PHP code – paste in a text editor, and save as a PHP file, for example “hummingbird-cache-inclusions.php“:

    <?php
    /**
     * Plugin Name: [Hummingbird Pro] Page cache only on specific URLs
     * Description: Reverses the behavior of the "Exclusions" field, allowing caching only on specific URLs
     * Author: Anderson Salas @ WPMUDEV
     * Task: SLS-5039
     * Author URI: https://premium.wpmudev.org
     * License: GPLv2 or later
     */
    
    add_action( 'plugins_loaded', function() {
    	if ( ! defined( 'WPHB_VERSION' ) ) {
    		return; // Hummingbird is not installed/enabled.
    	}
    
    	if ( ! function_exists( 'wpmudev_hb_should_cache_url' ) ) {
    		function wpmudev_hb_should_cache_url( $uri ) {
    			global $wphb_cache_config;
    
    			if ( null === $wphb_cache_config || empty( $wphb_cache_config->exclude_url ) ) {
    				return false;
    			}
    
    			$exclude_urls = $wphb_cache_config->exclude_url;
    			$uri_pattern  = array_filter( $exclude_urls ); // Remove empty values.
    			
    			if ( empty( $uri_pattern ) ) {
    				return false;
    			}
    	
    			$uri_pattern   = implode( '|', $exclude_urls );
    			$invert        = false !== strpos( $uri_pattern, '!' );
    			$extra_pattern = null;
    
    			if ( $invert ) {
    				$uri_pattern = str_ireplace( '!', '', $uri_pattern );
    
    				// We still need to exclude some URLs (wp-login.php, xmlrpc.php, etc.)
    				$extra_pattern = 'wp-.*\.php|index\.php|xmlrpc\.php|sitemap[^\/.]*\.xml';
    			}
    			
    			if ( preg_match( "/$uri_pattern/i", $uri ) ) {
    				if ( null !== $extra_pattern && preg_match( "/$extra_pattern/i", $uri ) ) {
    					return false; 
    				}
    				return true;
    			}
    	
    			// Now do the same, but test the URI as part of the full URL.
    			$http_host = isset( $_SERVER['HTTP_HOST'] ) ? htmlentities( stripslashes( $_SERVER['HTTP_HOST'] ) ) : '';
    			$http_port = isset( $_SERVER['SERVER_PORT'] ) && 443 === (int) $_SERVER['SERVER_PORT'] ? 'https://' : 'https://';
    			
    			if ( preg_match( "/$uri_pattern/i", $http_port . $http_host . $uri ) ) {
    				if ( null !== $extra_pattern && preg_match( "/$extra_pattern/i", $http_port . $http_host . $uri ) ) {
    					return false; 
    				}
    				return true;
    			}
    	
    			return false;
    		}
    	}
    
    	add_filter( 'wphb_should_cache_request_pre', function( $cache ) {
    		if ( is_admin() || wp_doing_ajax() || wp_doing_cron() || ! is_bool( $cache )) {
    			return $cache; 
    		}
    
    		$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? stripslashes( $_SERVER['REQUEST_URI'] ) : '';
    		
    		if ( ! wpmudev_hb_should_cache_url( $request_uri ) ) {
    			$cache = false;
    		}
    
    		return $cache;
    	});
    
    }, 10 );

    Upload it to /wp-content/mu-plugins/ directory on the server, so that it runs as a must use plugin.

    It should make Hummingbird to not cache anything, except for the pages specified in the URL Strings exclusion list. You’ll need to put the ! symbol before each page/post slug, for example:

    !login
    !page1
    !page2

    Screenshot:
    https://prnt.sc/viX9W71_IGl6

    Best Regards,
    Dmytro

    Thread Starter reachingout

    (@reachingout)

    The above soultion seems to cache all pages on mobile devices.

    How does Page Cache work? Should it work immediately after activating or is there some lay time before it works?

    Thread Starter reachingout

    (@reachingout)

    But…I guess it is easier if I just specify all the pages that I want to exclude…they are not so many.

    Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @reachingout,

    The provided code should only cache the pages mentioned in the code.

    The above soultion seems to cache all pages on mobile devices.

    Please make sure you have cleared the exiting cache and re-check if you still notice this issue.

    How does Page Cache work? Should it work immediately after activating or is there some lay time before it works?

    Once a page is accessed after enabling the page cahce, it should be cached immediately to ensure faster subsequent page loads.

    I guess it is easier if I just specify all the pages that I want to exclude

    Is that you need to exclude the pages from catching, if so please use the inbuilt plugin feature that helps to exclude the page caching.

    Please feel free to get back to us if you need any further clarification.

    Kind Regards,
    Nebu John

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Only cache one page using Page Caching.’ is closed to new replies.