Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Small change to plugin. WordPress

  • SOLVED

My question is here:

https://wordpress.stackexchange.com/questions/345437/how-to-use-get-blogs-of-user

The code is here:

https://pastebin.com/yU5Ky6bb

Basically, it currently checks if a user is a member of a group account, and throws an error.

But I also want it to check if the user already has an account (not group account, just a normal WP user account) on any subsite on the multisite, and then throw the same error.

I tried adding the code you can see on the SE link, and it throws the error even though the user is new and does not have any accounts anywhere.

Answers (2)

2019-08-20

Gabriel Reguly answers:

Hi Jim,

What happens if you try using 1 for the count? As in the code below:

if ( (rcpga_group_accounts()->members->get_group_id( $user_id )) || ( count( $blogs_user) > 1 ) ) {

I suspect the user can belong to one site at least, else one cannot have an user id.

Cheers,
Gabriel


jimbob comments:

Good point.

Just tried it using 1 for the count as you suggested.

The user can be added to 2 groups, but not a third. If I try and add him to a third group, the error is shown.

What I need is: the user can be added only to 1 group, but not a second. If I try and add him to a second group, the error should be shown.


jimbob comments:

Just also tried with >= 1.
The error is shown, even though it's a new user with no sites.


Gabriel Reguly comments:

Hmm, now that is a different question.


Gabriel Reguly comments:

This ( count( $blogs_user) > 1 ) ) will check if user is present in more than 1 blog.


Gabriel Reguly comments:

And this (rcpga_group_accounts()->members->get_group_id( $user_id )) will check if user already belongs to 1 group.

From https://docs.restrictcontentpro.com/article/1892-group-accounts-functions

rcpga_group_accounts()->members->get_group_id( $user_id )

Returns the ID number of the given member's group (or false if none exists). $user_id can be omitted to check the current user.


jimbob comments:

Yes I understand that.
On my site, if the user has one blog, it actually means he is also in a group.
So checking if he has a site assigned is an easy way of checking if he is in a group.

The more complex way of doing it is to get a list of the sites the user is part of first then for each of those sites probably switch to it ( https://codex.wordpress.org/Function_Reference/switch_to_blog ) then check if the user is a group member of that site using the normal Group Accounts function, then switch back to the actual site he's on ( restore_current_blog(); ).

But why do it that way when you can just check if he has a normal account on any site on the network?


Gabriel Reguly comments:

Ok, then you need to know if the email address is already in use as emails must be unique.

At line 53, change the code:

// create a new user if one does not already exist
if ( $user = get_user_by( 'email', $args['user_email'] ) ) {

return new WP_Error( 'has-group', __( 'This user is already in a group. Users may only be part of one group at a time.', 'rcp-group-account' ) );

$user_id = $user->ID;
$new_user = false;
} else {
remove_action( 'user_register', 'rcp_user_register_add_subscription_level' );
$user_id = wp_insert_user( $args );
$new_user = true;
add_action( 'user_register', 'rcp_user_register_add_subscription_level' );
}


Gabriel Reguly comments:

Notice it will prevent new users to be added ;-)

E.g. One must first create the new user and then later add it to the group.


Gabriel Reguly comments:

It is the opposite :-/

It will prevent existing users to be added, only new users can be created and added to the groups.

Sorry about the wrong explanation.


jimbob comments:

Works perfectly.
Thank you!

2019-08-20

Fahad Murtaza answers:

Hi

I can help if you give me access to the plugin. I am going to inbox my details. I'll post the solution here once done.

Update: There is no way to send you a message to share secure details. So I'll let others help you out.

Fahad