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 6 Jahren, 6 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 24 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von Eric vor 6 Jahren, 5 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#879942

Hi,

I'm creating a membership website for a client. Membership is free, just requires registration. Upon registration, the new user is given a custom role and access to hidden content controlled by Access. Members are also allowed to choose from a list of service providers "agents" which is simply a custom post type with custom fields containing agent info.

I need to:
1. Allow members to choose an agent from a dropdown list (this could be handled on the member's profile CRED page).
2. Restrict the member to choosing only 1 agent (important).
3. Display the agent's name on the member's account page (this would just be the title of the cpt). This is in case the member forgets which agent they're working with...
4. Make sure an agent (just a post, remember) can be chosen by unlimited members.

This sorta sounds like designating a post as a "favorite," except that members can only have 1 favorite agent post.

What's the best way to go about this?

Thanks,
Eric

I forgot to mention that I already have the basics of the membership site created, as well as the agents custom post type...just need help with the agent-post/member association.

#880992

Hi, it sounds like this could be accomplished with a User custom field that stores the ID of an Agent. Can you tell me more about Members and Agents?
- Are there custom post types and custom posts that represent Members and Agents, or are they simply custom roles assigned to the standard User in WordPress?
- If they are post types, do you have any post relationship set up between the post types?
- If they are custom roles only, do you want your Members to log in to the wp-admin to modify their favorite, or do you want to set up a form on the front-end of the site where they can manage this?

#881740

Hi,

"Member" is a custom role assigned to a user when they register from a specific Gravity Form. Members will be accessing hidden content and doing other things on the site, including managing their own profile via CRED forms.

"Agent" is a custom post type with a custom field group. The agent posts are essentially bios and contact info. Pretty basic. Agents won't be users on the site.

I want to prevent Members from seeing/accessing wp-admin. (I know there are posts about this, and I'll handle that separately). I want Members to manage their account via CRED forms, and as part of that, be able to select 1 Agent as their desired agent. So...an Agent post/bio can be the choice of many Members, but a Member can only choose one Agent.

Does that clarify things a bit?

I can give you login credentials to the dev site if you like.

Thanks so much for your help!
Eric

#883408

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Eric,

As Christian is currently unavailable I will be handling this ticket for you.

I thought of a method that could work and let me know what you think.

I was thinking of creating a child post type for the agent CPT. Now using our CRED form we can add the child CPT creation to the frontend and then allow the user to select an Agent from there.

Finally on their profile we can display the parent name (Agent) on their respective profile.

Please let me know what you think of this method.

Thanks,
Shane

#886855

Hi Shane,

What would the Agent CPT be a child of? "Member" is a custom role, not a post type. Honestly, I'm not that familiar with child post types...

Thanks,
Eric

#889444

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Eric,

So what you want to do is for the user to favourite another user of another role?

#889515

Hi Shane,

No, "Agent" is not a role or a user. It's a cpt containing simple biographical information about the agent and a featured image of the agent.

"Member" is a custom role. I would like the Members to be able to choose an Agent as they're "favorite." Members can only choose one agent. But, an agent can be the favorite of many "Members."

#889521

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Eric,

Right so essentially since these are rolls then a user isn't able to favourite a roll.
Thanks,
Shane

#889524

Hi Shane,

I'm not trying to favorite a role. I'm trying to favorite a POST from a custom post type.

Again:
Member = Role
Agent = Custom Post Type

I would like a Member (user with role) to be able to "favorite" one POST from the custom post type called Agent.

#890463

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Eric,

Right I understand this but i've explained how you can achieve this with the method above.
https://toolset.com/forums/topic/method-for-user-to-choose-post-as-favorite/#post-883408

You can allow the Members to create a Child post for Agents. This will use our CRED forms to allow the user to select an agent from the dropdown.

However instead of favouriting the actual post itself they will create a child post and select an agent as to be the parent of that post, this way favouriting the agent they want.

Then you can display this on the user's profile.

Thanks,
Shane

#890725

I'm not following you... Are you suggesting that Members create the Agent posts? Members are not allowed to create posts of any kind in this system. All Agent posts (which are essentially bios) are created by me or by my client in the WP backend. Members will only be allowed to favorite an agent post that has already been created, presuming we can solve this...

#890755

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Eric,

Ok since this is a limitation as they are not able to create posts.

However if we use the favourite post plugin then the user can favourite multiple Agents.

I wouldn't be able to assist with a custom solution for the user to favourite a post as custom coding is out of our forum scope.

The best non-custom coding method is to use a child post or what you can do is when the user is creating their profile you can provide them with a list of the agents that they can select from on the form.

Please let me know what you think of this idea.

Thanks,
Shane

#890854

Choosing an agent when the member creates their profile is what I'd like to do. Hopefully they can also change the agent if they edit their profile with a CRED edit form. Can you point me to the documentation for how'd I'd use child posts for this, and how I'd connect the select menu to the Member CRED form?

#893150

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Eric,

Since its when they are creating their profiles then this would be much easier.

What you can do is to Create a custom field on your Members Role using our Types plugin.

Next when you are creating the form you will need to replace the auto generated form field for that Custom Field with a generic one like the one below.

[cred_generic_field field='Tests' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name='test'] ]
}
[/cred_generic_field]

Now for the options you see that I've used a view to generate the options.

Finally you will need to create this view and setup the format like this.

<!-- 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 -->

After which you will need to add this code to your functions.php file to ensure that the view output is a clean one.

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
  
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '375' ) {
        $start = strpos( $out, '<!-- wpv--noloop-start -->' );
        if ( 
            $start !== false
            && strrpos( $out, '<!-- wpv-noloop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-noloop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-noloop-end -->' );
            $out = substr( $out, 0, $end );
        }
    }
    return $out;
}

Just replace the 375 with the ID of your view and this should all work.

Please let me know if this helps.

Thanks,
Shane

#901318

OK, this looks like a fair amount of fuss for what seems to be a simple concept.

Also, you wrote, "What you can do is to Create a custom field on your Members Role using our Types plugin," but didn't provide further explanation as to what kind of field or how it works. I'd like to learn and understand the relationships here rather than just cut and paste code, so explanations are helpful, and may be useful to someone else down the road too.

Christian Cox wrote, "it sounds like this could be accomplished with a User custom field that stores the ID of an Agent."

If so, how can I accomplish this?

Maybe Christian Cox can jump back in here since that's what he proposed initially... 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? The only reason I used a custom post type for Agent (instead of a custom role) is because I thought it'd be simpler, especially since Agents don't need user accounts on this system.

To recap, a Member (custom role) needs to be able to select 1 Agent (an Agent can be either a cpt or custom role, whichever is easier. I don't care which at this point). An Agent can be the "favorite" of many Members, but a Member can only have 1 Agent.

Thanks,
Eric