• 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'];
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You mentioned the Codex, but not the Plugin Handbook. Maybe this will help:
    https://developer.www.ads-software.com/plugins/metadata/custom-meta-boxes/

    Thread Starter shnzndl

    (@shnzndl)

    I hadn’t checked there yet, thanks for the info!

    For posterity reasons, in case anyone is wondering, I got it all figured out, I hadn’t to passed the correct ‘name’ of the $dropdown_args into the save function.

    $dropdown_args = array(
            'post_type'        => 'page',
            'name'             => 'linked_page',
            'sort_column'      => 'menu_order, post_title',
            'echo'             => 1,
        );
    if ( isset( $_POST[ 'linked_page' ] ) ) {
    		$linked_page_id = $_POST[ 'linked_page' ][0];
    	    update_post_meta( $post_id, 'carousel_tabs_url_id', $linked_page_id );
    	}

    I got it to save no problem but a new issue cropped up where the linked page in wp_dropdown_pages dropdown needed to be reselected upon each update since the placeholder defaulted to the first item.

    I ended up scrapping it as it was getting a little over engineered for my needs and was only increasing the chance for user error. Instead I used the same slug for the custom post type as the page it was linking to and using that for each link.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Saving and retrieving data from wp_dropdown_pages metabox’ is closed to new replies.