WordPress.org

Ready to get started?Download WordPress

Forums

WP User Frontend
Custom Taxonomies (36 posts)

  1. lorenzoporretti
    Member
    Posted 2 years ago #

    Hi Tareq I need help, regarding the custom fields I've solved the problem by changing the prefix of the custom field.
    But now I want to integrate custom taxonomies, my custom taxonomies already are created from the function.php of templae and are read from the same template and print. How can I do to insert a field input for custom taxonomies in-form? I can get a good donation or if you want you tell me how much it costs! thank you!

  2. rolf deck
    Member
    Posted 2 years ago #

    Hi!

    I was about to ask the same (I guess..)...

    I am using this to output the new article form for a custom post type[wpuf_addpost post_type="listing"] but it grabs the default categories only and is not listing my 'listing_category'...

    How ca I do this?

    Thanks!

  3. lorenzoporretti
    Member
    Posted 2 years ago #

    Hi Rolf, this is the loop for the post type:

    $args = array( 'post_type' => 'listing', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    	the_title();
    	echo '<div class="entry-content">';
    	the_content();
    	echo '</div>';
    endwhile;

    (But your is an a different question)

  4. rolf deck
    Member
    Posted 2 years ago #

    Thanks for share that piece of code but I was thinking that you want t integrate custom tax along whit this shortcode [wpuf_addpost post_type="listing"]...

  5. lorenzoporretti
    Member
    Posted 2 years ago #

    I don't understand your problem... (I do not speak English very well)
    (listing is not a category)

  6. rolf deck
    Member
    Posted 2 years ago #

    Thanks for trying help-me!

    "listing" in my wordpress installation is a custom post type and I want to display custom categories from that custom post type when I use this shortcode: [wpuf_addpost post_type="listing"]

    My custom taxonomies are "listing_category".

    When I use [wpuf_addpost post_type="listing"], the only categories listed on a dropdown box are the default categories from wordpress...

  7. lorenzoporretti
    Member
    Posted 2 years ago #

    ok, you can create a custom template for this! as
    listing-category.php
    the code:

    <?php
    /*
    Template Name: Listing Category
    */
    ?>
    <?php get_header(); ?>
    $args = array( 'post_type' => 'listing', 'cat=3', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    	the_title();
    	echo '<div class="entry-content">';
    	the_content();
    	echo '</div>';
    endwhile;
    <?php get_footer(); ?>

    'cat=3', is the category number to display.

  8. rolf deck
    Member
    Posted 2 years ago #

    Hi lorenzoporretti!

    Thanks, man! But what I need is a way to the plugin call the categories for custom post_types instead of calling the default wordpress categories.

    My problem is:

    I have a custom post_type with custom categories

    eg: listing is a custom post-type that haves a main category which is products...

    When I place this code [wpuf_addpost post_type="listing"] on a page to those entries listed on the custom post_type "listing", the plugin only calls the categories from my blog and not the categories of this custom post_type...

    Do know if there is a way to do it?

    Thanks!

  9. MarceFX
    Member
    Posted 2 years ago #

    Hi,

    Any news on this? I'm desperate trying to get my custom taxonomies working :-/

    Thank you

  10. rolf deck
    Member
    Posted 2 years ago #

    I have no news on this... sorry! Also posted this thread but got no replies...

  11. lorenzoporretti
    Member
    Posted 2 years ago #

    I find this:
    http://tareq.wedevs.com/2012/04/how-to-extend-wp-user-frontend/ (the author of plugin)
    and this:
    http://shibashake.com/wordpress-theme/add-term-or-taxonomy-meta-data
    ... but I was not able to understand much about ...

  12. J Grandin
    Member
    Posted 1 year ago #

    Dear all,

    I harshly found a solution to edit custom taxonomies panel in front end pages using WP User Frontend.

    Please check link above posted by lorenzo to see plugin extensions functions kindly suggested by Tareq, and look at the chapter 'Processing the input'. The problem came when processing the data. Tareq example uses update_post_meta() but it only work for custom fields, not for custom taxonomies, that use wp_set_object_terms(). So you need to use this last function to save custom taxonomies in your front end form.

    First, create in functions.php your custom taxonomy input panel in a new function named for example wpufe_taxonomy(). I recommend this tutorial: WordPress Custom Taxonomy Input Panels by ShibaShake .
    Don't use metaboxes functions & CSS as they only work in back end of course. Call your input panel in the front end add/edit page using Tareq extension functions.

    Then to save the data, you have to write this
    add_action('save_post', 'save_taxonomy_data');
    then create a save_taxonomy_data() function like suggested in the above tutorial.

    Example of a saving function for a select list of custom taxonomy items:

    if (function_exists('wpufe_taxonomy')) {
        add_action('save_post', 'save_taxonomy_data');
    }
    
    function save_taxonomy_data($post_id) {
    // verify this came from our screen and with proper authorization.
    
     	if ( !wp_verify_nonce( $_POST['taxonomy_noncename'], __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
      	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        	return $post_id;
    
      	// Check permissions
      	if ( 'post' == $_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 == 'post') {
               $select_name = $_POST['select_name'];
    	   wp_set_object_terms( $post_id, $select_name, 'taxonomy_name' );
            }
    }

    Important note: using the following saving function suggested by Tareq will not work for some reason I can't figure.

    function wpufe_add_taxonomy() {
        add_action('save_post', 'save_taxonomy_data');
    }
    add_action( 'wpuf_add_post_after_insert', 'wpufe_add_taxonomy' );

    Finally don't forget to change select_name by the name attribute of your select list, in order to allow data processing.

    Again thanks Tareq for your fantastic work on this plugin.

  13. gerencia@enjambregroup.com
    Member
    Posted 1 year ago #

    Hi J Grandin, im a little confuse with all this i have a taxonomy created with Taxonomy Manager (plugin), the only thing that i need is how to add the select to the add / edit post and how to save it, could you give me a hand with this?

  14. J Grandin
    Member
    Posted 1 year ago #

    Dear Gerencia,

    1. To add a select to the add/edit post:
    Follow instructions from ShibaShake tutorial and add your custom taxonomy panel in your theme's functions.php file.

    It may look like this:

    /**
     * Add a select dropdown for a custom taxonomy
     * in WP User Frontend add/edit frontend form
     *
     * @uses wpuf_add_post_form_after_description action hook
     *
     * @param string $post_type the post type of the post add screen
     * @param object|null $post the post object
     */
    
    function wpufe_add_taxonomy_select( $post_type, $post = null) {
        ?>
    
        <label for="taxonomy_name">
                Taxonomy <span class="required">*</span>
        </label>
        <?php 
    
        // Add security nonce check
        wp_nonce_field( __FILE__, 'taxonomy_noncename' );
    
        // Get all taxonomy terms
        $taxonomies = get_terms('taxonomy_name', 'hide_empty=0');
        // You can also use - see below:
        // $taxonomies = wp_get_object_terms( $post->ID, 'taxonomy_name', array('fields' => 'ids') );
    
        ?>
    
        <?php
    
        // Here is another way to create your dropdown select with WordPress:
        // wp_dropdown_categories('taxonomy=taxonomy_name&hide_empty=0&orderby=name&name=select_name&show_option_none=Select Taxonomy&selected='.$taxonomies[0]);
    
        ?> 
    
        <select name='select_name' id='select_name' class="requiredField">
        <option value='' <?php if (!count($taxonomies)) echo "selected='selected'";?>>Select an item</option>
        <?php
    	foreach ($taxonomies as $taxonomy) {
                    $selected = (has_term($taxonomy->slug, 'taxonomy_name', $post->ID)) ? ' selected="selected" ' : '';
    		echo "<option value='" . $taxonomy->name . "' " . $selected . ">" . $taxonomy->name . "</option>\n";
    	}
        ?>
        </select>    
    
        <?php
    }
    
    add_action( 'wpuf_add_post_form_after_description', 'wpufe_add_taxonomy_select', 10, 2 );

    You can change the final position of your select in the form by modifying the preformatted position (here: wpuf_add_post_form_after_description) by one of the others you can found in Tareq's tutorial.

    2. Then to save the select, add the suggested saving function I posted above after the wpufe_add_taxonomy_select function.

    3. Change 'select_name' and 'taxonomy_name' by the select/taxonomy names you chose everywhere you found them.

    Hope it will help you. These functions could probably be improved of course, since I am not a PHP wizard.

  15. Katalo
    Member
    Posted 1 year ago #

    Hello!
    I´m also trying to add my custom taxonomies but I think I have tried everything, without any result. Grandin, dit the above code work for you, and how did it work? I tried but it doesnt seem to work.
    My problem is that i want to add my taxonomy like the regular category-fields. I dont know if thats what the above code does.
    If someone have a solution, any one, Im all ears!

  16. Katalo
    Member
    Posted 1 year ago #

    I´m sorry, it works great! The taxonomy select is there and everything. But it doesnt get saved. Any ideas?

  17. feshin
    Member
    Posted 1 year ago #

    I'm having the same problem - the taxonomy displays in the drop-down perfectly, but I can't figure out how to get it to save.

    I've tried the above code (thanks, J Grandin), but still no luck.

  18. feshin
    Member
    Posted 1 year ago #

    Ok - follow up with a possible solution. In my case I'm using a custom post type and the save taxonomy function is specific to a normal post.

    In the save function (save_taxonomy_data) look for this line:

    if ($post->post_type == 'post') {

    Change the post_type to your custom post type. I also had to remove the if statement around:

    add_action('save_post', 'save_taxonomy_data');
  19. J Grandin
    Member
    Posted 1 year ago #

    Dear Katalo, Dear Feshin,

    I think I found where you are stuck with the saving process.

    If you use custom posts, have a look to the save function and check these lines:

    // Check permissions
      	if ( 'post' == $_POST['post_type'] ) {

    and

    if ($post->post_type == 'post') {
               $select_name = $_POST['select_name'];

    In both cases, you must replace 'post' by the name of the custom post you use. You found it Feshin :)

    Despite I already wrote something before about it, replace also 'select_name' by the name attribute you use for your select dropdown. Without that change the save function will not find your select.

    Please tell me if it has helped you.

  20. Mohit Kumar
    Member
    Posted 1 year ago #

    I had tried the above solution, but I am not able for the save function to work. Here is my code.

    function wpufe_add_taxonomy() {
        add_action('save_post', 'save_taxonomy_data');
    }
    add_action( 'wpuf_add_post_after_insert', 'wpufe_add_taxonomy' );
    
    function save_taxonomy_data($post_id) {
    // verify this came from our screen and with proper authorization.
    
     	if ( !wp_verify_nonce( $_POST['taxonomy_noncename'], __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
      	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        	return $post_id;
    
      	// Check permissions
      	if ( 'post' == $_POST['deal'] ) {
        	if ( !current_user_can( 'edit_page', $post_id ) )
          		return $post_id;
      	}
      	elseif ( 'post' == $_POST['property'] ) {
        	if ( !current_user_can( 'edit_page', $post_id ) )
          		return $post_id;
      	}
      	elseif ( 'post' == $_POST['business'] ) {
        	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 == 'deal') {
               $select_name = $_POST['deal_cat'];
    	   wp_set_object_terms( $post_id, $select_name, 'taxonomy_name' );
    	    $area_name = $_POST['area'];
    	   wp_set_object_terms( $post_id, $area_name, 'taxonomy_name' );
            }
            if ($post->post_type == 'property') {
               $select_name = $_POST['property_cat'];
    	   wp_set_object_terms( $post_id, $select_name, 'taxonomy_name' );
    	    $area_name = $_POST['area'];
    	   wp_set_object_terms( $post_id, $area_name, 'taxonomy_name' );
            }
            if ($post->post_type == 'business') {
               $select_name = $_POST['business_cat'];
    	   wp_set_object_terms( $post_id, $select_name, 'taxonomy_name' );
    	    $area_name = $_POST['area'];
    	   wp_set_object_terms( $post_id, $area_name, 'taxonomy_name' );
            }
    }
  21. J Grandin
    Member
    Posted 1 year ago #

    Dear Mohit, I hope I can help.

    First, you must not touch the "check permissions" part of the function; the code checks if the user has permissions to edit either post or page. Note that the custom posts you refer to are checking permissions for 'edit_page' which is incorrect since they are posts.

    This could be also a nonce problem, since you didn't change 'taxonomy_noncename' by another noncename of your own.

    Also, I didn't tried the function if you include several custom posts in it. I suppose that you tested it before with one custom post only?

    In order to avoid misunderstandings, here is the sequence of functions I used for a custom post named 'anecdote' with a custom taxonomy named 'anecdote-media':

    ///////////////   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 wpufe_anecdote_media( $post_type, $post = null) {
        ?>
    
        <label for="anecdote-media">
                Media <span class="required">*</span>
        </label>
    
        <?php 
    
        // Add security nonce check
        wp_nonce_field( __FILE__, 'nonce-anecdote-media' );
    
        // Get all media taxonomy terms
        $medias = get_terms('anecdote-media', 'hide_empty=0'); //$medias = wp_get_object_terms( $post->ID, 'anecdote-media', 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=anecdote-media&hide_empty=0&orderby=name&name=post_media&show_option_none=Select media&selected='.$medias[0]);
    
        ?> 
    
        <select name='post_media' id='post_media' class="requiredField">
        <option value='' <?php if (!count($medias)) echo "selected='selected'";?>>Select a media</option>
        <?php
    	foreach ($medias as $media) {
                    $selected = (has_term($media->slug, 'anecdote-media', $post->ID)) ? ' selected="selected" ' : '';
    		echo "<option value='" . $media->name . "' " . $selected . ">" . $media->name . "</option>\n";
    	}
        ?>
        </select>    
    
        </li>
        <?php
    }
    
    /**
     * The following action adds a form section
     * below the description field of WP User Frontend
     */
    
    add_action( 'wpuf_add_post_form_after_description', 'wpufe_anecdote_media', 10, 2 );
    
    /**
     * 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('wpufe_anecdote_media')) {
        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-anecdote-media'], __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 ( 'anecdote' == $_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 == 'anecdote') {
               $media = $_POST['post_media'];
    	   wp_set_object_terms( $post_id, $media, 'anecdote-media' );
            }
    }

    I recommend also that you check your sequence of functions with the tutorial 'WordPress Custom Taxonomy Input Panels by ShibaShake' linked in an above post.

    Finally, I encountered (as for others, as you probably noticed in the plugin forum) a permission issue depending on if you use WPUFE with a contributor/author/editor level or with an administrator level.

  22. ninofrenn
    Member
    Posted 1 year ago #

    feeling stuck about this one, they don't save
    here is mine

    /**
     * Add a select dropdown for a custom taxonomy
     * in WP User Frontend add/edit frontend form
     *
     * @uses wpuf_add_post_form_after_description action hook
     *
     * @param string $post_type the post type of the post add screen
     * @param object|null $post the post object
     */
    
    function wpufe_add_taxonomy_select( $post_type, $post = null) {
        ?>
    
        <label for="about">
                About <span class="required">*</span>
        </label>
    
        <?php 
    
        // Add security nonce check
        wp_nonce_field( __FILE__, 'taxonomy_noncename' );
    
        // Get all taxonomy terms
        $taxonomies = get_terms('about', 'hide_empty=0');
        // You can also use - see below:
        // $taxonomies = wp_get_object_terms( $post->ID, 'about', array('fields' => 'ids') );
    
        ?>
    
        <?php
    
        // Here is another way to create your dropdown select with WordPress:
        // wp_dropdown_categories('taxonomy=about&hide_empty=0&orderby=name&name=about&show_option_none=Select Taxonomy&selected='.$taxonomies[0]);
    
        ?> 
    
        <select name='about' id='about' class="requiredField">
        <option value='' <?php if (!count($taxonomies)) echo "selected='selected'";?>>Select an item</option>
        <?php
    	foreach ($taxonomies as $taxonomy) {
                    $selected = (has_term($taxonomy->slug, 'about', $post->ID)) ? ' selected="selected" ' : '';
    		echo "<option value='" . $taxonomy->name . "' " . $selected . ">" . $taxonomy->name . "</option>\n";
    	}
        ?>
        </select>    
    
        <?php
    }
    
    /**
     * Input the media data after submitting
     */
    
    if (function_exists('wpufe_add_taxonomy_select')) {
        add_action('save_post', 'save_taxonomy_data');
    }
    
    add_action( 'wpuf_add_post_form_after_description', 'wpufe_add_taxonomy_select', 10, 2 );
    
    function save_taxonomy_data($post_id) {
    // verify this came from our screen and with proper authorization.
    
     	if ( !wp_verify_nonce( $_POST['taxonomy_noncename'], __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
      	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        	return $post_id;
    
      	// Check permissions
      	if ( 'questions' == $_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 == 'questions') {
               $select_name = $_POST['about'];
    	   wp_set_object_terms( $post_id, $select_name, 'questions' );
            }
    }

    any help??

  23. J Grandin
    Member
    Posted 1 year ago #

    In function wpufe_add_taxonomy_select, your taxonomy name is "about".
    In function save_taxonomy_data, your taxonomy name becomes "questions".
    Your should probably fix it :)

  24. ninofrenn
    Member
    Posted 1 year ago #

    what so careless!!!

    I love you Jonathan, it's working properly now!

  25. J Grandin
    Member
    Posted 1 year ago #

    You're welcome, ninofrenn.

    Also I noticed that your custom post, your custom taxonomy (and your select?) use exactly the same name. To avoid possible mistakes when reading and checking the code, maybe you could use different names?

    All the code could be simplified I think by replacing the taxonomy name / custom post name / select name by strings at the top of the code. This way you will not have to look for the locations where these names have to be set. But this idea will work as long as you don't use several dropdowns with different custom taxonomies, like I did. In this case different strings must be used for each select you want to add in the frontend form.

    Of course this is more a development for a next version of the plugin.

  26. ninofrenn
    Member
    Posted 1 year ago #

    @Jonathan
    Is the function above do the same process for "tags input" ?
    Then, I'm wondering just change the way in category option select with the input tags

    i.e maybe like this

    <label for="custom_tags">Title <span class="required">*</span></label>
    <input class="requiredField" value="" name="custom_tags" id="custom_tags" minlength="2" type="text">

  27. J Grandin
    Member
    Posted 1 year ago #

    @ninofrenn
    Have you tried it with tags since your last post ? Since tags are categories but non hierarchical, I think they can be set as custom taxonomies (have a look in the WP codex here).

    To found how to enable the tag interface in WPUFE, have a look at this tutorial (made by Shibashake, thanks to him again):

    To enable the tag meta-box interface, you will need to include the Javascript associated with it, in this case post.js.

    Since Tareq didn't include this tags interface for the 'normal' tags in WPUFE (you have to separate tags with commas in a normal text field), maybe that shouldn't work?

  28. tkhan
    Member
    Posted 1 year ago #

    Thanks to all the help in this thread I now have dropdown select boxes for my custom taxonomies on my add/edit post pages. I was wondering if anyone could give me pointers on how to display my taxonomy terms as checkboxes.

    Any help would be brilliant

  29. strangerrj
    Member
    Posted 1 year ago #

    Thanks wp user front end. This is an very good plug in. I do have the themes wallpress with different post templates. In wordpress under adjustments there's the option to Standard article post format collecting main. When I do adjust the individual post tempate there, it works in the back end but not in the front end ( wp user front end). Wp user front end will take the standart post format template.

    I do work in the word press and php and I'm happy about every helpfull hint.

    greetings

  30. startecommerce2
    Member
    Posted 1 year ago #

    Hi Guys,

    First of all I want to thank you all for this golden and great thread.

    I implemented something to show my Taxonomies in dropdown list in submit form.

    Now when the user select taxonomy, this taxonomy will be saved in variable as string (e.g. Blue)

    But how I can add this selected taxonomy with specific post_id?

    what is the function that sync the taxonomy to the current post id or to any post id?

    also can i do that if i have the taxonomy name/slug?

    Please advise

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic