public
Last active

Sample Meta Box

  • Download Gist
meta-box-sample.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
<?php
 
/* 1. Back End Usage
---------------------------------------------------------------------*/
 
// add neccessary actions
// @see http://codex.wordpress.org/Function_Reference/add_action
add_action( 'add_meta_boxes', 'rg_add_custom_box' );
add_action( 'save_post', 'rg_save_postdata', 10, 1 );
 
/**
* Adds a box to the main column on the Post edit screen
*
* @hook add_meta_boxes
* @see http://codex.wordpress.org/Function_Reference/add_meta_box
*/
function rg_add_custom_box() {
add_meta_box(
'rg_sectionid', __( 'WP Editor Meta Box', 'rg_textdomain' ), 'rg_inner_custom_box', 'post'
);
}
 
/**
* Function that renders the Meta Box
* called as the 3rd parameter of add_meta_box
*/
function rg_inner_custom_box($post) {
 
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'rg_noncename' );
 
// The actual fields for data entry
$meta = get_post_meta( $post->ID, 'rg_wp_editor_sample', true );
?>
<?php
 
$content = $meta ? $meta['es'] : '';
// provide textarea name for $_POST variable
$args = array(
'textarea_name' => 'rg_wp_editor_sample',
);
// render wp_editor with custom arguments
wp_editor( $content, 'rg_wp_editor_sample', $args );
?>
 
<?php
 
}
 
/**
* When the post is saved, save our custom data
*
* @hook save_post
*/
function rg_save_postdata($post_id) {
 
// check if we're on the general post (post, page, cpt, etc.) page and if the nonce has been set
if ( !isset( $_POST['post_type'] ) or !isset( $_POST['rg_noncename'] ) ) {
return;
}
// check if the we're on the post page
if ( !in_array( $_POST['post_type'], array( 'post' ) ) ) {
return;
}
 
// if we're doing an auto save return
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
 
// verify nonce for the safety reasones
// @see http://codex.wordpress.org/WordPress_Nonces
if ( !wp_verify_nonce( $_POST['rg_noncename'], plugin_basename( __FILE__ ) ) )
return;
 
// make sure that current user has proper rights to save the post
// @see http://codex.wordpress.org/Roles_and_Capabilities
if ( !current_user_can( 'edit_post', $post_id ) )
return;
 
 
// get the data from the $_POST
$es = $_POST['rg_wp_editor_sample'];
 
 
// store the meta as an array in case something else was added later on
$meta = array(
'es' => $es,
);
 
update_post_meta( $post_id, 'rg_wp_editor_sample', $meta );
}
 
/* 2. Front End Usage
--------------------------------------------------------------------------------*/
 
// use within the loop
// @see http://codex.wordpress.org/The_Loop
$meta = get_post_meta( $post->ID, 'rg_wp_editor_sample', true );
echo isset($meta['es']) ? apply_filters('the_content', $meta['es']) : 'no Meta Box meta saved';

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.