The Ultimate Guide to WordPress Plugin Development: Essential Hooks, Functions, and Tips.

The Ultimate Guide to WordPress Plugin Development: Essential Hooks, Functions, and Tips.

With a strong background in WordPress development, focusing on custom themes, plugins, and performance optimization, I’m committed to building efficient, scalable, and engaging websites. I’m actively seeking dynamic WordPress development roles where I can apply my skills to impactful projects and continue enhancing my expertise.        

If you're getting into WordPress plugin development or looking to level up your skills, this guide is for you. Whether you're building something simple or a large-scale custom integration, these tools form the backbone of every successful plugin.

Let’s break down the most important hooks and functions with practical examples so you can start coding smarter today.


1. add_action() & do_action() – Action Hooks

What are they?

  • add_action() allows you to run your code at specific points in the WordPress lifecycle.
  • do_action() creates your own custom hook that others (or your own code) can attach to.

add_action() Example:

add_action('init', 'custom_plugin_init');

function custom_plugin_init() {
    error_log("Plugin loaded");
}        

do_action() Example:

function register_custom_user($user_data) {
    // Register user logic...
    do_action('after_custom_user_register', $user_data);
}

// Somewhere else
add_action('after_custom_user_register', 'send_custom_welcome');

function send_custom_welcome($user_data) {
    wp_mail($user_data['email'], 'Welcome!', 'Thank you for registering!');
}        

2. add_filter() & apply_filters() – Filter Hooks

What are they?

  • add_filter() lets you change existing data.
  • apply_filters() allows you to define your own filter hook.

Example:

add_filter('the_content', 'append_thank_you_note');

function append_thank_you_note($content) {
    return $content . "<p>Thank you for reading!</p>";
}        

3. AJAX – wp_ajax_ and wp_ajax_nopriv_

What is it?

Used for sending and handling AJAX requests in the frontend and backend.

Example:

add_action('wp_ajax_save_data', 'handle_ajax_save');
add_action('wp_ajax_nopriv_save_data', 'handle_ajax_save');

function handle_ajax_save() {
    echo 'Data saved!';
    wp_die(); // important to end AJAX
}        

4. register_activation_hook() – Setup on Activation

What is it?

Used to run code once, when your plugin is activated.

Example:

register_activation_hook(__FILE__, 'my_plugin_activate');

function my_plugin_activate() {
    add_option('my_plugin_installed', time());
}        

Use it when:

  • Creating custom database tables
  • Adding default options
  • Scheduling cron jobs

Optional but important if your plugin needs an initial setup.


5. WP_Query vs wp_get_recent_posts()

Article content

WP_Query Example:

$query = new WP_Query(['post_type' => 'book']);
while ($query->have_posts()) {
    $query->the_post();
    echo get_the_title();
}
wp_reset_postdata();        

wp_get_recent_posts() Example:

$posts = wp_get_recent_posts(['numberposts' => 5]);
foreach ($posts as $post) {
    echo $post['post_title'];
}        

6. wp_enqueue_script() & wp_enqueue_style() – Load Assets Properly

Example:

add_action('wp_enqueue_scripts', 'load_my_plugin_assets');

function load_my_plugin_assets() {
    wp_enqueue_style('plugin-css', plugin_dir_url(__FILE__) . 'style.css');
    wp_enqueue_script('plugin-js', plugin_dir_url(__FILE__) . 'script.js', ['jquery'], null, true);
}        

Always use this method for loading CSS and JS instead of hardcoding them.


7. $wpdb – Direct Database Access

Example:

global $wpdb;
$table = $wpdb->prefix . 'my_custom_table';
$wpdb->insert($table, ['name' => 'Sujoy', 'email' => 'sujoy@example.com']);        

Use with caution — always sanitize input.


8. Settings API – Add Admin Settings Pages

Example:

add_action('admin_init', 'register_my_plugin_settings');

function register_my_plugin_settings() {
    register_setting('my_group', 'my_option');
    add_settings_section('main', 'Main Settings', null, 'my_plugin');
    add_settings_field('field1', 'Option Name', 'my_field_callback', 'my_plugin', 'main');
}

function my_field_callback() {
    echo "<input type='text' name='my_option' value='" . get_option('my_option') . "' />";
}        

9. register_post_type() – Create Custom Post Types

Example:

register_post_type('book', [
    'label' => 'Books',
    'public' => true,
    'supports' => ['title', 'editor', 'thumbnail']
]);        

10. register_taxonomy() – Create Custom Taxonomies

Example:

register_taxonomy('genre', 'book', [
    'label' => 'Genre',
    'hierarchical' => true,
]);        

11. register_rest_route() – Expose Custom REST Endpoints

Example:

add_action('rest_api_init', function () {
    register_rest_route('myplugin/v1', '/info', [
        'methods' => 'GET',
        'callback' => function () {
            return ['status' => 'OK'];
        }
    ]);
});        

Great for headless or app integrations.


12. add_shortcode() – Add Shortcodes

Example:

add_shortcode('greeting', function () {
    return "Hello from my plugin!";
});        

Usage in content: [greeting]


13. Cron Jobs – Scheduled Events

Example:

if (!wp_next_scheduled('my_hourly_event')) {
    wp_schedule_event(time(), 'hourly', 'my_hourly_event');
}

add_action('my_hourly_event', 'do_scheduled_task');

function do_scheduled_task() {
    // Your repeating logic
}        

14. Form Security – Nonces

Example:

// In form
wp_nonce_field('my_action', 'my_nonce');

// On submit
if (!wp_verify_nonce($_POST['my_nonce'], 'my_action')) {
    die('Security check failed!');
}        

Nonces are crucial for protecting forms from CSRF attacks.


15. wp_mail() – Sending Emails

Example:

wp_mail('user@example.com', 'Welcome!', 'Thanks for signing up!');        

You can filter or log failed emails using wp_mail_failed.


Conclusion

Whether you're building a simple utility plugin or a full-featured solution, mastering these core functions and hooks will make your development journey smooth and future-proof.

If you're looking for a WordPress developer with expertise in database management and site optimization, feel free to connect with me here on LinkedIn or send me a message to discuss how we can collaborate.        
Rony Pramanick

Freelancer| Aspiring Graphic & UX/UI Designer | Skilled in CRM & Client Relations | Open to Work-from-Home Opportunities

1w

Ai post gulo fb Teo upload koro

Like
Reply

To view or add a comment, sign in

More articles by Sujoy Sen

Insights from the community

Others also viewed

Explore topics