Custom Plugin: How to Include Install Buttons of other 3rd Party Plugins?

Question

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;
}
0
Mr Gandy 2 months 0 Answers 11 views 0

Leave an answer