0

I'm trying to write a plugin installer, akin to TGMPA, but just for its core functionality of actually installing the plugin and nothing more.

I've identified that Plugin_Upgrader is what I need and decided to emulate this.

$plugin = THEME_DIR . '/Inc/Plugins/my-shortcodes.zip';
$options = array(
        'package' => $plugin,
        'destination' => WP_PLUGIN_DIR,
        'clear_destination' => false,
        'clear_working' => true,
        'is_multi' => true,
        'hook_extra' => array(
            'plugin' => $plugin,
        ),
);

require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

class My_Upgrader extends Plugin_Upgrader
{
    public function run( $options )
    {
        $result = parent::run( $options );
        return $result;
    }
}

$upgrader = new My_Upgrader;
$upgrader->run($options);
$upgrader->install( $plugin );

Unfortunately, this throws the following error:

Uncaught Error: Call to undefined function request_filesystem_credentials()     
in C:\xampp\htdocs\wordpress\wp-admin\includes\class-wp-upgrader-skin.php:93     
Stack trace: #0 C:\xampp\htdocs\wordpress\wp-admin\includes\class-wp- 
upgrader.php(187): WP_Upgrader_Skin->request_filesystem_credentials(false, 
'C:\\xampp\\htdocs...', false) #1 C:\xampp\htdocs\wordpress\wp- 
admin\includes\class-wp-upgrader.php(693): WP_Upgrader->fs_connect(Array) #2 
C:\xampp\htdocs\wordpress\wp-content\themes\amaranth\header.php(53): 
WP_Upgrader->run(Array) #3 C:\xampp\htdocs\wordpress\wp- 
content\themes\amaranth\header.php(59): My_Upgrader->run(Array) #4 
C:\xampp\htdocs\wordpress\wp-includes\template.php(688): 
require_once('C:\\xampp\\htdocs...') #5 C:\xampp\htdocs\wordpress\wp- 
includes\template.php(647): load_template('C:\\xampp\\htdocs...', true) #6 
C:\xampp\htdocs\wordpress\wp-includes\general-template.php(41): 
locate_template(Array, true) #7 C:\xampp\htdocs\wordpress\wp- 
content\themes\amaranth\index.php(15): get_header() #8 C:\xampp\htdoc in 
C:\xampp\htdocs\wordpress\wp-admin\includes\class-wp-upgrader-skin.php on 
line 93

It has something to do with its...skin? Am I missing something?

A few notes:

  • I understand that the Filesystem module would default to FTP if it can't write to wp-content, as such, this error might pop up, but it's not the case. WP can write to wp-content.

Disclaimer: This is a very dumb installer, but I gotta start somewhere. Any suggestions are welcome.

1

I forgot to include the files necessary for these functions to work.

And so, here's your dirty-quick way to install plugins:

$plugin = THEME_DIR . '/Inc/Plugins/my-shortcodes.zip';
$options = array(
        'package' => $plugin,
        'destination' => WP_PLUGIN_DIR,
        'clear_destination' => false,
        'clear_working' => true,
        'is_multi' => true,
        'hook_extra' => array(
            'plugin' => $plugin,
        ),
);

require_once ABSPATH  . 'wp-admin/includes/misc.php';

if(!function_exists('request_filesystem_credentials')){
    require_once ABSPATH  . 'wp-admin/includes/file.php';
}

if( !class_exists('\Plugin_Upgrader')) {
    require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
}

class My_Upgrader extends Plugin_Upgrader
{
    public function run( $options )
    {
        $result = parent::run( $options );
        return $result;
    }
}

$upgrader = new My_Upgrader;
$upgrader->run($options);
$upgrader->install( $plugin );
1

The error message says that request_filesystem_credentials() is undefined. So make sure that function is defined by including the file before class-wp-upgrader.php.

include_once ABSPATH . '/wp-admin/includes/file.php' );
3
  • 1
    totally not admin_url as you need a path not a url Jun 1 '18 at 18:46
  • Aha! Close, I figured it out, kinda ashamed this didn't occur to me. See my answer.
    – coolpasta
    Jun 1 '18 at 19:28
  • @MarkKaplun oops Jun 1 '18 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.