Skip to content

Instantly share code, notes, and snippets.

@philipdowner
Created February 7, 2015 14:51
Show Gist options
  • Save philipdowner/84ce8c328a8dd2a23c8c to your computer and use it in GitHub Desktop.
Save philipdowner/84ce8c328a8dd2a23c8c to your computer and use it in GitHub Desktop.
Quick example of adding a meta box to WordPress based on user permissions
<?php
// This is the important part. You only add the meta box if the user has the required capability(ies).
// http://codex.wordpress.org/Roles_and_Capabilities
//
// You could add additional checks to ensure the meta box is only displayed on certain pages.
if( current_user_can('my_custom_capability') ) { //Change the capability depending on your user permissions
add_action('add_meta_boxes_page', 'my_callback_function');
}
/**
* My Callback Function
* Adds the custom meta box to the admin in the specified area.
*
* @return void
*/
function my_callback_function() {
add_meta_box('htmlid', 'My Meta Box', 'my_metabox_callback_function');
}
/**
* My Metabox Callback Function
* Displays the meta box when called
*
* @param obj $post The current post (page) object
* @return void
*/
function my_metabox_callback_function($post) {
//Echo out your custom fields here. Instructions for saving your meta box on submit are here:
//http://codex.wordpress.org/Function_Reference/add_meta_box
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment