ioannisM-2
Support threads created in the last 30 days: 0
Favorite Forum Topics
Status | Topic | Supporter | Voices | Posts | Freshness |
---|---|---|---|---|---|
Favorite Posts By Logged In User (Add To / Remove not working properly)
1
2
Started by: Team DNK
in: Toolset Professional Support
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 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 |
3 | 19 | 7 years ago | ||
How can I add complex CSS3 to CRED-form-fields?
Started by: philipG-3 in: Toolset Professional Support |
3 | 7 | 7 years, 1 month ago |
Forum Topics Created
Status | Topic | Supporter | Voices | Posts | Freshness |
---|---|---|---|---|---|
Toolset Custom Fields in Php not Working
Started by: ioannisM-2 in: Toolset Professional Support |
2 | 7 | 7 years ago | ||
Types Cred Compatibility with Transliteration Plugin
Started by: ioannisM-2 in: Toolset Professional Support |
2 | 3 | 7 years, 1 month ago |