Problem: I would like Users to be able to choose certain posts as favorites. They should also be able to remove them from favorites.
Solution:
Create a repeating numeric field on your post type called "favorites". This field will be updated to add the current User's ID when they select a favorite. When they unfavorite a post, this field will be updated to remove their ID.
Create a CRED form that allows you to modify the posts that can be favorited. Remove all the content from the CRED form and replace it with the following code:
[credform class='cred-form cred-keep-original'] [wpv-conditional if="( [toolset_is_favorite id='[wpv-post-id]'] eq '1' )"] [cred_generic_field field='remove-favorite' type='hidden' class=''] { "required":0, "validate_format":0, "default":"1" } [/cred_generic_field] [/wpv-conditional] [cred_field field='form_submit' value='Favorite' urlparam='' class='x-btn x-btn-global mam'] [/credform]
Add the following JavaScript to your CRED form's JS panel:
jQuery(window).bind("load", function() { jQuery( "form[id^='cred_form_368']").find("input[type='submit']").val(jQuery( "input[name='remove-favorite']").length ? "Remove from Favorites" : "Add to Favorites"); } )
This code will toggle between showing "Add to Favorites" and "Remove from Favorites" as the button name.
Add the following PHP to your child theme's functions.php file:
add_action('cred_save_data_368', 'sda_user_favorites',10,2); function sda_user_favorites($post_id, $form_data) { if( isset($_REQUEST['remove-favorite']) ) { delete_post_meta($post_id, 'wpcf-favorites', get_current_user_id()); } else { add_post_meta($post_id, 'wpcf-favorites', get_current_user_id(), false); } } /* Favorites shotcode for use as a conditional */ add_shortcode( 'toolset_is_favorite', 'toolset_is_favorite_func'); function toolset_is_favorite_func($atts) { global $wpdb; $post_id = $atts['id']; $user_id = get_current_user_id(); $favorites = $wpdb->get_results( "SELECT * FROM wp_postmeta WHERE meta_key = 'wpcf-favorites' AND post_id = $post_id AND meta_value = $user_id LIMIT 1"); return sizeof($favorites) ? 1 : 0; }
Be sure to add the toolset_is_favorite shortcode in Toolset > Settings > Frontend content > 3rd party shortcode arguments.
Place the CRED form in a View of posts, or in a Content Template for these posts.
Relevant Documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 18 replies, has 3 voices.
Last updated by 6 years, 11 months ago.
Assisted by: Christian Cox.