February 24, 2014 at 12:57 am 15770 |
Sk | 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' );
}
}
|
February 26, 2014 at 10:58 pm 15869 |
Alex
| Any assistance with this would be a big help if anyone has time 🙂
|
March 6, 2014 at 2:51 am 16223 |
Alex
| I know it’s a complicated question, but anyone any help with this from WPUF? Would appreciate it 🙂
|
March 6, 2014 at 4:11 pm 16240 |
Tareq Hasan
| Hello Alex, sorry I didn’t notice this thread.
If you are using action hook, I am not seeing any action that actually saves the taxonomy into the post. You used save_post hook and it resides in the admin area, not in frontend. Use wpuf_add_post_after_insert hook to save it. Check the example here http://docs.wedevs.com/using-action-hook-field/
|
March 7, 2014 at 1:36 am 16258 |
Alex
| Hi Tareq,
No worries about the delay. Thanks for taking the time to help us.
I do have wpuf_add_post_after_insert but didn’t include it in my snippet. The field is saving successfully, ie. it updates the field with the new value, but my problem is getting the current values to show as selected on my post edit page.
Something is wrong with the function at the point where it triggers selected values…
|
June 18, 2014 at 12:43 am 22081 |
Mark
| I am having this problem as well. The taxonomies display ok on the front end form however when I save the item the taxonomies do not save. Other fields save ok, just not the custom taxonomy fields. The taxonomies are created using CPT-onomies: Using Custom Post Types as Taxonomies
|
June 18, 2014 at 1:31 pm 22113 |
Oritro Ahmed
| @Mark,
Its really hard for us to track a old thread with new replay. Out Best Suggestion is, Take a solve from a old question, if didn’t work, open a new thread.
|
June 30, 2014 at 1:34 pm 22607 |
Sk
| Hello,
this maybe happens because the post type dosen’t support custom taxonomy that you added.
Could you please provide us your site access to check the problem and it’s reason.
Note: Please make your reply private when the information is confidential.
thank you.
|