Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Hide theme page and post options for specific role WordPress

  • SOLVED

Hi,

I'm using the Sahifa Wordpress theme.

How can I hide/remove the Sahifa page options and Sahifa post options on a new of existing page or post for a specific role (for example ‘editor)?

I have create a child theme and it would be great if the modification can be placed inside the functions.php inside the child theme.

Thanx.

Answers (1)

2013-01-15

Dbranes answers:

you could use the the <strong>remove_meta_box</strong> function like this:

if (is_admin()) :
function my_remove_meta_boxes() {
if(!current_user_can('administrator')) {
remove_meta_box('someboxname', 'post', 'normal');
remove_meta_box('someboxname', 'page', 'normal');
}
}
add_action( 'admin_menu', 'my_remove_meta_boxes' );
endif;


see
http://codex.wordpress.org/Function_Reference/remove_meta_box

You will have to find the name of the metaboxes you want to remove, instead of the <em>someboxname</em>.

You could look for the code <strong>add_meta_box</strong> to get the correct sahifa metabox names.

---

There are also plugins that can hide admin menu based on the user role:

http://wordpress.org/extend/plugins/admin-menu-editor/screenshots/
http://wordpress.org/extend/plugins/adminimize/screenshots/
http://www.deluxeblogtips.com/hide-admin-menu/ (not free)

---

You can also use

add_action( 'admin_init', 'my_remove_sahifa_pages' );
function my_remove_sahifa_pages() {
if(!current_user_can('manage_options')) {
remove_menu_page('sahifa.php'); // replace this with the correct file
}
}


where you can adjust the filename and the capabilityes (manage_options).

---

you can also look for

add_menu_page()


and edit the 3rd input parameter, i.e.

add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );


where $capability can be integers 0-10 or predefined values like 'add_users' or 'publish_posts'.

See more here

http://codex.wordpress.org/Roles_and_Capabilities
http://codex.wordpress.org/Function_Reference/add_menu_page



Bluewonder comments:

Hi,

I have look for add_meta_box in the theme files and I found the page post-options.php

Below you see the first lines of code of post-options.php:

<?php
add_action("admin_init", "posts_init");
function posts_init(){
add_meta_box("post_options", theme_name ." - Post Options", "post_options", "post", "normal", "high");
add_meta_box("page_options", theme_name ." - Page Options", "page_options", "page", "normal", "high");
}

I have placed the code below in the file functions.php inside my child theme of Sahifa:

if (is_admin()) :
function my_remove_meta_boxes() {
if(!current_user_can('administrator')) {
remove_meta_box('post_options', 'post', 'normal');
remove_meta_box('page_options', 'page', 'normal');
}
}
add_action( 'admin_menu', 'my_remove_meta_boxes' );
endif;

I have no result. What do I wrong?
Can you help me further please?


Dbranes comments:

ok, what happens with

function my_remove_meta_boxes() {
remove_meta_box('post_options', 'post', 'normal');
remove_meta_box('page_options', 'page', 'normal');
}
add_action( 'admin_menu', 'my_remove_meta_boxes' ,999);


Bluewonder comments:

Hi,

I've tried, but with no result. Maybe I do something wrong.
May I put the code in functions.php of a child theme?

Below you see the code in functions.php (inside the child theme):

<?php

$themename = "sahifa-child";

function my_remove_meta_boxes() {
remove_meta_box('post_options', 'post', 'normal');
remove_meta_box('page_options', 'page', 'normal');
}
add_action( 'admin_menu', 'my_remove_meta_boxes' ,999);

?>


Dbranes comments:

ok, what about using different hook:


add_action( 'admin_init', 'my_remove_meta_boxes' ,999);


or

add_action( 'do_meta_boxes', 'my_remove_meta_boxes' ,999);


Bluewonder comments:

Hi,

I'm sorry, but the Sahifa page and post options are still visible.

See code below of functions.php in child theme:

<?php

$themename = "sahifa-child";

function my_remove_meta_boxes() {
remove_meta_box('post_options', 'post', 'normal');
remove_meta_box('page_options', 'page', 'normal');
}
add_action( 'do_meta_boxes', 'my_remove_meta_boxes' ,999);
?>

OR

<?php

$themename = "sahifa-child";

function my_remove_meta_boxes() {
remove_meta_box('post_options', 'post', 'normal');
remove_meta_box('page_options', 'page', 'normal');
}
add_action( 'admin_init', 'my_remove_meta_boxes' ,999);
?>



Dbranes comments:

<blockquote>but the Sahifa page and post options are still visible.</blockquote>

we are not trying to remove the Sahifa page with this code snippet, only metaboxes ;-)


Dbranes comments:

another hook ;-)

add_action( 'add_meta_boxes', 'my_remove_meta_boxes' ,999);


Bluewonder comments:

Hi,

I mean that the Sahifa theme "page options" are still visible/enable under the WYSIWYG-editor on a new of existing page.
Same for post.

This should not be visible for specific roles (for example 'editor').


Bluewonder comments:

Hi,

I mean that the Sahifa theme "page options" are still visible/enable under the WYSIWYG-editor on a new of existing page.
Same for post.

This should not be visible for specific roles (for example 'editor').


Bluewonder comments:

Hi,

I've tried with the code below, but no result.

<?php

$themename = "sahifa-child";

function my_remove_meta_boxes() {
remove_meta_box('post_options', 'post', 'normal');
remove_meta_box('page_options', 'page', 'normal');
}
add_action( 'add_meta_boxes', 'my_remove_meta_boxes' ,999);
?>


Dbranes comments:

ok ;-) strange - are there any other add_meta_box functions?


Dbranes comments:

can you use post-options.php in your child theme and change from

add_action("admin_init", "posts_init");
function posts_init(){
add_meta_box("post_options", theme_name ." - Post Options", "post_options", "post", "normal", "high");
add_meta_box("page_options", theme_name ." - Page Options", "page_options", "page", "normal", "high");
}


to

add_action("admin_init", "posts_init");
function posts_init(){
if(!current_user_can('administrator')) {
add_meta_box("post_options", theme_name ." - Post Options", "post_options", "post", "normal", "high");
add_meta_box("page_options", theme_name ." - Page Options", "page_options", "page", "normal", "high");
}
}


Bluewonder comments:

Hi,

I've tried with an adjusted post-options.php in my child theme (subdirectory: wp-content/themes/sahifa-child), but I have no result. It works well when I adjust the original file post-options.php in the subdirectory wp-content/themes/sahifa/panel

But there is a little problem. The Sahifa page and post options are not visible if I logged in as an administrator. When I logged in as an editor (different role) the options are well visible. It has to be reversed/conversed.

I used the following code in post-options.php:

add_action("admin_init", "posts_init");
function posts_init(){
if(!current_user_can('administrator')) {
add_meta_box("post_options", theme_name ." - Post Options", "post_options", "post", "normal", "high");
add_meta_box("page_options", theme_name ." - Page Options", "page_options", "page", "normal", "high");
}
}

Should it be possible the place the adjusted code in the child theme? If there is an update of the Sahifa theme, the customized file post-options.php will be overwritten.


Dbranes comments:

ok replace

!current_user_can('administrator')

with

current_user_can('administrator')

Are you sure the child theme is working? and no typos in filenames ?


Bluewonder comments:

Hi Dbranes,

It works when I adjust the original file post-options.php
Thanx.

Maybe there is a problem with my child theme.
I've tried something with style.css in my child theme, but nothing changed on my website.

What exactly do you mean with "typos"?


Dbranes comments:


typing errors (like function.php instead of functions.php)

maybe this will help you:
http://codex.wordpress.org/Child_Themes
http://wp.tutsplus.com/tutorials/theme-development/child-themes-basics-and-creating-child-themes-in-wordpress/


Bluewonder comments:

Dbranes,

Thanx a lot for your help.

I will look further at my child theme.
Thanx for the url's.