Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Sample Meta Box
<?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';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.