Add a page description meta box in wordpress

Views: 45 Last modified: August 02nd, 2011 Comments: 0

Depending on the reason why you use wordpress pages, it might be quite convenient for you adding descriptions to them. With the below snippet you are able to add some descriptions on pages. Just copy and paste the below snippet into functions.php for instance, and a new meta box will be shown when you add a new page or edit a page.

It stores his data into the postmeta table, with as meta_key “page_desc”.

<?php
/**
 * AddPageDescription()
 */
// Adds the meta box
function AddPageDescription() {
    add_meta_box(
        'page_description',
        __( 'Page description'),
        'PageDescription',
        'page'
    );
}

/**
 * PageDescription()
 */
function PageDescription() {
    // Label and text field
    echo '<label for="page_description">';
        _e("Page description");
    echo '</label> ';
    echo '<input type="text" id="page_description" name="page_description" value="" size="250" />';
}

/**
 * SaveDescriptionBox()
 */
function SaveDescriptionBox() {
    // Against auto save
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
      return;
    // If the post type is page
    if ( 'page' == $_POST['post_type'] )
    {
        // And the user can't edit - stop execution of function
        if ( !current_user_can( 'edit_page', $post_id ) )
            return;
    }

    // update the actual post meta - can be found in the database under page_desc @ table prefix _postmeta
    if (trim($_POST['page_description']) != '')
        update_post_meta(get_the_ID(), 'page_desc', $_POST['page_description']);
}

// Hook onto the admin hook
add_action( 'admin_init', 'AddPageDescription');

// Save the meta box
add_action( 'save_post', 'SaveDescriptionBox' );
?>
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

    Mail this!

    To: From:Sum {0+2} =  
    Anything to add ?

        You must be logged in to post a comment.