Skip to content

Instantly share code, notes, and snippets.

@offroadkev
Last active October 1, 2015 19:16
Show Gist options
  • Save offroadkev/bd2c73cf5baee5252eb4 to your computer and use it in GitHub Desktop.
Save offroadkev/bd2c73cf5baee5252eb4 to your computer and use it in GitHub Desktop.
WordPress Meta Box with a wp_editor field
<?php
// Some quick notes about this code:
// Keep in mind this is for a 'portfolio' post type.
// Also, you'll notice the meta key sometimes has a leading underscore. This is so the meta field and saved data don't appear in the native 'custom fields' fields, just in case you have them supported in your post type args
// While it says this should go in functions.php, it should really go in your post type plugin file. In this case it would be the portfolio.php file
// Services Provided meta box & wp_editor field
function services_provided_meta_box(){
add_meta_box("services-provided-meta-fields", "Services Provided", "services_provided_meta", 'portfolio', "normal", "low");
}
add_action("admin_init", "services_provided_meta_box");
function services_provided_meta(){
global $post;
$custom = get_post_custom($post->ID);
$sprovided = (!empty($custom["_sprovided"][0])) ? $custom["_sprovided"][0] : '';
?>
<div>
<?php wp_editor( $sprovided, 'sprovided', $settings = array('textarea_rows'=>40) ); ?>
</div>
<?php
}
function save_points($postid,$post){
global $_POST;
// set the ID to the parent post, not the revision
$postid = (wp_is_post_revision( $postid )) ? wp_is_post_revision( $post ) : $postid;
$post_type = get_post_type( $postid );
if ('portfolio' == $post_type) {
update_post_meta($postid, "_sprovided", $_POST["sprovided"]); // save the data
}
}
add_action('save_post', 'save_points', 1, 2);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment