Skip to content

How to add a Subtitle Meta Box to Post or Pages

StockSnap / Pixabay

Sometimes a title is not enough. So you might want to  add an additional subtitle to your posts or pages. Thankfully wordpress provides all the functionality to edit the content creation pages as you want.

This sneaky little snippet creates a subtitle field on top of the content box but below the actual title input on pages and posts.

Add this to your themes functions.php file:

 * Add a "deck" (aka subhead) meta box to post page(s) and position it
 * under the title.
 * @todo Move to class.
 * @see
 * @see
 * @see
 * @see

# Adds a box to the main column on the Post and Page edit screens:
function foo_deck($post_type) 

    # Allowed post types to show meta box:
    $post_types = array('post', 'page');

    if (in_array($post_type, $post_types)) 

        # Add a meta box to the administrative interface:
            'foo-deck-meta-box', // HTML 'id' attribute of the edit screen section.
            'Deck',              // Title of the edit screen section, visible to user.
            'foo_deck_meta_box', // Function that prints out the HTML for the edit screen section.
            $post_type,          // The type of Write screen on which to show the edit screen section.
            'advanced',          // The part of the page where the edit screen section should be shown.
            'high'               // The priority within the context where the boxes should show.


# Callback that prints the box content:
function foo_deck_meta_box($post) 

    # Use `get_post_meta()` to retrieve an existing value from the database and use the value for the form:
    $deck = get_post_meta($post->ID, '_deck', true);

    # Form field to display:



    # Display the nonce hidden form field:
        plugin_basename(__FILE__), // Action name.
        'foo_deck_meta_box'        // Nonce name.

 * @see

# Save our custom data when the post is saved:
function foo_deck_save_postdata($post_id)  current_user_can('edit_post', $post_id)))  ( ! DOING_AUTOSAVE)) && (( ! defined('DOING_AJAX')) 

# Get the deck:
function foo_get_deck($post_id = FALSE) 

    $post_id = ($post_id) ? $post_id : get_the_ID();

    return apply_filters('foo_the_deck', get_post_meta($post_id, '_deck', TRUE));

# Display deck (this will feel better when OOP):
function foo_the_deck() 

    echo foo_get_deck(get_the_ID());

# Conditional checker:
function foo_has_subtitle($post_id = FALSE) 

    if (foo_get_deck($post_id)) return TRUE;

# Define the custom box:
add_action('add_meta_boxes', 'foo_deck');
# Do something with the data entered:
add_action('save_post', 'foo_deck_save_postdata');

 * @see
 * @see

# Now move advanced meta boxes after the title:
function foo_move_deck() 

    # Get the globals:
    global $post, $wp_meta_boxes;

    # Output the "advanced" meta boxes:
    do_meta_boxes(get_current_screen(), 'advanced', $post);

    # Remove the initial "advanced" meta boxes:

add_action('edit_form_after_title', 'foo_move_deck');

Better cop from:

Dieser Beitrag hat 0 Kommentare

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

An den Anfang scrollen