WordPress plugin combination - wordpress

I'm using the pie registration plugin and I want to know how can I automatically define a role (in my case I want to define as subscriber) when people register and login. I want to do this because I want to restrict some zones of the website and redirect those zones for the register or login page. Since the free version of the plugin doesn't have restricted areas, I'm also using the plugin called members that does 50% of what I want. So I want to combine these 2 plugins' functionalities.
tldr: how can I give the role subscriber on pie-registration?

When you are going to register user you have assign it a role for ex.
if you are using wp_insert_user( $userdata ) function than you can assign user role in $userdata as follows.
$userdata = array(
'user_login' => 'login_name',
'user_pass' => 'pass',
'role' => 'role' // When creating an user, `user_pass` is expected.
);
There are some default roles in wordpress , you can also creates new roles by using following code.
<?php add_role( $role, $display_name, $capabilities ); ?>
And you can assign capabilities, so by using this two function you can achieve your goal.
You can read more about:wp_insert_user , add_role

Related

show only custom plugin menu for custom user role user using add role hooks

I have created a custom user role for new users in functions.php like below codes:
add_action( 'admin_init', 'add_custom_roles' );
function add_custom_roles() {
$student = add_role( 'student', 'Student', array(
'read' => false,
'assignments' => true,
));
if( null !== $student ) {
$role->add_cap( 'assignments', true);
}
}
Want to allow access student roles only to view my custom plugin page: assignments but it is not working. Custom plugin is created by add_menu_page hook.
You are getting close to your goal, to help yourself you could install this (https://it.wordpress.org/plugins/members/) which make capabilities handling easier, and then tweak the settings in order to obtain the menĂ¹ visibility you want.
You should "clone" the minimum WP user role required to enter the backend (should be contributor if i remember good) and then add your custom permission to access plugin page

Add alternate customer role to WordPress / WooCommerce

I am trying to add another customer role to WordPress and WooCommerce. I will use this new customer role to assign alternate prices when the user is logged in. My code works but I cannot find what permissions a customer has in WordPress / WooCommerce by default. I want this new role to have identical permissions to the default customer account. The code below is located in my child functions.php file.
/* Custom user roles */
add_role('distributor', __(
'Distributor'),
array(
'read' => true, // Allows a user to read
'create_posts' => true, // Allows user to create new posts
'edit_posts' => true, // Allows user to edit their own posts
'edit_others_posts' => true, // Allows user to edit others posts too
'publish_posts' => true, // Allows the user to publish posts
'manage_categories' => true, // Allows user to manage post categories
)
);
You can use the capabilies of another role and use this as the "capabilities array" when you create the new user role with the add_role() function of wordpress. I assume the role you want the capabilities to be copied over is called customer. You can adjust this.
add_role( 'distributor', 'Distributor', get_role( 'customer' )->capabilities );
The function accepts an array of capabilites. With get_role() you get an role object and access the capabilities of that role.
So we create a new role with the capabilities of an existing role.

with 2 separate Wordpress installs, how to achieve a new member signing up on one site and automatically be inserted into the other?

How can this situation be done?
Wordpress install at ABC.com.
Wordpress install at 123.com.
A new member signs up at abc.com, then instantly and automatically, the same user's email address and password be programmatically inserted into 123.com. After the abc.com sign up process is completed, the new user is redirected to log in at 123.com.
How to achieve that?
you can achieve your functionality using two tricks, firstly you have to connect your wordpress with second DB.
global $wpdb;
$newdb = new wpdb( 'USERNAME' , 'PASSWORD' , 'DATABASE NAME' , 'HOST' );
$newdb->show_errors();
Then you can use the user_register hook to get all data from user upon registration AND update them to new database as a user.
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
//print_r($_POST); // to get all information of user
// Insert data in other database
$newdb->insert('wp_users', array(
'display_name' => $_POST['first_name'],
'user_email' => $_POST['user_email'],
'user_phone' => $_POST['user_phone'], // ... and so on
));
}
Haven't tried this code but it will work.

Add a user along with one custom post type

I am developing this plugin that admin can add a user in the backend and when user is created, plugin can automatically generate one custom post which I have added to the theme. The custom post will store user ID that is just created (or if it is possible make that user an author of the post)
I wonder if what I have mentioned above is possible practically. If anybody has any better advice, I am open for any suggestions.
Thank you in advance
I'm not sure that a unique custom post type per user is the best way to implement what you're wanting to achieve. If you have 100 users, you will have 100 custom post types making the wp-admin a nightmare as the left menu would grow with so many menu-items.
Wouldn't it be easier to just use a normal post type and then have the page that shows the user's dashboard filter the posts to only show posts where the user is the post_author? You could then add a hook to catch when a user registers and create the example post, you could modify the code below and add it to your functions.php:
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
$userPostsCategory = 3;
// Create post object
$my_post = array(
'post_title' => 'Sample Story' ),
'post_content' => 'You can edit this or create a new story',
'post_status' => 'publish',
'post_author' => user_id,
'post_category' => array( $userPostsCategory )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
This method will lower the number of customisations you'd have to do to your themes and make management of the posts a little easier.
Further reading on this:
User registration hook
Inserting a post using wp_insert_post

After user registered in wordpress the user should be inactive

In registration, after user registered, they should confirm the email validation link, until they validate the email their role should be inactive and they should not able to login in to the website.
Am adding the user using wp_insert_user(), in this function there is a field name call 'role' but I don't know what should be given there. I tried "inactive" but not working. Kindly suggest me.
Assuming your new users are assigned the 'subscriber' role on registration, you should hook to the 'user activated' hook and do something like:
wp_update_user( array ( 'ID' => $user_id, 'role' => 'author' ) ) ;
In your application you should ensure the 'subscriber' is locked-down or inactive (i.e. can't do anything much) and your 'author' role has the full set of capabilities that you need your active users to have.

Resources