• Hi,

    I’m creating a client portal and your video tutorial has been very helpful, now i’m trying to make my customer’s life easier and i would like to create automatically new page and custom capabilities when user register to the web site. I found some code here:
    https://www.s2member.com/forums/topic/updating-page-meta-data/

    and my code is:

    // Add page after new user registered
    add_action(‘user_register’, ‘registration_newpage’);

    function registration_newpage($user_id) {

    $user_info = get_userdata($user_id);
    $username = $user_info->user_login;
    $post = array();
    $post[‘post_name’] = ‘Members-Page-for-‘.$username; // The name (slug) for the page
    $post[‘post_type’] = ‘page’; //sets type
    $post[‘post_content’] = esc_attr($username.’ – This page was created for you and any messages that we need to send you with regards to any products, services or changes to your membership will be posted here.’.$userid);
    $post[‘post_author’] = $username;
    $post[‘post_status’] = ‘publish’; //status
    $post[‘post_title’] = $username;
    $post_id = wp_insert_post ($post);
    if (!$post_id) {
    wp_die(‘Error creating user page’);
    } else {
    update_post_meta($post_id, ‘_wp_page_template’, ‘page-members.php’);
    $user = new WP_User($user_id);
    $user->add_cap(“access_s2member_ccap_$username”);
    update_post_meta($post_id, ‘s2member_ccaps_req’, “$username”);
    $new_options = Array(); // s2member array for security level
    $new_options[“ws_plugin__s2member_level0_pages”] = $post_id; // set Level0 for this Page
    c_ws_plugin__s2member_menu_pages::update_all_options ($new_options, true, false, array (“page-conflict-warnings”), true); // s2member update

    }
    return;
    }

    It works great, it creates a new page and populate user custom capability and page custom capability with the username, the only problem is that it doesn’t protect the page, i mean if user1 is logged and he types wwww.mysite.com/user2 he can see user2 content and it shouldn’t be because i’m using custom capabilities.

    Hope someone can help me,
    Many thanks
    Vincenzo

    https://www.ads-software.com/plugins/s2member/

Viewing 8 replies - 1 through 8 (of 8 total)
  • I don’t believe that custom capabilities work that way. They grant capabilities to users at that particular level. They do not grant capabilities to the degree of page protection for a particular level.

    Because your code grants access to this page to anyone at level 0, it follows that anyone logged in to your site will be able to access the page.

    Thread Starter raizen84

    (@raizen84)

    It is possible if you put level 0 in the page and specify the same custom cap for the user and the page, as described in this video made by the plugin’s author:

    My problem is: how i can populate them automatically ??

    Yes, I understand that.

    But you need to grant the capability to a user. The code you have here is taken from a user who was creating one custom capability for all users, which is precisely the result you are getting.

    You need to create a specific capability for each user. No doubt it’s possible, but I don’t know how to do that.

    But how about this workaround? Instead of trying to protect the page, what about protecting the content instead? Then you can wrap the content with a conditional. You would, of course, have to change the title of the page to something like “Member’s Page”.

    Would that work for you?

    Thread Starter raizen84

    (@raizen84)

    Hi, thank for your reply.

    I’m using the username to create a specific capability for each user as you can see:

    $user->add_cap(“access_s2member_ccap_$username”);
    update_post_meta($post_id, ‘s2member_ccaps_req’, “$username”);

    And it works, it actually populate the fields correctly, but when i’m logged in the website as user1 and i type url for the user2 page i can see it even if the capability is set up ?? and i can’t really understand why ;(

    Protecting the content is great but what i’m trying is to make everything automatic at the registration process ??

    Have you got any idea on how to make it working?
    Thanks for your help

    I understand that, but you say this code isn’t working (edit: to make the process automatic). So my suggestion was to try something similar for the conditional wrapper in a page (though not, now, a page with that person’s name in the title).

    The process would still be automated, because you’d be directing new users to the one page, but then they would automatically be seeing different content when they got there.

    The only real difference to you is that you would have to set up the conditional on the page as well as this particular script.

    Thread Starter raizen84

    (@raizen84)

    Hi,

    I think i found where the problem could be. s2member save the custom capability for the page in a serialized array, so basically when the admin edits the page with some contents and then updates the page, s2member will store the capability with the correct array and everything will work fine ??

    Your idea is great but i’m scared it could cause confusion when the admin (will not be me but my client) has to update the page with personal user contents ??

    Thanks for your time and your suggestion anyway ??

    Glad you got it working!

    I am sure others will be interested to read and use your code!

    Thread Starter raizen84

    (@raizen84)

    Yes i hope it will be helpful even if it’s not working 100% :))

    Thanks also to Andrew Hodges who optimized the code and wrote in this post:

    https://www.s2member.com/forums/topic/updating-page-meta-data/

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Update posta meta’ is closed to new replies.