Вот фрагмент кода PHP, демонстрирующий один из способов программной установки плагина WordPress:
<?php
// Define the plugin file URL
$plugin_url = 'https://example.com/plugin-file.zip';
// Define the location where the plugin will be installed
$plugin_path = '/path/to/wp-content/plugins/';
// Generate a unique name for the plugin folder
$plugin_folder = 'plugin-' . md5( $plugin_url );
// Build the full path to the plugin folder
$plugin_full_path = $plugin_path . $plugin_folder;
// Download the plugin file
$response = wp_remote_get( $plugin_url );
if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) {
// Create the plugin folder
wp_mkdir_p( $plugin_full_path );
// Save the plugin file
$plugin_file = $plugin_full_path . '/plugin-file.zip';
file_put_contents( $plugin_file, wp_remote_retrieve_body( $response ) );
// Unzip the plugin file
$zip = new ZipArchive();
$zip->open( $plugin_file );
$zip->extractTo( $plugin_full_path );
$zip->close();
// Delete the plugin file
unlink( $plugin_file );
// Activate the plugin
activate_plugin( $plugin_folder . '/plugin-file.php' );
echo 'Plugin installed and activated successfully.';
} else {
echo 'Failed to download the plugin file.';
}
?>
Этот фрагмент кода загружает файл плагина по указанному URL-адресу, создает уникальную папку для плагина, сохраняет файл плагина, извлекает его содержимое и активирует плагин с помощью функции activate_plugin()р>
Вот еще несколько способов, которые можно использовать для установки плагина WordPress:
-
Использование установщика плагинов WordPress:
$plugin_slug = 'plugin-slug'; $result = wp_install_plugin( $plugin_slug ); -
Использование WP-CLI (интерфейс командной строки для WordPress):
wp plugin install plugin-slug --activate -
Использование API плагинов WordPress:
$slug = 'plugin-slug'; $plugin_info = plugins_api( 'plugin_information', array( 'slug' => $slug ) ); if ( is_wp_error( $plugin_info ) ) { echo 'Failed to fetch plugin information.'; } else { $result = install_plugin_install_status( $plugin_info ); if ( is_wp_error( $result ) ) { echo 'Failed to install the plugin.'; } else { echo 'Plugin installed successfully.'; } } -
Использование FTP:
Вы можете вручную загрузить файлы плагина в каталог/wp-content/plugins/с помощью FTP-клиента.