WordPress.org

Ready to get started?Download WordPress

Forums

Make a metabox that let's users select a link category (3 posts)

  1. nadine00
    Member
    Posted 1 year ago #

    Hello!

    I actually have this issue written up on stack exchange, but figured I would cross post to here as well.

    Basically I'm trying to make a meta box that will allow users to choose from a list of link categories and associate them with a post. I'm having some issues trying to figure out how to do this tho.

    Any ideas? this is my updated in progress code (note i have not added a save function yet):

    function tf_book_purchase (){
    
    			global $post;
    			               $custom  = get_post_custom($post->ID);
    			               $link    = $custom["link"][0];
    
    			               echo '<div class="admin_meta">';
    
    			               $myterms = get_terms("link_category");
    
    			               echo $myterms;
    
    			               $myterms = array();
    
    			               echo $myterms;
    
    			               echo '<select name="link">';
    			               echo '<option class="audio_select">Select a Link Category</option>';
    			               foreach($myterms as $term){
    			                   $term_slug=$term->slug;
    			                   $term_name =$term->name;
    			                   $term_id =$term->term_id;
    
    			                   if($link == $myterms[]= $term->term_id){
    			                      echo '<option value="'.$myterms[]= $term->term_id.'" selected="true">'.$myterms[]= $term->term_id.'</option>';
    			                         }else{
    			                      echo '<option value="'.$myterms[]= $term->term_id.'">'.$myterms[]= $term->term_id.'</option>';
    			                         }
    
    			               }
    
    			               echo '</select><br /></div>';
    			               echo '<p>Selecting an audio file from the above list to attach to this post.</p>';
    
    	 }
    
    		add_action ('save_post', 'save_tf_book_purchase');
    
    		function save_tf_book_purchase() {
    
    		}

    And here's a link to the original question:

    http://wordpress.stackexchange.com/questions/65122/how-to-make-a-metabox-that-contains-link-categories

  2. nadine00
    Member
    Posted 1 year ago #

    Have this mostly solved, just looking for a way to visually keep the selected option as selected...anyone?:

    function tf_book_purchase (){
        global $post;
                $custom  = get_post_custom($post->ID);
                $link    = $custom["link"][0];
                $selected = isset( $custom['link'] ) ? esc_attr( $custom['link'][0] ) :'';
    
                echo '<div class="link_header">';
    
                $myterms = get_terms("link_category");
    
                echo '<select name="link" id="link">';
                echo '<option class="buy_books">Select A Link Category</option>';
                foreach($myterms as $term){
                            $term_slug=$term->slug;
                            $term_name =$term->name;
                            $term_id =$term->term_id;
    
               ?>   <option value="<?php echo $term_id;?>" <?php selected( $selected, ".$term_id." ); ?>><?php echo $term_name;?></option>
    
                <?php   }
    
                 echo '</select><br /></div>';
                 echo '<p>Please select a set of purchase links for this book.</p>';
    
         }
    
        add_action ('save_post', 'save_tf_book_purchase');
    
        function save_tf_book_purchase() {
    
        global $post;
    
        // make sure we're on a supported post type
            if ( $_POST['post_type'] != 'books' ) return;  
    
            // verify this came from our screen and with proper authorization.
            if ( !wp_verify_nonce( $_POST['book_nonce_name'], 'book-nonce' )) return;
    
            // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
            if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
    
            // Check permissions
            if ( 'page' == $_POST['post_type'] ) {
                if ( !current_user_can( 'edit_page', $post_id ) ) return;
            } else {
                if ( !current_user_can( 'edit_post', $post_id ) ) return;
            }
    
        //if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ return $post->ID; }
    
        update_post_meta( $post->ID, 'link', esc_attr( $_POST['link'] ));
    
        }
  3. nadine00
    Member
    Posted 1 year ago #

    Anyone? Bueller.....? Bueller...?

Topic Closed

This topic has been closed to new replies.

About this Topic