Created
November 6, 2014 19:02
-
-
Save rbk/64db7cda57251fe1a271 to your computer and use it in GitHub Desktop.
Adds dropdown menu of navigation menus from the Wordpress menu system to page edit screens.
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 | |
/** | |
* Adds a box to the main column on the Post and Page edit screens. | |
*/ | |
function myplugin_add_meta_box() { | |
$screens = array( 'page' ); | |
foreach ( $screens as $screen ) { | |
add_meta_box( | |
'myplugin_sectionid', | |
__( 'Custom Sidebar Menu', 'myplugin_textdomain' ), | |
'myplugin_meta_box_callback', | |
$screen | |
); | |
} | |
} | |
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' ); | |
/** | |
* Prints the box content. | |
* | |
* @param WP_Post $post The object for the current post/page. | |
*/ | |
function myplugin_meta_box_callback( $post ) { | |
// Add an nonce field so we can check for it later. | |
wp_nonce_field( 'myplugin_meta_box', 'myplugin_meta_box_nonce' ); | |
/* | |
* Use get_post_meta() to retrieve an existing value | |
* from the database and use the value for the form. | |
*/ | |
$value = get_post_meta( $post->ID, 'sidebar_menu_name', true ); | |
?> | |
<p>Menus can be created in the WordPress menu system: Appearance -> Menus.</p> | |
<p>Once you create a menu, you can select it here.</p> | |
<p>This is an optional sidebar menu. For this menu to display you must set the current page to use the Sidebar Template.</p> | |
<select name="gurustu-sidebar-menu-name" id="gurustu-sidebar-menu-name"> | |
<option></option> | |
<?php | |
$menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) ); | |
foreach ( $menus as $menu ) { | |
if( $value == $menu->name ){ | |
echo '<option selected>'.$menu->name.'</option>'; | |
} else { | |
echo '<option>'.$menu->name.'</option>'; | |
} | |
} | |
?> | |
</select> | |
<?php | |
// echo '<label for="myplugin_new_field">'; | |
// _e( 'Description for this field', 'myplugin_textdomain' ); | |
// echo '</label> '; | |
// echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />'; | |
} | |
/** | |
* When the post is saved, saves our custom data. | |
* | |
* @param int $post_id The ID of the post being saved. | |
*/ | |
function myplugin_save_meta_box_data( $post_id ) { | |
/* | |
* We need to verify this came from our screen and with proper authorization, | |
* because the save_post action can be triggered at other times. | |
*/ | |
// Check if our nonce is set. | |
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) { | |
return; | |
} | |
// Verify that the nonce is valid. | |
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_meta_box' ) ) { | |
return; | |
} | |
// If this is an autosave, our form has not been submitted, so we don't want to do anything. | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return; | |
} | |
// Check the user's permissions. | |
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) { | |
if ( ! current_user_can( 'edit_page', $post_id ) ) { | |
return; | |
} | |
} else { | |
if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
return; | |
} | |
} | |
/* OK, it's safe for us to save the data now. */ | |
// Make sure that it is set. | |
if ( ! isset( $_POST['gurustu-sidebar-menu-name'] ) ) { | |
return; | |
} | |
// Sanitize user input. | |
$my_data = sanitize_text_field( $_POST['gurustu-sidebar-menu-name'] ); | |
// Update the meta field in the database. | |
update_post_meta( $post_id, 'sidebar_menu_name', $my_data ); | |
} | |
add_action( 'save_post', 'myplugin_save_meta_box_data' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment