Running WP_Upgrader via Cron

I have written code that updates core when page runs. Everything works when I use the code via admin hook admin_init but doesn’t work (fatal error) when I add it to cron function or run it via any frontend hook init or template_redirect etc.

So, I tried to require the class-wp-upgrader.php file.

Read More
add_action('init', 'kit_wp_auto_update'); // doesn't work
// add_action('admin_init', 'kit_wp_auto_update'); // works

    function kit_do_update(){
        require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );

        fb('kit_do_update');    


        $core_transient = get_site_transient( 'update_core' );
        fb($core_transient);

        $updates = $core_transient->updates;
        $current = $updates[0];

        if( !empty( $core ) && $current->response == 'upgrade' ){

            $skin = new Kit_Upgrader_Skin();        
            $upgrader = new Core_Upgrader($skin);
            $result = $upgrader->upgrade($current);

            if( is_wp_error( $result ) ) {
                return $result;
            }else{
                return true;
            }

        }
    }

But it still gets some function undefined.

Fatal error: Call to undefined function request_filesystem_credentials() in /home4/sisir/public_html/_sites/wpkitten.com/site/wp-admin/includes/class-wp-upgrader.php on line 1113

Questions:

  1. request_filesystem_credentials() is defined inside the file so I don’t understand why its undefined.
  2. Another one of my concern is if I try to run the function with cron. That means a visitor (logged out) might trigger the upgrade. As the upgrading process involves filesystem processes (read/write). Will wordpress have enough permission to do the upgrade?

Related posts

1 comment

  1. You say you included class-wp-upgrader.php, but the Codex page for request_filesystem_credentials() indicates that the function is defined in /wp-admin/includes/file.php. That’s the file you’d need to require() before you can use it.

    Looking at the source of class-wp-upgrader.php, the request_filesystem_credentials() function is part of the WP_Upgrader_Skin class, not a standalone function. There’s no guarantee that WP_Upgrader_Skin::request_filesystem_credentials() does the same thing as request_filesystem_credentials(), either.

Comments are closed.