Skip to main content
edited tags
Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459
Source Link
Mahdi
  • 29
  • 2

how does a plugin get active in wordpress

I am trying to understand the mechanism that wordpress active a plugin for now i find the register_activation_hook in wp-includes/plugin.php

{
function register_activation_hook($file, $function) {
    $file = plugin_basename($file);
    add_action('activate_' . $file, $function);
}

and activate_plugin in wp-admin/includes/plugin.php

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;

    if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) {
        if ( !empty($redirect) )
            wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
        ob_start();
        wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
        $_wp_plugin_file = $plugin;
        include_once( WP_PLUGIN_DIR . '/' . $plugin );
        $plugin = $_wp_plugin_file; // Avoid stomping of the $plugin variable in a 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 main plugin file from 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 is 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 is 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 main plugin file from 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 is 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;
}

And i sense first the plugin is activated through the activate_plugin And when the plugin is activated successfully then the register_activation_hook would be performed. If it is like the way i said how does plugin now it is activated to run the register_activation_hook ? Or if I am wrong i would thank you guys to help me to figure out the mechanism.