activate_plugin

函式
activate_plugin ( $plugin, $redirect = '', $network_wide = false, $silent = false )
引數
  • (string) $plugin Path to the plugin file relative to the plugins directory.
    Required:
  • (string) $redirect Optional. URL to redirect to.
    Required:
    Default: (empty)
  • (bool) $network_wide Optional. Whether to enable the plugin for all sites in the network or just the current site. Multisite only. Default false.
    Required:
    Default: false
  • (bool) $silent Optional. Whether to prevent calling activation hooks. Default false.
    Required:
    Default: false
返回值
  • (null|WP_Error) Null on success, WP_Error on invalid file.
定義位置
相關方法
activate_pluginsdeactivate_pluginsactivate_sitewide_plugindeactivate_sitewide_pluginvalidate_plugin
引入
2.5.0
棄用
-

activate_plugin: 這個函式用於啟用WordPress中的一個外掛。它執行一些檢查,以確保該外掛可以被啟用,然後在WordPress的選項表中新增一個條目,表明該外掛已經啟用。

在一個”sandbox”中嘗試啟用外掛,成功後重定向。

已經被啟用的外掛不會再嘗試被啟用。

它的工作方式是,在嘗試包含外掛檔案之前,將重定向設定為錯誤。如果外掛失敗了,那麼重定向將不會被成功資訊所覆蓋。同時,選項不會被更新,啟用鉤也不會在外掛出錯時被呼叫。

應該注意的是,下面的程式碼在任何情況下都不會真正防止檔案內的錯誤。這段程式碼不應該被用在其他地方來複制”sandbox”,它使用重定向來工作。{@source 1}

如果發現任何錯誤或輸出文字,那麼將被捕獲以確保成功重定向將更新錯誤重定向。

function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
	$plugin = plugin_basename( trim( $plugin ) );

	if ( is_multisite() && ( $network_wide || is_network_only_plugin( $plugin ) ) ) {
		$network_wide        = true;
		$current             = get_site_option( 'active_sitewide_plugins', array() );
		$_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
	} else {
		$current = get_option( 'active_plugins', array() );
	}

	$valid = validate_plugin( $plugin );
	if ( is_wp_error( $valid ) ) {
		return $valid;
	}

	$requirements = validate_plugin_requirements( $plugin );
	if ( is_wp_error( $requirements ) ) {
		return $requirements;
	}

	if ( $network_wide && ! isset( $current[ $plugin ] )
		|| ! $network_wide && ! in_array( $plugin, $current, true )
	) {
		if ( ! empty( $redirect ) ) {
			// We'll override this later if the plugin can be included without fatal error.
			wp_redirect( add_query_arg( '_error_nonce', wp_create_nonce( 'plugin-activation-error_' . $plugin ), $redirect ) );
		}

		ob_start();

		// Load the plugin to test whether it throws any errors.
		plugin_sandbox_scrape( $plugin );

		if ( ! $silent ) {
			/**
			 * Fires before a plugin is activated.
			 *
			 * If a plugin is silently activated (such as during an update),
			 * this hook does not fire.
			 *
			 * @since 2.9.0
			 *
			 * @param string $plugin       Path to the plugin file relative to the plugins directory.
			 * @param bool   $network_wide Whether to enable the plugin for all sites in the network
			 *                             or just the current site. Multisite only. Default false.
			 */
			do_action( 'activate_plugin', $plugin, $network_wide );

			/**
			 * Fires as a specific plugin is being activated.
			 *
			 * This hook is the "activation" hook used internally by register_activation_hook().
			 * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
			 *
			 * If a plugin is silently activated (such as during an update), this hook does not fire.
			 *
			 * @since 2.0.0
			 *
			 * @param bool $network_wide Whether to enable the plugin for all sites in the network
			 *                           or just the current site. Multisite only. Default false.
			 */
			do_action( "activate_{$plugin}", $network_wide );
		}

		if ( $network_wide ) {
			$current            = get_site_option( 'active_sitewide_plugins', array() );
			$current[ $plugin ] = time();
			update_site_option( 'active_sitewide_plugins', $current );
		} else {
			$current   = get_option( 'active_plugins', array() );
			$current[] = $plugin;
			sort( $current );
			update_option( 'active_plugins', $current );
		}

		if ( ! $silent ) {
			/**
			 * Fires after a plugin has been activated.
			 *
			 * If a plugin is silently activated (such as during an update),
			 * this hook does not fire.
			 *
			 * @since 2.9.0
			 *
			 * @param string $plugin       Path to the plugin file relative to the plugins directory.
			 * @param bool   $network_wide Whether to enable the plugin for all sites in the network
			 *                             or just the current site. Multisite only. Default false.
			 */
			do_action( 'activated_plugin', $plugin, $network_wide );
		}

		if ( ob_get_length() > 0 ) {
			$output = ob_get_clean();
			return new WP_Error( 'unexpected_output', __( 'The plugin generated unexpected output.' ), $output );
		}

		ob_end_clean();
	}

	return null;
}

常見問題

FAQs
檢視更多 >