Allow specific user manage & install plugins - WooCommerce/Wordpress - php

I try to allow specific user to install and manage plugins. His role is Store Manager (WooCommerce), and this is my functions:
function add_theme_caps() {
// Get & Set user ID
$shopmanager_id = 7;
$shopmanager = new WP_User( $shopmanager_id );
// Let him manage and install plugins
$shopmanager->add_cap( 'edit_plugins', true );
$shopmanager->add_cap( 'install_plugins', true );
}
add_action( 'admin_init', 'add_theme_caps');
But it's not working. How to achieve that?

OK, so I made this in different way. There was some problem with Manager role from WooCommerce. So I decide to add this user ADMIN role, and then lower its privileges with custom code, if/else and custom redirections via functions.php.

Related

Know when a user is created in Wordpress

I try to find a way to know when a user is created in Wordpress, can anyone help me?
What I want is a function so when an admin is registered the fuction do X thing. What is X thing? I am going to take the data of that user to create an account on another platform.
Summary: I want to know when an administrator is created with a function to run another function.
There are multilple hooks you can use. Here is a nice overview : https://usersinsights.com/wordpress-user-registration-hooks-visualized/
I think user_register is the right one for you:
add_action( 'user_register', function ( $user_id ) {
$user_meta = get_userdata($user_id);
$user_roles = $user_meta->roles;
// you now have the role and can call your function after checking if it is admin
} );

How can i customize Wordpress list_users capability?

I am trying to find a way to customize the list_users capability in Wordpress,
I have created a new role called my_admin and i added some capabilities to it.
The problem is that the list_users capability displays all users while i want to display only a group of users according to a user meta field.
Another question about the edit_users capability can i limit this capability to edit the users without promoting them, this capability enables editing the users and also promoting them which means that it enables changes to user roles also. Is there any way i can remove this promoting feature from it, can i also select only one input field to be edited and remove other fields?
function my_new_service_capabilities() {
// Get the role object.
$editor = get_role( 'my_admin' );
// A list of capabilities to add.
$addcaps = array(
'read',
'edit_posts',
'list_users', //I need to list users according to a user meta value.
'edit_users', //I need to enable editing users in only one input field
'moderate_comments',
);
foreach ( $addcaps as $acap ) {
// Add the capability.
$editor->add_cap( $acap );
}
}
add_action( 'init', 'my_new_service_capabilities' );
I think this should work for you →
https://codex.wordpress.org/Class_Reference/WP_User_Query
steps →
Retrieve all users.
User For each loop.
Use meta_query parameter, because WP stores capabilities as json
string in user_meta.
I am giving you a clue how to do this →
$entire_user_list = get_users();
$somse_users= array();
foreach($entire_user_list as $user){
if($user->has_cap('specific_capability')){
$somse_users[] = $user;
}
}
I had solved it using the DOMDocument class.

How to hide save order button in woocommerce edit orders?

How to hide save order button from admin in woocommerce edit orders if specific role is logged in?
Orders menu is protected by ‘edit_shop_orders’ capability. It’s possible to prohibit new order creation if you activate ‘create’ capability at User Role Editor Settings.
But ‘edit_shop_orders’ capability is enough to ‘process’ and ‘complete’ the order. The only way to allow a read-only access to the WooCommerce orders is to use this filter from WooCommerce code:
$actions = apply_filters( 'woocommerce_admin_order_actions', $actions, $the_order );
Your function for this filter should return empty $actions array for ‘editor’ role in order to make for them read-only access.
Let me know if you need further help.
Add This piece of code to function which gets executed ONLY for "specific" role:
function add_my_action($actions, $the_order){
return array();
}
add_filter( 'woocommerce_admin_order_actions', 'add_my_action', 10, 2);**

Cannot assign user roles in wordpress

I was unable to login to wp-admin "You don't have sufficient privileges" so I added this code to my themes functions.php
/*
* Add your own functions here. You can also copy some of the theme functions into this file.
* Wordpress will use those functions instead of the original functions then.
*/
function codelight_all_permissions( $allcaps, $cap, $args ) {
$allcaps[$cap[0]] = true;
return $allcaps;
}
add_filter( 'user_has_cap', 'codelight_all_permissions', 0, 3 );
Then I was able to get in as site admin, I went to users and noticed there are no roles assigned. In the database I have level 10 and a:1:{s:13:"administrator";s:1:"1";} assigned.
If I create a new user I cannot choose a role for that user from the dropdown. I only get the option "No role for this site". Does anyone know how I can troubleshoot this issue?

How to remove unwanted roles from admin's "New User" Page?

Basiclly, I must Remove the next roles whenever the admin is creating a new user and clicks on the dropdown menu tu display all the available roles:
Contributor
Editor
Unapproved User
Author
It should only display Subscriber and Administrator.
Also I must change the name of Subscriber to something different, where is this code? This problem would seem to me like there was a plugin to it.
To remove a role you can use
<?php remove_role( 'editor'); ?>
and to rename a role add the following php t your functions.php file
function change_role_name() {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
//You can replace "administrator" with any other role "editor", "author", "contributor" or "subscriber"...
$wp_roles->roles['administrator']['name'] = 'Owner';
$wp_roles->role_names['administrator'] = 'Owner';
}
add_action('init', 'change_role_name');
(The above is from here
There is a function called remove_role that WordPress has in its PHP API. you use it like this:
<?php remove_role('author'); ?>
author could be replaced with any role you want. The way you want to use this command is different than most WP commands. This is because it removes the role from the DB so any future times, the role is already removed. I would recommend making a custom WP plugin that just removes the roles you want on activation.WP Plugin Help

Categories