Complete Concept of Plugin Activation Hook in WordPress

Reading Time: 8 minutes
140 Views

WordPress, a robust and extensively used content management system, provides users with the ability to enhance its functionality through plugins. Activating plugins is an important step in adding new functionality into your WordPress website. To control duties during plugin activation, WordPress provides a powerful mechanism known as the “activation hook“.

In this detailed guide, we will look at the plugin activation hook in WordPress and provide you with a clear knowledge as well as practical examples.

You’ll discover how to make a plugin, register the activation hook, and perform specific actions during the activation process. We’ll also go over common circumstances where activation hooks might be useful.

Read More: Concept of Deactivation Hook in WordPress Tutorial

Let’s get started.

What is WordPress Plugin Activation Hook?

A plugin activation hook in WordPress is a special event that occurs when a plugin is activated on a WordPress website. When a developer’s plugin is activated, it allows them to run custom code or functions.

The primary aim of employing activation hooks is to conduct setup activities, database operations, or any other actions required to completely functionalize the plugin after activation.

Key Component of a Plugin Activation Hook

The activation function, which is defined in the plugin’s main PHP file, is the most important component of a plugin activation hook.

When the plugin is activated, this function is automatically triggered, and it normally involves the following tasks:

  • Setting Default Options: Activation hooks are frequently used to configure the plugin’s default options and settings, ensuring that it operates as intended immediately after activation.
  • Database Configuration: If the plugin necessitates the creation of a custom database table or data structures, the activation function can do so in the WordPress database.
  • Adding Custom Roles or Capabilities: The activation function can create custom user roles or add specific capabilities to existing roles for security and access control.
  • Displaying Activation Messages: Upon successful activation, activation hooks can display informational messages or notifications to the user, providing instructions or advice on how to get started with the plugin.

Syntax

Here’s an example of how a plugin activation hook is defined within a WordPress plugin:

function my_plugin_activation() {

    // Perform activation tasks, such as setting default options, creating database tables, etc.
}

register_activation_hook(__FILE__, 'my_plugin_activation');

Plugin Activation: How To Create a Database Table?

Creating a database table while activating a WordPress plugin is a typical operation, especially if your plugin requires data storage and management.

To build a database table during plugin activation, follow these steps:

  • Define the Database Table Structure
  • Write the Activation Function
  • Use the dbDelta Function
  • Execute the Activation Function

Here’s an example of how to create a database table during plugin activation:

function my_plugin_activation() {

    global $wpdb;

    $table_name = $wpdb->prefix . 'my_custom_table';

    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        name varchar(255) NOT NULL,
        email varchar(255) NOT NULL,
        created_at datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
        UNIQUE (id)
    ) $charset_collate;";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    
    dbDelta($sql);
}

register_activation_hook(__FILE__, 'my_plugin_activation');

Code Explanation,

  • We define the activation function my_plugin_activation.
  • We specify the table name and it’s structure.
  • The dbDelta function processes the SQL query and ensures the table is created or updated as needed.
  • register_activation_hook is used to register the activation function to run when the plugin is activated.

Read More: How To Stop Spam User Registration in WordPress Using Plugin

Plugin Activation: How To Create a WordPress Page?

When you wish to give specified content or functionalities that require specific pages, creating a WordPress page after plugin activation can be handy.

Here’s how to make a WordPress page while a plugin is being activated:

  • Define the Page Content
  • Write the Activation Function
  • Use the wp_insert_post Function:
  • Execute the Activation Function

Here’s an example of how to create a WordPress page during plugin activation:

function my_plugin_activation() {
  
    $page_title = 'My Custom Page';
    $page_content = 'This is the content of my custom page.';
    $page_template = ''; // You can specify a page template if needed.
    
    $page = array(
        'post_title' => $page_title,
        'post_content' => $page_content,
        'post_status' => 'publish',
        'post_type' => 'page',
        'page_template' => $page_template,
    );

    $page_id = wp_insert_post($page);

    if ($page_id !== 0) {
        // Page created successfully.
    }
}

register_activation_hook(__FILE__, 'my_plugin_activation');

Plugin Activation: How To Add Custom Roles in WordPress?

When your plugin requires unique user skills or access levels, adding custom user roles in WordPress after plugin activation might be a handy feature.

Here’s how to activate a plugin with custom roles:

  • Write the Activation Function
  • Use the add_role Function
  • Define Role Capabilities
  • Execute the Activation Function

Read More: How To Disable RSS Feed URLs in WordPress Website Tutorial

Here’s an example of how to add custom user roles in WordPress during plugin activation:

function my_plugin_activation() {
  
    // Define the custom role and its capabilities.
    $role = 'custom_role';
  
    $capabilities = array(
        'read' => true,
        'edit_posts' => true,
        // Add more capabilities as needed.
    );

    // Add the custom role.
    add_role($role, 'Custom Role', $capabilities);
}

register_activation_hook(__FILE__, 'my_plugin_activation');

Additionally, you can also do Message Setup, Database Setup, Default Options Setup, etc on plugin activation.

That’s it.

We hope this article helped you to learn about Complete Concept of Plugin Activation Hook in WordPress in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more