• atburi

    (@aburi)


    Hi Everyone,

    I’m using the latest version of WPUF PRO to create my front-end forms using action hooks.

    I’ve been able to hook in a custom taxonomy multi-select dropdown based on the method outlined here by @j-grandin .

    The taxonomy dropdown shows up and saves properly, but on the “Edit Post” page the selected taxonomy terms do not show up as selected. It just shows the “Nothing selected” title, even though terms *are* selected.

    Could someone please take a look at my code below and check out the place where selected terms are supposed to get echoed as ‘selected=”selected”‘?

    Thanks,
    Alex

    ///////////////   MEDIA CUSTOM TAXONOMY    /////////////////////
    
    /**
     * First, create a form section for WP User Frontend
     * with the fields relative to the custom taxonomy
     * in a function 'wpufe_anecdote_media'
     */
    
    /**
     * Add media dropdown to the add anecdote area
     *
     * @uses wpuf_add_post_form_description action hook
     *
     * @param string $post_type the post type of the post add screen
     * @param object|null $post the post object
     */
    
    function wpuf_countries( $post_type, $post = null) { ?>
    
        <label for="anecdote-media">Countries</label>
    
        <?php
    	    // Add security nonce check
    	    wp_nonce_field( __FILE__, 'nonce-countries' );
    
    	    // Get all media taxonomy terms
    	    $countries = get_terms('countries', 'hide_empty=0'); //$countries = wp_get_object_terms( $post->ID, 'countries', array('fields' => 'ids') );
        ?>
    
        <li>
    
        <?php
    
        // You can also use the included dropdown generator function of wordpress, but I'd prefer to code the select myself in order to avoid possible problems
        //wp_dropdown_categories('taxonomy=countries&hide_empty=0&orderby=name&name=post_media&show_option_none=Select media&selected='.$countries[0]);
    
        ?> 
    
        <select name='post_media' id='post_media' multiple class="requiredField form-control selectpicker">
    	    <option value='' <?php if (!count($countries)) echo "selected='selected'";?>>Select a media</option>
    	    <?php
    			foreach ($countries as $country) {
    		        $selected = (has_term($country->slug, 'countries', $post->ID)) ? ' selected="selected" ' : '';
    				echo "<option value='" . $country->name . "' " . $selected . ">" . $country->name . "</option>\n";
    			}
    	    ?>
        </select>    
    
        </li>
        <?php
    }
    add_action( 'hook_countries', 'wpuf_countries', 10, 3 );
    
    /**
     * Validate existence of the media after anecdote creation/edit
     * If the select is empty, it returns an error message
     *
     * @uses 'wpuf_add_post_validation' filter
     *
     * @param array $errors errors array
     * @return array errors array
     */
    
    function wpufe_media_validation( $errors ) {
        if( $_POST['post_media'] == '' ) {
            $errors[] = 'Please select a media for your anecdote';
        }
    
        return $errors;
    }
    add_filter( 'wpuf_add_post_validation', 'wpufe_media_validation' );
    add_filter( 'wpuf_edit_post_validation', 'wpufe_media_validation' );
    
    /**
     * Input the media data after submitting
     */
    
    if (function_exists('wpuf_countries')) {
        add_action('save_post', 'save_media_data');
    }
    
    /**
     * Save the media taxonomy data
     */
    
    function save_media_data($post_id) {
    // verify this came from our screen and with proper authorization.
    
     	if ( !wp_verify_nonce( $_POST['nonce-countries'], __FILE__ )) {
        	return $post_id;
      	}
    
      	// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
            // maybe you'll find this unnecessary since there is no possible autosave in the frontend
      	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        	return $post_id;
    
      	// Check permissions
      	if ( 'request' == $_POST['post_type'] ) {
        	if ( !current_user_can( 'edit_page', $post_id ) )
                return $post_id;
      	} else {
                if ( !current_user_can( 'edit_post', $post_id ) )
                return $post_id;
      	}
    
      	// OK, we're authenticated: we need to find and save the data
    
      	$post = get_post($post_id);
    	if ($post->post_type == 'request') {
    		$country = $_POST['post_media'];
    		wp_set_object_terms( $post_id, $country, 'countries' );
    	}
    }

    https://www.ads-software.com/plugins/wp-user-frontend/

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Using action hook to add custom taxonomy to form.’ is closed to new replies.