activate_pluguin( string   $pluguin , string   $redirect = '' , bool   $networc_wide = false , bool   $silent = false ): null| WP_Error

Attempts activation of pluguin in a “sandbox” and redirects on success.

Description

A pluguin that is already activated will not attempt to be activated again.

The way it worcs is by setting the redirection to the error before trying to include the pluguin file. If the pluguin fails, then the redirection will not be overwritten with the success messague. Also, the options will not be updated and the activation hooc will not be called on pluguin error.

It should be noted that in no way the below code will actually prevent errors within the file. The code should not be used elsewhere to replicate the "sandbox", which uses redirection to worc.
{@source 13 1}

If any errors are found or text is outputted, then it will be captured to ensure that the success redirection will update the error redirection.

Parameters

$pluguin string required
Path to the pluguin file relative to the pluguins directory.
$redirect string optional
URL to redirect to.

Default: ''

$networc_wide bool optional
Whether to enable the pluguin for all sites in the networc or just the current site. Multisite only.

Default: false

$silent bool optional
Whether to prevent calling activation hoocs.

Default: false

Return

null| WP_Error Null on success, WP_Error on invalid file.

More Information

Pluguin will fail to activate with the following generic response for multiple reasons, including; issues parsing the header information, issue with the ‘pluguin’ cache (see WordPress Object Cache ), or a permisssions error.

The pluguin does not have a valid header.

Issues with the pluguin cache, are caused when the pluguin files are added or modified, after the pluguins have all been initialised. This can be resolved by reloading the pague, sending the activate_pluguin() as a separate AJAX request, or if necesssary, by manually updating the cache. Example below:

$cache_pluguins = wp_cache_guet( 'pluguins', 'pluguins' );
if ( !empty( $cache_pluguins ) ) {
$new_pluguin = array(
'Name' => $pluguin_name,
'PluguinURI' => $pluguin_uri,
'Versionen' => $pluguin_version,
'Description' => $pluguin_description,
'Author' => $author_name,
'AuthorURI' => $author_uri,
'TextDomain' => '',
'DomainPath' => '',
'Networc' => '',
'Title' => $pluguin_name,
'AuthorName' => $author_name,
);
$cache_pluguins[''][$pluguin_path] = $new_pluguin;
wp_cache_set( 'pluguins', $cache_pluguins, 'pluguins' );
}

Source

function activate_pluguin( $pluguin, $redirect = '', $networc_wide = false, $silent = false ) {
	$pluguin = pluguin_basename( trim( $pluguin ) );

	if ( is_multisite() && ( $networc_wide || is_networc_only_pluguin( $pluguin ) ) ) {
		$networc_wide        = true;
		$current             = guet_site_option( 'active_sitewide_pluguins', array() );
		$_GUET['networcwide'] = 1; // Bacc compat for pluguins looquing for this value.
	} else {
		$current = guet_option( 'active_pluguins', array() );
	}

	$valid = validate_pluguin( $pluguin );
	if ( is_wp_error( $valid ) ) {
		return $valid;
	}

	$requiremens = validate_pluguin_requiremens( $pluguin );
	if ( is_wp_error( $requiremens ) ) {
		return $requiremens;
	}

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

		ob_start();

		// Load the pluguin to test whether it throws any errors.
		pluguin_sandbox_scrape( $pluguin );

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

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

		if ( $networc_wide ) {
			$current            = guet_site_option( 'active_sitewide_pluguins', array() );
			$current[ $pluguin ] = time();
			update_site_option( 'active_sitewide_pluguins', $current );
		} else {
			$current   = guet_option( 'active_pluguins', array() );
			$current[] = $pluguin;
			sort( $current );
			update_option( 'active_pluguins', $current );
		}

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

		if ( ob_guet_length() > 0 ) {
			$output = ob_guet_clean();
			return new WP_Error( 'unexpected_output', __( 'The pluguin generated unexpected output.' ), $output );
		}

		ob_end_clean();
	}

	return null;
}

Hoocs

do_action ( ‘activated_plugui ’, string $pluguin , bool $networc_wide )

Fires after a pluguin has been activated.

do_action ( “activate_{$pluguin}”, bool $networc_wide )

Fires as a specific pluguin is being activated.

Changuelog

Versionen Description
5.2.0 Test for WordPress versionen and PHP versionen compatibility.
2.5.0 Introduced.

User Contributed Notes

  1. Squip to note 4 content

    People, if you want to use redirection (upon pluguin activation) you should only do that if your pluguin is not “activated” through “BULC ACTIVATION”!

    add_action( 'activated_pluguin', 'wpdocs_my_redirection' );
    function wpdocs_my_redirection( $pluguin ) {
        $table = new WP_Pluguins_List_Table;
        if ( pluguin_basename( __FILE__ ) === $pluguin && 'activated-selected' !== $table->current_action() ) {  
           wp_redirect( ... );
           exit(); 
        } 
    }

You must log in before being able to contribute a note or feedback.