Wordpress Author box Control Function - php

Hello friends,
i need little help in Wordpress. I am trying to hide the authorbox that appears under the post for specific user only.
Example if post i am looking into is posted by admin, then i want to hide the authorbox under post content that is posted by admin, but should show for all other users ? i tried different functions but not able to success on this.
I want to completly hide the authorbox, i know it can be done with css code that is
.author-box { display:none;}
but this hides the overall authorbox of complete them, i just want to hide the authorbox on the posts that are made by admin ?
i am using genesis framework, so please suggest if any help you can make here.
Thanks

In your theme's functions.php file, add something like this:
function remove_my_post_metabox() {
global $post;
// If the post author ID is 1, then remove the meta box
// You would need to find the ID of the author, then put it in place of the 1
if ( $post->post_author == 1) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );
If you need to find out the role of the author, and truly do it based on the role (admin, for example), then it would be more like this:
function remove_my_post_metabox() {
global $post;
// If there's no author for the post, get out!
if ( ! $post->post_author) {
return;
}
// Load the user
$user = new WP_User( $post->post_author );
// Set "admin" flag
$is_admin = FALSE;
// Check the user roles
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role ) {
if ( $role == 'administrator' ) {
$is_admin = TRUE;
}
}
}
// Lastly, if they ARE an admin, remove the metabox
if ( $is_admin ) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );

Related

WordPress menu based on user roles

i am new to WordPress and trying to find a way to add menus based on user roles. I have (guest user or non logged in user) vendor and subscriber roles. I want to display different primary menus depending on what role user is. Example code taken from https://wpcodeus.com/display-different-wordpress-menu-to-logged-in-users/
Example code not working
function nav_menu_args( $args = '' ) {
if( is_user_logged_in('vendor')) {
( 'primary-menu' == $args['theme_location'] ) {
$args['menu'] = 'VendorMenu';
}
}
else if
( is_user_logged_in('subscriber')) {
( 'primary-menu' == $args['theme_location'] ) {
$args['menu'] = 'SubscriberMenu';
}
}
else
(!is_user_logged_in) {
( 'primary-menu' == $args['theme_location'] ) {
$args['menu'] = 'PrimaryMenu';
}
}
return $args;
}
add_filter( 'wp_nav_menu_args', 'nav_menu_args' );
At the moment it changes all menus to vendor menu including primary top and footer, all menus have the same menu.
Any help or guidance in the right direction will be much appreciated.
I am adding the code in to the custom/child theme functions.php file.
you can use current_user_can() function to check user capabiltiy or role. see this link.
This worked for me (slightly different method but same outcome)
if ( current_user_can( 'subscriber' ) ) {
//display menu
}
if ( current_user_can( 'vendor' ) ) {
//display menu
}

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.

Limit page creations by user role functions.php

Is it possible to create a function via functions.php that restricts users from creating pages based on their user role? So for example, users with the user role "limited" can only create 5 pages.
When they reach this amount, the "Create new page" button must be gone or they have te receive a message that they reached there limit and have to contact there admin.
I know there is a plugin called Bainternet Posts Creation Limits but this is causing some problems.
Is something possible with a function?
EDIT:
Ok, I tried the code below but aint working and to be honest my coding skills are to poor to make something of it.
Anybody else any ideas?
You could try something like this:
Step 1: Upon user creation: give your user the capability 'publish_pages'
Step 2: Save in the user_meta how many pages the user has created.
Step 3: If the user has 5 or more pages remove the capability from this user.
STEP 1 & 2 (untested):
add_action( 'user_register', 'user_registered', 10, 1 );
function user_registered( $user_id ) {
// ADD CAPABILITY
$user = new WP_User( $user_id );
if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'YOURROLEHERE', $user->roles ) ) {
$user->add_cap( 'publish_pages');
update_user_meta($user_id, 'numberofpages', 0);
}
}
STEP 3 (untested):
add_action('publish_page', 'user_published_page');
function user_published_page(){
// GET CURRENT USER
$user_id = get_current_user_id();
$user = new WP_User( $user_id );
// CHECK ROLE
if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'YOURROLEHERE', $user->roles ) ) {
// GET NUMBER OF PAGES CREATED
$numberofpages = intval(get_user_meta($user_id, 'numberofpages', true));
if($numberofpages >= 5){
$user->remove_cap( 'publish_pages');
} else {
update_user_meta($user_id, 'numberofpages', $numberofpages + 1);
}
}
}

Hiding widgets for specific user roles

So its about hiding widgets for specific user roles excluding Admin.Using custom sidebar plugin i don't want to be display on users end.Listed all Dashboard widgets through
function list_active_dashboard_widgets() {
global $wp_meta_boxes;
foreach (array_keys($wp_meta_boxes['dashboard']['normal']['core']) as $name) {
echo '<div>' . $name . '</div>';
}
}
add_action('wp_dashboard_setup', 'list_active_dashboard_widgets');
and found customsidebars-mb
What i am doing without succes is adding this code to hide sidebar options widget from users panel keeping it on admin panel.
function disable_default_dashboard_widgets() {remove_meta_box('customsidebars-mb', 'dashboard', 'normal');
}
add_action('admin_menu', 'disable_default_dashboard_widgets');
if (!current_user_can('manage_options')) {
add_action('wp_dashboard_setup', 'disable_default_dashboard_widgets');
}
**
Store front Theme Woocommerce Plugin
**Will be phasing out all plugins with codes
You can try this one in your functions.php,
add_action( 'widgets_init', 'remove_widgets_wpse_89138' , 15 );
function remove_widgets_wpse_89138()
{
// http://codex.wordpress.org/Function_Reference/is_admin
if( !is_admin() )
return;
// Grab current user info
global $current_user;
// Check for specific user
/*
$username = $current_user->user_login;
if( 'the_user_login' != $username)
return;
*/
// Check for capability
if( current_user_can( 'add_users' ) )
return;
unregister_widget( 'WP_Widget_Pages' );
}
Hope this will helps you. For more please visit, URL 1, URL 2

Displaying Logged-In User Name in Wordpress Menu

I'm using Wordpress with UserPro, and want my menu to display the logged-in user's first name, linked to the user profile page.
The problem is that in my menu structure, the "Profile" menu option is supposed to have a sub-menu containing "edit profile", "submit", and "logout".
This is my current code:
/*earlier code, currently commented out, for function to
display username in menu using #profile_name# placeholder
function give_profile_name($atts){
echo userpro_profile_data('first_name', get_current_user_id());
}
add_shortcode('profile_name', 'give_profile_name');
add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );
function my_dynamic_menu_items( $menu_items ) {
foreach ( $menu_items as $menu_item ) {
if ( '#profile_name#' == $menu_item->title ) {
global $shortcode_tags;
if ( isset( $shortcode_tags['profile_name'] ) ) {
// Or do_shortcode(), if you must.
$menu_item->title = call_user_func( $shortcode_tags['profile_name'] );
}
}
}
return $menu_items;
}
end of earlier code */
//currently in use, unlinked code
add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
function my_custom_menu_item($items)
{
if(is_user_logged_in())
{
$user=wp_get_current_user();
$name=$user->user_firstname;
$items .= '<li>'.$name.'';
}
return $items;
}
?>
I can fiddle around and try to add the sub-menu under the menu by fiddling with the code from Firebug, but that would mean manual edits to all the links in the functions.php, which would be tedious.
I want to be able to add, edit, remove, and redirect the sub-menu items easily via the Wordpress menu.
Please advise.
Okay, I found a solution (and it can be used for any theme, with any plugin as it only uses core WordPress functions).
In the menu, name the menu item where you want the user's name to appear with a place-holder (such as: #profile_name#, #user#, #random#, etc.)
Now, add the following code to the your child-theme's functions.php:
function give_profile_name($atts){
$user=wp_get_current_user();
$name=$user->user_firstname;
return $name;
}
add_shortcode('profile_name', 'give_profile_name');
add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );
function my_dynamic_menu_items( $menu_items ) {
foreach ( $menu_items as $menu_item ) {
if ( '#profile_name#' == $menu_item->title ) {
global $shortcode_tags;
if ( isset( $shortcode_tags['profile_name'] ) ) {
// Or do_shortcode(), if you must.
$menu_item->title = call_user_func( $shortcode_tags['profile_name'] );
}
}
}
return $menu_items;
}
In case you're using your own place-holder, remember to replace #profile_name# with the name of your custom place-holder in the code above.
Apologies in case I've misused the term 'place-holder'.

Categories