Problem: I would like to allow my Users to "favorite" posts.
Solution:
- Add a number custom field to the User's profile. This number will store the favorite post ID.
- Create a Form that edits the current User's profile
- Remove the number field from the Form and replace it with a generic select field that stores the value of the favorite post ID:
[cred_generic_field field='your-lockbox-agent-id' type='select' class='' urlparam=''] { "required":0, "validate_format":0, "default":[[types usermeta="your-lockbox-agent-id" output="raw" user_current="true"][/types]], "options":[[wpv-view name='list-of-lockbox-agents-view-for-cred-forms']] } [/cred_generic_field]
- Create a View of posts that will provide the options for the generic select field. Use this loop format:
<wpv-loop> [wpv-item index=1] {"value":"[wpv-post-id]","label":"[wpv-post-title]"} [wpv-item index=other] ,{"value":"[wpv-post-id]","label":"[wpv-post-title]"} </wpv-loop>
- Add the following custom filter code to your child theme's functions.php file:
add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 ); function prefix_clean_view_output( $out, $id ) { $ids = array( 12345, 67890 ); if ( in_array( $id, $ids )) { $start = strpos( $out, '<!-- wpv-loop-start -->' ); if ( $start !== false && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false ) { $start = $start + strlen( '<!-- wpv-loop-start -->' ); $out = substr( $out , $start ); $end = strrpos( $out, '<!-- wpv-loop-end -->' ); $out = substr( $out, 0, $end ); } else { $start = strpos( $out, '>' ); if ( $start !== false) { $out = substr( $out, $start + 1 ); $end = strpos( $out, '<' ); $out = trim(substr( $out, 0, $end )); } } } return $out; }
- Replace 12345, 67890 with the View's ID, or a comma-separated list of View IDs if you want to apply this filter to more than one View.
- Add the following custom code that will capture the selected post ID and save it in the User's favorite post field:
add_action('cred_save_data', 'save_new_lockbox_agent',10,2); function save_new_lockbox_agent($user_id, $form_data) { $ids = array( 38, 78 ); if( in_array( $form_data['id'], $ids )){ $agent = $_POST['your-lockbox-agent-id']; update_user_meta($user_id, 'wpcf-your-lockbox-agent-id', $agent ); } }
Relevant Documentation:
https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms/
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 24 replies, has 3 voices.
Last updated by 6 years, 4 months ago.
Assisted by: Christian Cox.