0

My employer has over a dozen WP sites, and we keep creating more. I decided to develop a plugin with all of the functions, etc. that we use across all the sites. We also use 3rd party plugins (such as Gravity Forms) on pretty much all of the sites. I would like to add a section on my settings page that detects if these 3rd party plugins are installed and if not, then include an "Install" button. I know how to detect the plugins, but I'm stuck at the install/activate part of the ordeal.

In my code below, I am trying to install the Fillable PDFs plugin from the settings page. The paid plugin zip file is on our main server. Other plugins I can get the zip file links directly from WordPress.org. I found the functions code online, but I'm getting a fatal error:

Starting ...

Check if new plugin is already installed - it's not installed. Installing.

There has been a critical error on your website...

On my settings page:

<table class="form-table">
    <tr valign="top">
        <th scope="row">Fillable PDFs</th>
        <td>
        <?php 
        $fpdf_plugin_slug = 'forgravity-fillablepdfs/fillablepdfs.php';
        $fpdf_zip_url = 'https://mywebsite.com/download-plugins/forgravity-fillablepdfs_2.2.4.zip';
        if ( !is_plugin_active( $fpdf_plugin_slug ) ) {
            echo '<span class="span_install_button"><form method="post">
                <input type="hidden" name="pluginSlug" value="'.$fpdf_plugin_slug.'">
                <input type="hidden" name="pluginZip" value="'.$fpdf_zip_url.'">
                <input type="submit" name="install_plugin" class="btn" value="Install Plugin" />
            </form></span>';
        } else {
            echo 'Installed';
        } ?>
        </td>
    </tr>
</table>

In my functions:

if ( isset($_POST["install_plugin"])) {
    add_action( 'init', 'eri_install_plugin');
}
function eri_install_plugin() {
  $plugin_slug = $_POST['pluginSlug'];
  $plugin_zip = $_POST['pluginZip'];  
    
  echo 'Starting ...<br><br>';
   
  echo 'Check if new plugin is already installed - ';
  if ( is_plugin_installed( $plugin_slug ) ) {
    $installed = true;
  } else {
    echo 'it\'s not installed. Installing.';
    $installed = install_plugin( $plugin_zip );
  }
   
  if ( !is_wp_error( $installed ) && $installed ) {
    echo 'Activating new plugin.';
    $activate = activate_plugin( $plugin_slug );
     
    if ( is_null($activate) ) {
      echo '<br>Done! Everything went smooth.';
    }
  } else {
    echo 'Could not install the new plugin.';
  }
}
function is_plugin_installed( $slug ) {
  if ( ! function_exists( 'get_plugins' ) ) {
    require_once ABSPATH . 'wp-admin/includes/plugin.php';
  }
  $all_plugins = get_plugins();
   
  if ( !empty( $all_plugins[$slug] ) ) {
    return true;
  } else {
    return false;
  }
}
function install_plugin( $plugin_zip ) {
  include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  wp_cache_flush();
   
  $upgrader = new Plugin_Upgrader();
  $installed = $upgrader->install( $plugin_zip );
 
  return $installed;
}

UPDATE: Here is my fatal error from debug:

Fatal error: Uncaught Error: Call to undefined function request_filesystem_credentials() in /home/cngfoccykyf1/public_html/wp-admin/includes/class-wp-upgrader-skin.php:136 Stack trace: #0 /home/cngfoccykyf1/public_html/wp-admin/includes/class-wp-upgrader.php(188): WP_Upgrader_Skin->request_filesystem_credentials(false, '/home/cngfoccyk...', false) #1 /home/cngfoccykyf1/public_html/wp-admin/includes/class-wp-upgrader.php(719): WP_Upgrader->fs_connect(Array) #2 /home/cngfoccykyf1/public_html/wp-admin/includes/class-plugin-upgrader.php(137): WP_Upgrader->run(Array) #3 /home/cngfoccykyf1/public_html/wp-content/plugins/eri-webtools-plugin/includes/theme/functions.php(294): Plugin_Upgrader->install('https://mywebsite....') #4 /home/cngfoccykyf1/public_html/wp-content/plugins/eri-webtools-plugin/includes/theme/functions.php(262): install_plugin('https://mywebsite....') #5 /home/cngfoccykyf1/public_html/wp-includes/class-wp-hook.php(287): eri_install_plugin('') #6 /home/cngfoccykyf1/public_html/wp-includes/class-wp-hook.php(311): WP_Ho in /home/cngfoccykyf1/public_html/wp-admin/includes/class-wp-upgrader-skin.php on line 136

UPDATE AGAIN: Changed my install_plugin function to:

function install_plugin( $plugin_zip ) {
    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';
    }
    wp_cache_flush();

    $upgrader = new Plugin_Upgrader();
    $installed = $upgrader->install( $plugin_zip );

    return $installed;
}

Now it's installing fine, but getting multiple warnings:

Warning: Cannot modify header information - headers already sent...

6
  • Edit your question to include the actual fatal error. You should see it in your debug log.
    – shanebp
    Nov 23 '20 at 21:03
  • @shanebp, just added it.
    – Michael
    Nov 23 '20 at 21:14
  • 1
    Did you try including the file that contains the missing function? developer.wordpress.org/reference/functions/…
    – shanebp
    Nov 23 '20 at 21:27
  • No, I'm in a little confused on what file you are referring to? I see the request_filesystem_credentials() function, but I'm unsure how to include it.
    – Michael
    Nov 23 '20 at 21:33
  • Okay, after searching around, I found out what I think you're referring to. Following the answer here: link, I was able to install the plugin, but it's showing multiple "Warning: Cannot modify header information." It's my understand that it's usually an issue with whitespace between ?> and <?php?
    – Michael
    Nov 23 '20 at 21:47

Your Answer

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

Browse other questions tagged or ask your own question.