• Resolved bluantinoo

    (@bluantinoo)


    Hello,
    I’m running into a caching problem with an ajax function.

    I have a localized script to wich I pass some dynamic data. This data changes as non-logged users perform actions (Data are user metas), but the data passed to the localized script are not updated.

    More specifically, the script is registered and localized by a callback php function that is called by ajax, something like this:

    
    add_action( 'wp_enqueue_scripts', 'my_callback_function' );
    function my_callback_function() {
    
            wp_register_script( 'my-script', get_stylesheet_directory_uri() . '/js/wp-myscript.js', array('jquery') );
    
            $user_data = get_my_user_data();
           
            wp_localize_script( 'my-script', 'js_data', $user_data );
    
            wp_enqueue_script( 'my-script' );
    
        }
    

    Then in my wp-myscript.js I just try a simple console.log(favorites_data);

    But data are update only if user is logged in, otherwise they will remain the same of the first page load.

    Any hint would be much appreciated.

    • This topic was modified 6 years, 9 months ago by bluantinoo.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Hristo Pandjarov

    (@hristo-sg)

    SiteGround Representative

    You can try purging the cache when an event that changes the site output occurs:

    if (function_exists(‘sg_cachepress_purge_cache’)) {
    sg_cachepress_purge_cache();
    }

    Thread Starter bluantinoo

    (@bluantinoo)

    Hi Hristo,
    thanks for the tip, but like this I will clear the entire cache, while I would need to clear just one page cache.
    Is therre no way to clear just the page users is on?

    Plugin Author Hristo Pandjarov

    (@hristo-sg)

    SiteGround Representative

    I am afraid that this is not possible, we clear the entire cache per installation once an update occurs. There are many reasons for that but mostly we can’t know for sure which page is modified 🙁

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Localized Script Data updates only for logged users’ is closed to new replies.