0
0
Wordpressframework~5 mins

Plugin header and activation in Wordpress

Choose your learning style9 modes available
Introduction

A plugin header tells WordPress about your plugin. Activation lets WordPress start your plugin so it works.

When creating a new WordPress plugin to add features to a website.
When you want WordPress to recognize your plugin and show it in the admin area.
When you want to run setup tasks like creating database tables when the plugin starts.
When you want to enable or disable your plugin easily from the WordPress dashboard.
Syntax
Wordpress
<?php
/*
Plugin Name: Your Plugin Name
Plugin URI: https://example.com
Description: A short description of your plugin.
Version: 1.0
Author: Your Name
Author URI: https://yourwebsite.com
License: GPL2
*/

register_activation_hook(__FILE__, 'your_plugin_activate');

function your_plugin_activate() {
    // Code to run on activation
}
The plugin header must be at the very top of your main plugin file.
Use register_activation_hook to run code only when the plugin is activated.
Examples
This is a minimal plugin header with the basic required fields.
Wordpress
<?php
/*
Plugin Name: Simple Greeting
Description: Shows a greeting message.
Version: 1.0
Author: Jane Doe
*/
This code logs a message when the plugin is activated.
Wordpress
<?php
register_activation_hook(__FILE__, 'activate_greeting_plugin');

function activate_greeting_plugin() {
    error_log('Greeting plugin activated!');
}
This example creates a database table when the plugin activates.
Wordpress
<?php
/*
Plugin Name: Data Setup Plugin
Version: 1.0
Author: Dev
*/

register_activation_hook(__FILE__, function() {
    global $wpdb;
    $table = $wpdb->prefix . 'my_table';
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        name tinytext NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

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

This plugin sets a welcome message when activated. The message shows once in the WordPress admin area and then disappears.

Wordpress
<?php
/*
Plugin Name: Welcome Message
Description: Displays a welcome message on activation.
Version: 1.0
Author: Friendly Dev
*/

register_activation_hook(__FILE__, 'welcome_message_activate');

function welcome_message_activate() {
    update_option('welcome_message', 'Hello! Thanks for activating the plugin.');
}

add_action('admin_notices', 'show_welcome_message');

function show_welcome_message() {
    if ($message = get_option('welcome_message')) {
        echo '<div class="notice notice-success is-dismissible"><p>' . esc_html($message) . '</p></div>';
        delete_option('welcome_message');
    }
}
OutputSuccess
Important Notes

Always place the plugin header at the top of your main plugin file for WordPress to detect it.

Use register_activation_hook carefully to avoid errors during activation.

Activation code runs only once when the plugin is turned on, not on every page load.

Summary

The plugin header tells WordPress about your plugin.

Activation hooks run code when the plugin is enabled.

Use activation to set up things like options or database tables.