Skip Navigation

[Gelöst] Method for user to choose post as „favorite“

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 support ticket is created vor 5 Jahre, 10 Monate. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 Antworten, has 3 Stimmen.

Last updated by Eric vor 5 Jahre, 9 Monate.

Assisted by: Christian Cox.

Author
Artikel
#901697

Would it be better to create an Agent role with custom user fields for their bio info instead of using an Agent custom post type?
No it won't be easier or better, because there is no built-in way to associate one User with another User using custom fields. Either approach will require a generic field, with options automated by a View. Using a custom post type is better than a User role because better search and filter options are provided in post-type Views as opposed to User-type Views.

The method I had in mind is explained in detail by Shane here:
https://toolset.com/forums/topic/method-for-user-to-choose-post-as-favorite/#post-901318

It is a bit complex because there is currently no built-in way to select a CPT post and save that selected post to a User's profile. So instead, the best way to accomplish this is by saving a post ID (the Agent post ID) in the Member User's profile using a "Number" type custom field. However since your Member Users have no idea which numeric ID is associated with each Agent, you must allow your Users select by Agent name instead of ID in the Member Profile CRED form. Then Members can select the appropriate Agent when editing their profile. Shane described how this type of select field can be accomplished by replacing the automatically-generated Agent ID field in the CRED form builder with a generic select field, with options defined by a custom View.

#910900

OK, I've tried to muddle through this, but something's not quite right. I used the custom code provided and I edited for my site, but the select menu doesn't get displayed on the Member's post or edit CRED forms. Is it possible that there's a syntax error here:

[cred_generic_field field='your-agent' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name='list-of-agents-view-for-cred-forms'] ]
}
[/cred_generic_field]

BTW, if I drop the view into the CRED form (without using the cred_generic_field container) I get an array like:

{"value":"54","label":"Kelly"} , {"value":"58","label":"Joyce"} , {"value":"56","label":"Joe"} , {"value":"60","label":"Ed"} , {"value":"57","label":"Donna"} , {"value":"59","label":"Chuck"}

Any ideas?

Thanks,
Eric

#911510

The only thing I can think of is maybe the white space is throwing things off. Please adjust the Loop Output to use this more condensed version with no spaces around the commas and tags:

<!-- wpv-loop-start --><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><!-- wpv-loop-end -->

If this doesn't work, I'll be glad to take a closer look.

#912007

Hi Christian,

Thanks, but unfortunately that didn't solve it. Do you want access to the site?

#912412

Yes, please provide login credentials here and I will take a look. Please also let me know where I can find the Form on the front-end of the site to test things out.

#912609

It says the password is incorrect for this User, can you check the credentials again? I'll activate private fields so you can provide updates if necessary.

#912659

Okay thanks, login worked this time. I made some modifications in the functions.php theme file, so please be sure to download that file to your local repository so you have the latest version. It looks like there were some problems in the filter code, so I fixed those and now it looks like the custom select field is appearing in the form. Can you confirm?

#913079

Yes, I see the custom select field. When I make a selection and save the CRED form, however, my choice doesn't appear to get saved.

#913155

Okay thanks, I have added some custom PHP code that will capture the selection in the generic field, and store it in the post's custom field. I updated the "Create" form to store the proper value, and I updated the "Edit" form to both display and store the proper value. You'll notice that I created a new custom field, "your-lockbox-agent-id", that stores a number. The reason this field is required is that there is no way to create a "select" field that holds dynamic options. In other words, you would have to manually create all the options in wp-admin, which kind of defeats the purpose of using a View to show all the options. Instead, you'll just store the Agent's ID, because that will be enough for you to use to display any information you need about the Agent. Please download functions.php again to receive the latest updates.

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 );	
	}	
}

In the "edit" form:

[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]

In the "create" form:

[cred_generic_field field='your-lockbox-agent-id' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[[wpv-view name='list-of-lockbox-agents-view-for-cred-forms']]
}
[/cred_generic_field]

I also updated the Beaver Builder design on the My Account page. It looks like you want to display the related Agent's name, so I used the wpv-post-title shortcode and the wpv-user shortcode together:

[wpv-post-title id="[types usermeta='your-lockbox-agent-id' user_current='true' output='raw'][/types]"]

This lets you display the title of the post, where the ID matches the current User's "your-lockbox-agent-id" custom field value.

Let me know if you have questions about this approach, or if it's not working like you expect.

#915527

Wow, I can't thank you enough. That was above and beyond support for Toolset. I really appreciate it!

Thanks again,
Eric

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.