Introduction
A plugin header tells WordPress about your plugin. Activation lets WordPress start your plugin so it works.
A plugin header tells WordPress about your plugin. Activation lets WordPress start your plugin so it works.
<?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 }
register_activation_hook to run code only when the plugin is activated.<?php
/*
Plugin Name: Simple Greeting
Description: Shows a greeting message.
Version: 1.0
Author: Jane Doe
*/<?php register_activation_hook(__FILE__, 'activate_greeting_plugin'); function activate_greeting_plugin() { error_log('Greeting plugin activated!'); }
<?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); });
This plugin sets a welcome message when activated. The message shows once in the WordPress admin area and then disappears.
<?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'); } }
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.
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.
and ending with */ ?>.register_activation_hook(__FILE__, function() {
update_option('my_plugin_activated', true);
});update_option to set 'my_plugin_activated' to true in the WordPress options table.register_activation_hook(__FILE__, 'activate_plugin');
function activate_plugin() {
add_option('plugin_activated', true);
}add_option adds a new option only if it does not exist. If the option exists, it fails silently. update_option adds or updates the option.add_option will not update the value, possibly causing unexpected behavior.Pass the test and this pattern moves unknown