最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览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. 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 = '.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...

发布评论

评论列表(0)

  1. 暂无评论