Skip Navigation

[Resolved] Let the author decide whether a post is publicly accessible or only for members

This support ticket is created 5 years ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Author
Posts
#1387515

Hi there

We have developed a platform for members. We want to give our members the opportunity to choose whether their profiles (CPT) are public or can only be viewed by other members.

This concerns on the one hand the profiles themselves. The users should be able to activate a checkbox to make their profile publicly accessible. If the checkbox is not active, the profile can only be viewed by members. My question: does this work with a checkbox and what should it do? I've also thought about whether we'd better offer a field instead, where members can choose between two categories - public and private. And then we use Access to restrict access to that category and to display only those profiles in the public search results that also belong to the corresponding category.

There is one special feature: Our members can write blog posts. Their name and a link to the profile will be displayed there. That should remain so. Those who do not publish their profile should also be displayed with their name. A click on their profile just leads to a "Access denied" page. I'm not sure if making their profiles in one way or the other would render the View unusable.

I know how to show and hide content within a post depending on privacy settings. But I didn't find out how to do that with whole posts.

Thanks for your help.

Best,
Marcial

#1387741

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Toolset offers the step by step tutorial that you may use to understand how you can create membership sites using Toolset:
=> https://toolset.com/learn/create-membership-site-wordpress-using-toolset-plugins/

The following doc may help you as well:
=> https://toolset.com/learn/create-membership-site-wordpress-using-toolset-plugins/restricting-read-access-specific-pages-posts-members/

Even, we offer a free test platform http://www.discover-wp.com where you can install any of the available reference site we offer and play with that:
=> https://discover-wp.com/site-types/membership-layouts/

If you want to grant the role-based access to specific content inside pages, you can use the Toolset access shortcode [toolset_access]
=> https://toolset.com/documentation/user-guides/access-control-texts-inside-page-content/

I suggest first to follow the above materials and test site and play with that and check what's possible and prepare your site's flow and if you need any further help, please feel free to get in touch with us.

#1388547

Hi Minesh

Thanks for the links. However I didn't find an answer to my question when reading trough the docs.

I'd like to let the autors decide, if their post is public or not. Wheter that is by assigning it a "Post Group" or another way of doing it. Can you help me with that?

Best,
Marcial

#1388587

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - as explained with the following Doc - you can restrict the posts to your members-only by following the instructions given in the following doc:
=> https://toolset.com/learn/create-membership-site-wordpress-using-toolset-plugins/restricting-read-access-specific-pages-posts-members/

However - the only thing is that for now, you need to add what post you want to add that post group that should be accessible to memeber-only manually. There is no dynamic feature available and to do that you will need to adjust your code.

So, while you save your post, you need to add the access-group name to your post that is used to identify that this post belongs to this post group. However, first of all, I would like to know from where you want to allow user to select checkbox to make their profile private/public? is it from backend or using Toolset form?

#1388639

Hi Minesh

I now have the following setup:
- Memberprofiles are public as default (set in Access)
- I created a Post Group called "Private Profiles", that deny access for guests

So far that works. When visiting a member profile as a guest, it works except the post group of that profile is set to "Private Profiles".

Users can edit their profile with a frontend form. I made a user field to store their privacy setting with a radio selection. But of course we can change that to connect it with the post group value. Is this then the way to do it: https://toolset.com/forums/topic/how-to-use-cred-post-form-to-set-post-group-for-new-cpt-post/#post-572829?

Will it be possible to exclude those profiles from public search results? We could also use two views, one for guests that only searches in the public profiles and one for logged in users that checks all profiles.

What do you think of my suggestion to use a taxonomy? Wouldn't that be easier? Or doesn't that work the way I think it would?

Thanks a lot for your help!

#1388645

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Users can edit their profile with a frontend form. I made a user field to store their privacy setting with a radio selection. But of course we can change that to connect it with the post group value. Is this then the way to do it: https://toolset.com/forums/topic/how-to-use-cred-post-form-to-set-post-group-for-new-cpt-post/#post-572829?
==>
Yes - this is exactly what I'm going to suggest to you. so, you can attach the _wpcf_access_group meta key with its value dynamically when user make profile private - the meta key should be added with the post group value and if its public the meta key should be deleted.

Will it be possible to exclude those profiles from public search results? We could also use two views, one for guests that only searches in the public profiles and one for logged in users that checks all profiles.
==>
Yes, it's better to have two views. You can try to use the wpv_filter_query hook to filter your view's results on fly.

What do you think of my suggestion to use a taxonomy? Wouldn't that be easier? Or doesn't that work the way I think it would?
==>
No, I do not think taxonomy would help here because its much easier to update the post meta as access group is attached to post meta.

#1388701

Hi Minesh

I found the meta-key: wpcf-custom-group-b4123778fc883721e99c02bcda3a5213.

And I have the user-field for the privacy-setting in the field wpcf-user_profile_privacy. It's value can be 1 or 2. 1 is public (no post group needed), 2 is when the post group should be enabled to restrict access.

The form ID is 139.

I adjusted the code as far as I can. But I have two questions remaining:
1. Do I need to change something in line 10 to let that work for ALL user roles?
2. The example code seems to check if a user is an admin. How can I adjust that to my case? The function should check if the value of "wpcf-user_profile_privacy" is "2".

add_action('cred_save_data', 'my_save_data_action',10,2);
 
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==139)// Change to your CRED Form ID you use
    {
        $user = wp_get_current_user(); // We get the current user data
         
         if ( in_array( 'administrator', (array) $user->roles ) ) { // My example checks for admins. You can use any Custom Role as well here
           update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-b4123778fc883721e99c02bcda3a5213');// We update the metafield for the post created, and pass the correct Access Group ID. This is the value you need to find in the database.
        }
        else {// something else if above is not true
            update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-b4123778fc883721e99c02bcda3a5213');
        }
    }
}
#1388703

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry, I'm bit confused when you say, you have a user-field.

Can you please share the screenshot of your form where you build the Toolset form in the backend? Once I check that I will answer your other questions.

#1388733
cred-post-group.PNG

Sorry, my bad. I mixed two different things up.

The field is a normal custom post field from the profile CPT: wpcf-mitglied_privacy.

This is the shortcode in the form.

[cred_field field="mitglied_privacy" force_type="field" class="form-check-input search-filters" output="bootstrap"]
#1389273

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - so when checkbox value is equal to 2 is when the post group should be enabled to restrict access. we need to attach that submitted post with access group.

You should try to use the following code:

add_action('cred_save_data', 'my_save_data_action',10,2);
  function my_save_data_action($post_id, $form_data) {
    // if a specific form
    if ($form_data['id']==139)// Change to your CRED Form ID you use
    {
        $user = wp_get_current_user(); // We get the current user data
          
        If(isset($_POST['wpcf-mitglied_privacy']) and $_POST['wpcf-mitglied_privacy']==2) {
           update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-b4123778fc883721e99c02bcda3a5213');// We update the metafield for the post created, and pass the correct Access Group ID. This is the value you need to find in the database.
      }
    }
}

I adjusted the code as far as I can. But I have two questions remaining:
1. Do I need to change something in line 10 to let that work for ALL user roles?
==>
Yes. I adjusted the above code so that there is no role restrictions applied.

2. The example code seems to check if a user is an admin. How can I adjust that to my case? The function should check if the value of "wpcf-user_profile_privacy" is "2".
==>
Yes - I changed/adjusted the above code accordingly.

#1389469

Awesome, that works. But now it should remove the post group again if the value changes to 1. How would I do that?

And how can I add a query filter to my existing search form to only display posts that have set the privacy field value to 1? Is there a way to add the query filter area to my search view later on (in the backend)?

#1389499

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

That is really simple. You can use the delete_post_meta() function.

Please use the following updated code:

add_action('cred_save_data', 'my_save_data_action',10,2);
  function my_save_data_action($post_id, $form_data) {
    // if a specific form
    if ($form_data['id']==139)  {
        $user = wp_get_current_user(); // We get the current user data
           
        if(isset($_POST['wpcf-mitglied_privacy']) and $_POST['wpcf-mitglied_privacy']==2) {
           update_post_meta($post_id,'_wpcf_access_group','wpcf-custom-group-b4123778fc883721e99c02bcda3a5213');
           // We update the metafield for the post created, and pass the correct Access Group ID. This is the value you need to find in the database.
      } else if(isset($_POST['wpcf-mitglied_privacy']) and $_POST['wpcf-mitglied_privacy']==1) {
                         delete_post_meta( $post_id, '_wpcf_access_group','wpcf-custom-group-b4123778fc883721e99c02bcda3a5213');
     }

    }
}
#1389519

Perfect! Thanks a lot.

Maybe you didn't see my other comment I added later to my previous post:

"And how can I add a query filter to my existing search form to only display posts that have set the privacy field value to 1? Is there a way to add the query filter area to my search view later on (in the backend)? Or can I set that in the shortcode?"

#1389541

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - you should edit your view and navigate to "Query Filter" section and add your custom field filter mitglied_privacy and set its value to 1.

More info:
=> https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/

#1391337

Hi Minesh

Thanks a lot. Everything works. And the solution was easier than I first thought.

Best,
Marcial