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.
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:
request_filesystem_credentials()
is defined inside the file so I don’t understand why its undefined.- 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?
You say you included
class-wp-upgrader.php
, but the Codex page forrequest_filesystem_credentials()
indicates that the function is defined in/wp-admin/includes/file.php
. That’s the file you’d need torequire()
before you can use it.Looking at the source of
class-wp-upgrader.php
, therequest_filesystem_credentials()
function is part of theWP_Upgrader_Skin
class, not a standalone function. There’s no guarantee thatWP_Upgrader_Skin::request_filesystem_credentials()
does the same thing asrequest_filesystem_credentials()
, either.