Created
February 7, 2015 14:51
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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