Remove publishing capability from user IDs - php

I'm trying to remove the capability of users other than the administrator/superadmin (ID number 1) to add a page, I know that there are plugins which you can edit the wordpress role but in my case I need it to be per user/username/userid (no plugin that I no of is available)..
Currently user number 2 needs to be 'Administrator' because a specific plugin I use only displays reports to 'Administrator' role but I need to remove add page capabilities. I have the following code:
function modify_capabilities()
{
global $userdata;
get_currentuserinfo();
$userdata->ID != 1 ->remove_cap('publish_pages');
}
add_action('admin_init','modify_capabilities');
But it doesn't work.. The error is in this line:
$userdata->ID != 1 ->remove_cap('publish_pages');

Your code seems off by a bit:
function modify_capabilities()
{
global $userdata;
get_currentuserinfo();
if ($userdata->ID != 1) {
$role = get_role('author');
$role->remove_cap('publish_pages');
$role->remove_cap('publish_posts');
}
}
add_action('admin_init','modify_capabilities');
Updated based on your comments and taking the original details from the blog you linked to. Not sure why you removed those parts though...

Related

Wordpress update user meta doesn't work with strtolower

I've a problem with the wp_update_user in wordpress. The code below works, but not as I want.
add_action('user_register', 'register_role', 10 , 1);
function register_role($user_id) {
$userdata = array();
$userdata['ID'] = $user_id;
$userdata['role'] = $_POST['Newrole']; //value for example 'Goldmember'
wp_update_user($userdata);
}
I have several problems:
the "Goldmember" is the displayname of the userrole. I see the new usermeta in the database (a:2:{s:6:"Goldmember";b:1;s:6:"subscriber";b:1;})
the "Goldmember" doesn't work in wordpress user administration, and the user has the default role activated
if I change "Goldmember" to "goldmember" with strtolower($_POST['Newrole']) the code doesn't work anymore
if I change the "Goldmember" to "goldmember" in the database (a:2:{s:6:"goldmember";b:1;s:6:"subscriber";b:1;}), it is visible in wordpress user administration and works
what do I have to change in my code, I don't see any mistake.
thanks a lot
I've changed my way to solve the problem. I took an other hook to change the userrole on registration.
I took the "uwp_after_process_register", this one will be fired at the whole end of the registration process, so there I can do, whatever I want.
This is now my solution, so every problem I had is solved, you only have to take UsersWP:
add_action('uwp_after_process_register', 'register_role', 10 , 2);
function register_role($data, $user_id) {
$userdata = array();
$userdata['ID'] = $user_id;
$userdata['role'] = $data['FormFieldRole'];
wp_update_user($userdata);
}

I am trying to link the comment author name to site url/profile/author username

I'm setting up a wordpress website where users can comment based on user roles.
I would like to link the name of the comment author to their personal profile page (site url/profile/username).
I have almost 0 knowledge about PHP, and I know a bit of CSS. I have tried a couple of different code snippets in my child theme function.php, but none of them seem to work properly.
The following snippet for example, only links the comment author name to site url/profile/user ID, but I would like it to be site url/profile/username
function force_comment_author_url($comment)
{
// does the comment have a valid author URL?
$no_url = !$comment->comment_author_url || $comment->comment_author_url == 'http://';
if ($comment->user_id && $no_url) {
// comment was written by a registered user but with no author URL
$comment->comment_author_url = 'http://www.founderslair.com/profile/' . $comment->user_id;
}
return $comment;
}
add_filter('get_comment', 'force_comment_author_url');
I expect to get the username and not the user ID. I've tried some changes in the snippet but nothing seems to work. I'd like to know what I am doing wrong and what I can do to improve it.
Thanks in advance.
You can make use of the inbuilt get_userdata function to find out the comment author username. I have explained the added coded in comments as suffix.
function force_comment_author_url($comment)
{
// does the comment have a valid author URL?
$no_url = !$comment->comment_author_url || $comment->comment_author_url == 'http://';
if ($comment->user_id && $no_url) {
$c_userdata = get_userdata( $comment->user_id ); // Add - Get the userdata from the get_userdata() function and store in the variable c_userdata
$c_username = $c_userdata->user_login; // Add - Get the name from the $c_userdata
// comment was written by a registered user but with no author URL
$comment->comment_author_url = 'http://www.founderslair.com/profile/' . $c_username; // Replace - user_id with username variable.
}
return $comment;
}
add_filter('get_comment', 'force_comment_author_url');

WooCommerce edit order privilledge

Hello i made a new role using Ultimate Member named stock_user
i want all users in the stock_user role only thing they can do
is to change the order status in wooCommerce admin page from processing to shipped only
i tried plugins but nothing worked im sorry i don't have any code but i don't know where to start
can anyone tell me where to start to make this work ?
i want to limit this dropdown menu options to processing & shipped only when a user with privildge stock_user is logged in
EDIT 2: Its now working as it should thank to IndexTwo Answer but the problem is that inventory role is a custom role made by Ultimate Member plugin how can i make the status work with it ? here is the code of Indextwo
function vnm_remove_woo_statuses($statuses) {
$currentUser = wp_get_current_user();
$removeStatusesArray = array('pending', 'completed', 'cancelled', 'refunded', 'failed', 'refund-issued', 'shipping-progress', 'on-hold');
// If it's inventory user role
if ( in_array('inventory', $currentUser->roles)) {
foreach ($removeStatusesArray as $status) {
if (isset($statuses['wc-' . $status])) {
unset($statuses['wc-' . $status]);
}
}
}
return $statuses;
}
add_filter('wc_order_statuses', 'vnm_remove_woo_statuses');
i mean it works when i change inventory to administrator just fine but how to get it working with the custom role (inventory) ? thank you
There are a couple of ways to approach the logic on this, but here's the function first:
function vnm_remove_woo_statuses($statuses) {
$currentUser = wp_get_current_user();
$removeStatusesArray = array('pending', 'completed', 'cancelled', 'refunded', 'failed', 'refund-issued', 'shipping-progress', 'on-hold');
// If it's any role other than administrator, remove the status
if (!in_array('administrator', $currentUser->roles)) {
foreach ($removeStatusesArray as $status) {
if (isset($statuses['wc-' . $status])) {
unset($statuses['wc-' . $status]);
}
}
}
// ALWAYS need to return the status array regardless
return $statuses;
}
add_filter('wc_order_statuses', 'vnm_remove_woo_statuses', 5, 1);
Note the last line of the function: you always need to return the value of a filter, regardless of the logic therein - otherwise things go wonky. I've also set a high priority of 5 just in case anything else was hooking in.
The conditional logic is currently set to remove statuses if ANY role other than administrator tried to edit an order:
if (!in_array('administrator', $currentUser->roles)) { /* Do stuff */ }
In your example, you were removing the statuses only for administrators. You could switch that around so that it only happens for stock_user users:
if (in_array('stock_user', $currentUser->roles)) { /* Do stuff */ }
...however, be aware that if you have the ability to add multiple roles to a user and add Stock User to an administrator, their access to ordser statuses would also be restricted here.

Show stuff only if profile owner does not belong to role X (in Drupal 7)

I have a Drupal 7 site, and I would like to print out inside the profile page some field content, according to the role of the profile user (not the logged in user).
I need to show a specific field only if the profile owner does not have role autor:
<?php
if (!in_array('autor', array_values($user->roles))) {
print drupal_render(field_view_field('profile2', $profile['main'], 'field_nombrecompleto1', 'value'));
}
else {print "yada yada";}
Note: The profile field in question, is a rofile2 field and not a core profile one.
What´s wrong with that code? Because it will print out the field in question in any case, regardless of the role of the profile owner.
I´ve tried this other code, and in this case the field won´t print out in any case:
<?php
if (is_array($user->roles) &&
in_array('authenticated user', $user->roles) &&
!in_array('autor', $user->roles)) {
print drupal_render(field_view_field('profile2', $profile['main'], 'field_nombrecompleto1', 'value'));
}
?>
Here is how this is accomplished in Drupal 7 when modifying the form for editing your profile:
function MYMODULE_form_profile2_edit_main_form_alter(&$form, $form_state) {
global $user;
$roles = $user->roles;
if(!in_array('authenticated user', $roles)) {
$form['profile_main']['MY_FIELD_ID']['#access'] = FALSE;
}
}

Drupal: How to display part of an author's profile in a node

I have been trying to do display a custom field I created in the manage fields section of user accounts for nodes in addition to the profile page. The problem I am having with this code is that it will display the first field it finds and display that for every user, not the field for that particular user.
And ideas? I believe it's finding the first value in the array and not the value for the particular user in the array.
Here is m setup so far:
Added this to my template.php of my theme:
function mythemename_preprocess_node(&$vars) {
global $user;
$user = user_load($user->uid); // Make sure the user object is fully loaded
$team = field_get_items('user', $user, 'field_team');
if ($team) {
$vars['field_team'] = $team[0]['value'];
}
}
Then, added this to my node.tpl.php in order to display it on nodes.
if (isset($field_team) && !empty($field_team)) :
echo '$field_team.'</div>';
endif;
UPDATE:
Found my own aswer here:
http://drupal.org/node/1194506
Code used:
<?php
$node_author = user_load($node->uid);
print ($node_author->roles[3]);
print ($node_author->field_biography['und'][0]['value']);
?>
You can use drupal's 'Author Pane' module for that. Try this:
http://drupal.org/project/author_pane

Categories