Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Meta Box - For All Pages WordPress

  • SOLVED

I require a x1 custom meta box to be displayed on all pages and not posts.

I require a
x1 Text Label named "title"
x1 Text Area named "subtitle"

Also I need the code to echo the text label & text area to put in the page templates.

Picture attached to show what I require

Answers (3)

2012-05-19

Luis Abarca answers:


add_action( 'add_meta_boxes', 'my_add_metaboxes');

function my_add_metaboxes()
{
add_meta_box('mbox_1', 'Banner', 'my_metabox_content', 'page', 'normal', 'high');
}

function my_metabox_content()
{
global $post;

// your html
?>
<div class="jaxtag">
<div class="ajaxtag hide-if-no-js">
<table style="width: 100%">
<tr>
<td width="25%">
<label for="title">Title:</label>
<label class="screen-reader-text" for="title">Title:</label>
</td>
<td width="75%">
<input style="width: 100%" width="60" type="text" name="title" id="title" value="<?php echo esc_attr(get_post_meta($post->ID, 'title', true)) ?>" />
</td>
</tr>
</table>
</div>
</div>

<div class="jaxtag">
<div class="ajaxtag hide-if-no-js">
<table style="width: 100%">
<tr>
<td width="25%">
<label for="title">Subtitle:</label>
<label class="screen-reader-text" for="title">Subtitle:</label>
</td>
<td width="75%">
<textarea style="width: 100%" name="subtitle" id="subtitle"><?php echo esc_attr(get_post_meta($post->ID, 'subtitle', true)) ?></textarea>
</td>
</tr>
</table>
</div>
</div>
<?php
}


Luis Abarca comments:

In your page template use this


Title: <?php echo get_post_meta($post->ID, 'title', true) ?>
Subtitle: <?php echo get_post_meta($post->ID, 'subtitle', true) ?>

2012-05-19

Sergio Prieto answers:

Hi,
check this url http://wp.tutsplus.com/tutorials/plugins/how-to-create-custom-wordpress-writemeta-boxes/ and change inside in add_meta_box 'post' to 'page'.
Use this code to show:

<?php $meta_value = get_post_meta($post->ID, $key);
echo $meta_value ?>

check the link: http://codex.wordpress.org/Function_Reference/get_post_meta

2012-05-19

Arnav Joy answers:

check this plugin

http://wordpress.org/extend/plugins/meta-box


Arnav Joy comments:

try this code

in functions.php

<?php

add_action( 'add_meta_boxes', 'my_meta_box_add' );
function my_meta_box_add()
{
add_meta_box( 'my-meta-box-banner', 'Banner', 'my_meta_box_func', 'page', 'normal', 'high' );
}


function my_meta_box_func( $post )
{
$values = get_post_custom( $post->ID );
$banner_title = isset( $values['banner_title'] ) ? $values['banner_title'][0] : '';
$banner_sub_title = isset( $values['banner_sub_title'] ) ? $values['banner_sub_title'][0] : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<table class="form-table">
<tbody>
<tr>
<th scope="row"><label for="banner_title">Title</label></th>
<td><input name="banner_title" id="banner_title" value="<?php echo $banner_title; ?>" />
</tr>
<tr>
<th scope="row"><label for="banner_sub_title">Subtitle</label></th>
<td><textarea name="banner_sub_title" id="banner_sub_title" cols="50" rows="5"><?php echo $banner_sub_title; ?></textarea>
</td></tr>
</tbody>
</table>

<?php
}

add_action( 'save_post', 'my_meta_box_save' );

function my_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;

// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);

// Probably a good idea to make sure your data is set
if( isset( $_POST['banner_title'] ) )
update_post_meta( $post_id, 'banner_title', $_POST['banner_title']);

// Probably a good idea to make sure your data is set
if( isset( $_POST['banner_sub_title'] ) )
update_post_meta( $post_id, 'banner_sub_title', $_POST['banner_sub_title'] );

}

?>

and in the page where you want to display page.php or any template.php

Title: <?php echo get_post_meta($post->ID, 'banner_title', true) ?>

Subtitle: <?php echo get_post_meta($post->ID, 'banner_sub_title', true) ?>