Saving and retrieving data from wp_dropdown_pages metabox
-
Hello all,
I’ve built a slider plugin using custom post types. My goal is to be able specify the page URL in the custom post type and then be able to use it later in a public function widget to populate a unique link in each slide.
I’ve got the metabox to populate a dropdown of all pages using wp_dropdown_pages with no issues. I’m struggling with getting the selection to save and then retrieving the selected URL to be used later.
My PHP is pretty shaky at best but I’m eager to learn. I’ve read several Codex entries including ones on both wp_dropdown_pages and metaboxes but haven’t been able work out a solution. If any of you have any suggestions or can point in the direction of some additional reading that my be of assistance I’d be grateful!
This is what I’ve come up with:
add_action( 'add_meta_boxes', 'carousel_tabs_url' ); //Output of metabox content function carousel_tabs_url_callback( $post ) { $dropdown_args = array( 'post_type' => 'page', 'name' => 'myplugin[page]', 'sort_column' => 'menu_order, post_title', 'echo' => 1, ); // Use nonce for verification wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename'); //Dropdown of pages wp_dropdown_pages( $dropdown_args ); } //Save wp_dropdown_pages function carousel_tabs_url_save( $post_id ) { // Check save status $is_autosave = wp_is_post_autosave( $post_id ); $is_revision = wp_is_post_revision( $post_id ); $is_valid_nonce = ( isset( $_POST[ 'my_custom_nonce' ] ) && wp_verify_nonce( $_POST[ 'my_custom_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false'; // Exit script depending on save status if ($is_autosave || $is_revision || !$is_valid_nonce ) { return; } // Check for input and sanitizes/save if needed if ( isset( $_POST[ 'meta_key' ] ) ) { update_post_meta( $post_id, 'meta_key', $_POST[ 'meta_key' ] ); } } //fire save action add_action( 'save_post', 'carousel_tabs_url_save' ); //loads saved URL into var $carousel_tab_url=$_POST['myplugin']['page'];
- The topic ‘Saving and retrieving data from wp_dropdown_pages metabox’ is closed to new replies.