Cached php? Updates are rendering only if logged as admin

I was asked to fix a wordpress based site and I don't get what's going on with it.

The (custom) theme has a page-home.php that is the Home page.

Whatever edits I make to this file, if I refresh the page logged in wp-admin (so, I'm an admin), the change is seen, but logged out (anonymous - as the site is intended to be used) it's still the old content.

I did the following:

  • downloaded all site locally, opened all *.php files, and searched for duplicate similar homepage sites, just in case a different one is served for anonymous browsing - no duplicate files
  • searched for keywords like is_admin, is_user_logged_in, didn't find such logic
  • went through this post to get what the order of execution is in WP. Helped a bit but didn't help me solve the problem
  • hardcoded things in header.php file -> result only displayed while logged in.

I don't know how else to find out why I keep getting old page.

I assume wordpress is rendering some pages, but why doesn't use the latest changes if anonymous? How to fix this?

Topic cache wp-admin Wordpress

Category Web


So I deleted cache folders, I removed plenty plugins, edited .htaccess files, etc and still didn't work.

I figured, if cache issues are lost after login, and since this site is designed to be used anonymously - well then log a fake user in.

  1. I created a user anonuser with subscriber role
  2. I added in functions.php this code to auto login (source):

    function auto_login() {
      $loginusername = 'anonuser';
      if (!is_user_logged_in()) { 
        //get user's ID
        $user = get_user_by('login', $loginusername);
        $user_id = $user->ID;
        wp_set_current_user($user_id, $loginusername);
        wp_set_auth_cookie($user_id);
        do_action('wp_login', $loginusername);
        show_admin_bar(false);
        exit;
      }
    }
    add_action('wp', 'auto_login');
    
  3. Lastly, just in case someone visits wp-admin manually, log them out and show the login page (source):

    add_action( 'init', 'my_custom_dashboard_access_handler');
    
    function my_custom_dashboard_access_handler() {
    
      // Check if the current page is an admin page
      // && and ensure that this is not an ajax call
      if ( is_admin() && !( defined( 'DOING_AJAX' ) && DOING_AJAX ) ){
    
        //Get all capabilities of the current user
        $user = get_userdata( get_current_user_id() );
        $caps = ( is_object( $user) ) ? array_keys($user->allcaps) : array();
    
        //All capabilities/roles listed here are not able to see the dashboard
        $block_access_to = array('subscriber', 'contributor', 'my-custom-role', 'my-custom-capability');
    
        if(array_intersect($block_access_to, $caps)) {
          wp_logout();
          wp_redirect( wp_login_url() );
          exit;
        }
      }
    }
    

If you have wp-cli installed : https://wp-cli.org/ You can try a wp cache type command from the root of your folder.

If you get Unknown it's a plugin issue or a mu-plugin issue I think.

If it's Default, there is no WP cache plugin (I'm not really sure about that but 90% sure).

After that you may grep for 'WP_CACHE' from the root of your project.

grep -Ri 'WP_CACHE' *

You'll be able to delete this constant and remove the cache folder. Things going to work. (I hope for you)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.