Support » Plugin: Prevent Browser Caching » Own code & versioning

  • Resolved Antipole


    This plugin looks like it is could be what I need for my issue. On my site some pages are very different depending on whether the user is logged in or logged out. Browser caching is causing confusion because a user may log in but then be shown the logged-out version of a page, complete with login panel!

    This plugin offers a solution: I can write code like:

    prevent_browser_caching( array( 
        'assets_version' => is_user_logged_in() ? ‘logged_in’ : ‘logged_out’ 
    ) );

    This should lead the browser to display the correct version of the page. Note that both versions of a page could be cached for some days and displayed as appropriate.

    However, the returned text completely replaces the version number, thus:

    With plugin not active: css?ver=3.1
    With plugin active without own function: css?ver=3.128536473

    Note that if the css is updated, this will force the new version of the page, even if the plugin has not updated the number it appends. In the above example, it might become css?ver=3.228536473

    However, with my code above, I get: css?ver=logged_in
    That is the returned string completely replaces the version number rather than being appended to it.

    I think it should be appended so I get: css?ver=3.1logged_in

    That way the displayed page will be updated if the css is updated, even if the logged in/out pages were available in the cache.


Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author kostyatereshchuk


    Hi @antipole,

    I tried your code and it works for me with results:
    – css?ver=logged_in
    – css?ver=logged_out

    But if you want to customize versioning more specifically I recommend removing prevent_browser_caching function, disable this plugin and use the code like this:

    function add_your_custom_version( $src ) {
        $src = add_query_arg( 'custom_version', ( is_user_logged_in() ? ‘logged_in’ : ‘logged_out’ ) , $src );
        return $src;
    add_filter( 'style_loader_src', 'add_your_custom_version', 10000 );
    add_filter( 'script_loader_src', 'add_your_custom_version', 10000 );

    Hope it will help.

    Thank you

    Thread Starter Antipole


    Thank you very much for taking the time to offer this solution. I have installed it and hope that will fix the issue.

    Thread Starter Antipole


    @kostyatereshchuk The extra parameter is being added to the <link>s that load the style sheets and script calls OK. But when loading actual pages or posts, it seems not, as the wrong version is being displayed.

    Perhaps I need to add the filter to some other place to get the logged in status added to the actual page/post requests? Any guidance much appreciated.

    Plugin Author kostyatereshchuk


    The filter should be in functions.php file of your current theme.

    But if your site uses CDN or if there is some type of the full-page caching or if loading of CSS/JS files is hardcoded by <link>/<script> tags – it may not work.

    Thread Starter Antipole


    I have the following in my child theme functions.php file:

    function ovni_distinguish_logged_in( $src ) {
        $src = add_query_arg( 'logged_in', ( is_user_logged_in() ? ‘yes’ : ‘no’ ) , $src );
        return $src;
    add_filter( 'style_loader_src', 'ovni_distinguish_logged_in', 10000 );
    add_filter( 'script_loader_src', 'ovni_distinguish_logged_in', 10000 );

    The parameter is being appended to all <link> and <script> elements, but not to the page itself. When the page has loaded, should I not see the parameter on the end of the URL as viewed in the browser’s address bar?

    PS Am not using any WP caching plugins – just browser caching.

    Plugin Author kostyatereshchuk


    Yes, this script is related only to <link> and <script> elements but not to the page URL. So, you will not see any changes in the browser’s address bar.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Own code & versioning’ is closed to new replies.