PHP function reference using args - php

I'm new to PHP. I'm trying to hide certain dashboard nav items from certain users (editors). I've added this to the functions, which has hidden it for all users:
<?php
function remove_menus(){
remove_menu_page( 'edit-comments.php' ); //Comments
}
add_action( 'admin_menu', 'remove_menus' );
?>
It says here you can use 'current_user_can' to pinpoint certain users but I'm unsure how to use both together.
So far I've tried:
function remove_menus(){
current_user_can(
remove_menu_page( 'editor', 'edit-comments.php' ); //Comments
) );
}
and
function remove_menus(){
current_user_can( array(
remove_menu_page( 'editor', 'edit-comments.php' ); //Comments
) );
}
..but from looking at other functions, they seem to in brackets with => inbetween so I'm presuming I'm using this function the wrong way.
Any help would be appreciated, thanks.

First answer, very simple , use logic "OR" operator:
<?php
function remove_menus(){
if( current_user_can('editor') || current_user_can('administrator') ) { // stuff here for admins or editors
remove_menu_page( 'edit-comments.php' ); //stuff here for editor and administrator
}
} ?>
If you want to check more than two roles, you can check if the roles of current user is inside an array of roles, something like:
<?php
function remove_menus(){
$user = wp_get_current_user();
$allowed_roles = array('editor', 'administrator', 'author');
if( array_intersect($allowed_roles, $user->roles ) ) {
remove_menu_page( 'edit-comments.php' ); //stuff here for allowed roles
}
} ?>
However, current_user_can can be used not only with users role name, but also with capabilities. So, once both editors and administrators can edit pages, your life can be easier checking for that capabilities:
<?php
function remove_menus(){
if( current_user_can('edit_others_pages') ) {
remove_menu_page( 'edit-comments.php' );// stuff here for user roles that can edit pages: editors and administrators
}
}
?>
Have a look here for more info on capabilities.

Related

How to hide specific page (from wp-admin) for certain users in wp?

My Image
I just wanted to hide specific page for certain users.
function remove_menus(){
// get current login user's role
$roles = wp_get_current_user()->roles;
// test role
if( in_array('administrator',$roles)){
remove_menu_page( 'edit-comments.php' ); //Posts
remove_menu_page( 'tools.php' );
remove_menu_page('edit.php');
remove_menu_page('wpcf7');
}
}
add_action( 'admin_menu', 'remove_menus' , 100 );
This what I am tried upto now and its working fine for all the page.
My question is I dont want to show home - Front page (Please see my image) If logged in user is not admin. and also I want to hide add new
You can use user's role capabilities and allow on the basis of role for add new items.
function manage_user_action() {
// get current login user's role
$roles = wp_get_current_user()->roles;
if( !in_array('administrator',$roles)){
//remove capabilities
$roles->remove_cap( 'edit_pages');
}
}
add_action( 'admin_init', 'manage_user_action');
To Remove Page from list
function jp_exclude_pages_from_admin($query) {
global $pagenow, $post_type;
if ( !current_user_can( 'administrator' ) && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__not_in'] = array( '10'); // Enter your page IDs here
//don't forget to the query
return $query;
}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );
For more help see this link : Click Here
I have extended #dineshkashera answer to target a specific user by ID and the code should be as follows :
function jp_exclude_pages_from_admin($query) {
global $pagenow, $post_type;
$current_user_id = get_current_user_id();
if ( $current_user_id == 2 && $pagenow == 'edit.php' && $post_type == 'page' )
$query->query_vars['post__not_in'] = array( '11', '12','13' ); // Enter your page IDs here
}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );
This code will get the user with ID of 2 and remove the pages IDs 11,12 and 13 from their WP page edit screen.

remove_menu_page() not getting initialized within if condition

I am trying to remove some menu pages based on user role, but when I add the function inside the if condition it doesn't do anything.
function contributor_posts_action() {
if ($role == 'contributor_posts') { // contributor_posts - custom role
// echo 'here'; for testing purposes and WORKS, so it goes under the if condition
add_action( 'admin_menu', 'remove_menus_contrib' );
function remove_menus_contrib(){
remove_menu_page( 'edit-comments.php' );
remove_menu_page( 'tools.php' );
remove_menu_page( 'edit.php?post_type=directory' );
remove_menu_page( 'edit.php?post_type=city' );
} // this function doesn't get hooked
add_action( 'admin_bar_menu', 'remove_admin_bar_items', 999 );
function remove_admin_bar_items( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'new-directory' );
$wp_admin_bar->remove_node( 'new-city' );
}// this one works properly. It's for removing for admin bar.
}
}
add_action( 'admin_init', 'contributor_posts_action' );
Try pulling the remove_menus_contrib() and the add_action( 'admin_menu', 'remove_menus_contrib' ) hook function out of your contributor_posts_action() function.
Some Wordpress hooks won't work inside other (custom) functions.

How to run a function on theme activation only in WordPress?

I want to add a capability to one of the default roles in WordPress. The add_cap article advises people to do this sort of thing on theme activation because the setting is saved to the database:
NB: This setting is saved to the database, so it might be better to run this on theme/plugin activation
The function I intend to use is:
function add_theme_caps() {
$role = get_role( 'author' );
$role->add_cap( 'edit_others_posts' );
}
add_action( 'admin_init', 'add_theme_caps');
As you can see, I'm currently hooking to admin_init, which causes the function to be run each time the admin area is accessed. How can I run the function on theme activation only?
You can use after_switch_theme . It will affect only your theme, of course.
http://codex.wordpress.org/Plugin_API/Action_Reference/after_switch_theme
I feel you should try something like this
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {
$role = get_role( 'author' );
$role->add_cap( 'edit_others_posts' );
}

Admin notice for custom fields in users dashboard

In Wordpress, I added some fields in the users dashboard. I made them required but I'd like to display a message if they are empty.
Here is an example (just one field) of what I did :
function my_admin_notice() { ?>
<div class="error">
<p><?php _e( 'Error!', 'user_street' ); ?></p>
</div>
<?php
}
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
if (!empty($_POST['user_street'])) {
update_user_meta( $user_id, 'user_street', $_POST['user_street'] );
}
else {
add_action('admin_notices', 'my_admin_notice');
return false;
}
}
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
Unfortunately, nothing happens.
I also tried with add_settings_error but I had the same problem.
Can someone give me a hand or just explain to me what I am doing wrong? It would be much appreciated! Thank you very much!
Looks like it's too late to add the admin_notice. An alternative is to use the action hook load-$page (that runs at the very beginning of a page load) and check if the user meta is empty. If so, trigger the notice.
add_action( 'load-profile.php', 'notice_so_23373245' );
add_action( 'load-user-edit.php', 'notice_so_23373245' );
function notice_so_23373245()
{
// Check to see if page is user-edit or profile
$user = isset( $_GET['user_id'] ) ? $_GET['user_id'] : get_current_user_id();
if( empty( get_user_meta( $user, 'user_street' ) ) )
add_action('admin_notices', 'my_admin_notice');
}

Block subscribers completely from beckend on wordpress

I am designing my website on wordpres. And I want to bolck subscribers completely from backend. I managed little little with some plugins. But the problem is that when they post something they get their display name beside their post and when they click on their display name they got redirected to their profile page on the backend. Please help me how to redirect subscriber to custom profile page when they click on their dispaly name and completely block them from beckend.
There is a great plugin by Christopher Davies (chrisguitarguy) which does what you are asking:
<?php
/*
Plugin Name: No Dashboard
Description: Don't allow subscribers to access to the wp-dashboard
Author: Christopher Davis
Plugin URI: https://gist.github.com/chrisguitarguy/1877504
*/
register_activation_hook( __FILE__, 'wpse43054_activation' );
function wpse43054_activation()
{
$role = get_role( 'subscriber' );
if( $role ) $role->remove_cap( 'read' );
}
register_deactivation_hook( __FILE__, 'wpse43054_deactivation' );
function wpse43054_deactivation()
{
$role = get_role( 'subscriber' );
if( $role ) $role->add_cap( 'read' );
}
add_action( 'init', 'wpse43054_maybe_redirect' );
function wpse43054_maybe_redirect()
{
if( is_admin() && ! current_user_can( 'read' ) )
{
wp_redirect( home_url(), 302 );
exit();
}
}
add_filter( 'get_user_metadata', 'wpse43054_hijack_admin_bar', 10, 3 );
function wpse43054_hijack_admin_bar( $null, $user_id, $key )
{
if( 'show_admin_bar_front' != $key ) return null;
if( ! current_user_can( 'read' ) ) return 0;
return null;
}

Categories