Using WordPress ‘page_attributes_meta_box_template’ PHP action

The page_attributes_meta_box_template WordPress PHP action fires immediately after the label inside the ‘Template’ section of the ‘Page Attributes’ meta box.

Usage

add_action('page_attributes_meta_box_template', 'your_custom_function', 10, 2);

function your_custom_function($template, $post) {
    // your custom code here
}

Parameters

  • $template (string|false) – The template used for the current post.
  • $post (WP_Post) – The current post.

More information

See WordPress Developer Resources: page_attributes_meta_box_template

Examples

Add custom content after the ‘Template’ label

This example adds a custom message after the ‘Template’ label in the ‘Page Attributes’ meta box.

add_action('page_attributes_meta_box_template', 'add_custom_message', 10, 2);

function add_custom_message($template, $post) {
    echo '<p><strong>Note:</strong> Choose the right template for your page.</p>';
}

Display a custom alert based on the template

This example displays a custom alert based on the selected template.

add_action('page_attributes_meta_box_template', 'display_template_alert', 10, 2);

function display_template_alert($template, $post) {
    if ($template == 'custom-template.php') {
        echo '<p><strong>Alert:</strong> This template has specific requirements.</p>';
    }
}

Show a custom image based on the selected template

This example shows a custom image based on the selected template.

add_action('page_attributes_meta_box_template', 'show_template_image', 10, 2);

function show_template_image($template, $post) {
    if ($template == 'homepage-template.php') {
        echo '<img src="/path/to/your/image.jpg" alt="Template Preview">';
    }
}

Display custom instructions for specific templates

This example displays custom instructions for specific templates.

add_action('page_attributes_meta_box_template', 'display_template_instructions', 10, 2);

function display_template_instructions($template, $post) {
    $instructions = array(
        'about-page-template.php' => 'Please fill out the "About" section.',
        'contact-page-template.php' => 'Please add your contact information.'
    );

    if (array_key_exists($template, $instructions)) {
        echo '<p><strong>Instructions:</strong> ' . $instructions[$template] . '</p>';
    }
}

Add custom CSS based on the selected template

This example adds custom CSS to the admin area based on the selected template.

add_action('page_attributes_meta_box_template', 'add_custom_css', 10, 2);

function add_custom_css($template, $post) {
    if ($template == 'full-width-template.php') {
        echo '<style>.postbox-container { background-color: #f5f5f5; }</style>';
    }
}