Working with Actions

·

·

WordPress actions are one of the two main types of hooks that let you execute custom code at specific points during WordPress execution. Unlike filters, actions do not return data—they simply perform tasks.

In this guide, we’ll explore three common and practical uses of actions:

  1. Enqueueing assets (CSS & JavaScript)
  2. Registering a custom post type
  3. Adding an admin menu

📌 1. Enqueueing Assets

When building a theme or plugin, you often need to load CSS and JavaScript files. WordPress provides a safe way to do this using the wp_enqueue_scripts action.

🔹 Syntax
add_action( 'wp_enqueue_scripts', 'callback_function' );
  • wp_enqueue_scripts → the hook for front-end asset loading
  • callback_function → your custom function to enqueue styles and scripts
🔹 Example
add_action( 'wp_enqueue_scripts', function () {
    // Enqueue main stylesheet
    wp_enqueue_style( 'site', get_stylesheet_uri(), [], '1.0.0' );

    // Enqueue JavaScript file
    wp_enqueue_script( 'site-js', get_stylesheet_directory_uri() . '/site.js', [], '1.0.0', true );
});

Explanation:

  • wp_enqueue_style loads a CSS file safely.
  • wp_enqueue_script loads a JS file in the footer (true as last parameter).
  • Using add_action ensures WordPress loads your assets at the correct point in the page load.

Best Practice: Always use WordPress functions (wp_enqueue_style, wp_enqueue_script) instead of hardcoding <link> or <script> tags.


📌 2. Registering a Custom Post Type

Custom post types (CPTs) allow you to organize content beyond posts and pages, such as books, portfolios, or events. Use the init action to register them during WordPress initialization.

🔹 Syntax
add_action( 'init', 'callback_function' );
🔹 Example
add_action( 'init', function () {
    register_post_type( 'book', [
        'label' => 'Books',
        'public' => true,
        'show_in_rest' => true, // Enables Gutenberg editor & REST API
        'supports' => [ 'title', 'editor', 'thumbnail' ],
    ]);
});

Explanation:

  • register_post_type defines a new post type with a label, visibility, and features.
  • show_in_rest enables the block editor and REST API support.
  • supports specifies features like title, editor, or thumbnail.

Tip: Always use init to register CPTs to ensure WordPress is fully loaded before registering content types.


📌 3. Adding an Admin Menu

Actions can also be used to add custom pages to the WordPress admin dashboard. The admin_menu hook lets you register new menus.

🔹 Syntax
add_action( 'admin_menu', 'callback_function' );
🔹 Example
add_action( 'admin_menu', function () {
    add_menu_page(
        'Training Settings',      // Page title
        'Training',               // Menu title
        'manage_options',         // Capability required
        'training-settings',      // Menu slug
        function () {             // Callback to display content
            echo '<div class="wrap"><h1>Training Settings</h1><p>Welcome!</p></div>';
        }
    );
});

Explanation:

  • add_menu_page creates a new top-level menu in the WordPress admin.
  • The callback function outputs HTML for the page.
  • manage_options ensures only administrators can access it.

Tip: For larger plugins, move the callback into a separate function file instead of an anonymous function.


📌 Summary

TaskHookPurpose
Enqueue CSS/JS assetswp_enqueue_scriptsSafely add front-end styles and scripts
Register custom post typeinitDefine new content types during WordPress initialization
Add admin menuadmin_menuAdd custom pages or menus to WordPress dashboard

Key Takeaways:

  • Actions let you run code at specific points without returning data.
  • Always hook into the correct action (wp_enqueue_scripts, init, admin_menu) to ensure WordPress loads everything properly.
  • Use WordPress functions instead of hardcoding functionality for better compatibility and maintainability.

I can also create a visual flowchart showing how these actions execute in WordPress, which is great for beginners to understand timing and order.