[Plugin: WP User Frontend] Custom Taxonomies
-
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!
-
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.
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');
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.
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' ); } }
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.
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??
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 ??what so careless!!!
I love you Jonathan, it’s working properly now!
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.
@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 tagsi.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”>@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?
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
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
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
@tkhan
I needed checkboxes as well and get them working just by modifying Jonathan Grandin’s code a bit.<?php foreach ($medias as $media) { $selected = (has_term($media->slug, 'practice-areas', $post->ID)) ? ' checked="yes" ' : ''; echo '<input type="checkbox" name="post_media[]" value="' . $media->name . '" ' . $selected . '> ' . $media->name . '<br />'; } ?>
Thanks for all of the info everyone has submitted here. It’s been a huge help.
- The topic ‘[Plugin: WP User Frontend] Custom Taxonomies’ is closed to new replies.