0

I want to hook into the subscriber role index.php page (wp-admin/index.php) to add some custom content outside of the widgets.

This admin_init works for all admin pages:

add_action( 'init', 'test_init');
function test_init(){
    add_action( 'admin_init', 'test_admin_init');
}

function test_admin_init() {
    echo "Test Admin Init";
}

But this doesn't work for only subscribers:

add_action('admin_init', 'add_to_dashboard');

function add_to_dashboard() {
    if (current_user_can('subscriber') && is_admin()) {
            add_action( 'admin_init', 'test_admin_init');
    }
}

function test_admin_init() {
    echo "Test Admin Init";
}

And how would this work for subscribers for only index.php and not profile.php?

Is this the wrong way to go about adding custom content to the subscriber's admin index.php page?

2 Answers 2

0

All of WP's built-in roles have subscriber capabilities. You're basically checking here, "if the current user has at least subscriber capabilities". If you want to isolate only the subscribers, you can check for the absence of a higher capability. If you are just using built-in roles, the next higher role of Contributor has edit_posts and delete_posts capabilities by default.

if(!current_user_can('edit_posts')) {
    // display your message
}

As far as adding content this way, admin_init is a pretty early hook, so depending on your content and how it is styled, it may cause some layout quirks. You mentioned you don't want to use a dashboard widget, so you might want to look into other hooks, or perhaps add an options page that subscribers can view with your custom content.

1
  • Thanks, I do have an options page, but I need that to be limited to the user entering html and not php. I have unhooked all widgets and show content in one full width widget area. I think I found a way to add the content I want to in the template file that calls the options page custom fields. But, this is useful to know. Commented Nov 23, 2017 at 1:24
0

The reason your second code block doesn't work is because you're trying to add a callback to the admin_init hook at the default priority to the admin_init hook at the default priority, but the default priority (10) has already been fired. If you wanted to add another hook to admin_init you could do something like this:

add_action( 'admin_init', 'add_to_dashboard', 0 );
function add_to_dashboard() {
  if( current_user_can( 'subscriber' ) ) {
    add_action( 'admin_init', 'test_admin_init' );
  }
}

function test_admin_init() {
  echo "Test Admin Init";
}

Although, this would work too:

add_action( 'admin_init', 'add_to_dashboard' );
function add_to_dashboard() {
  if( current_user_can( 'subscriber' ) ) {
    echo "Test Admin Init";
  }
}

Although, I don't like testing to see if a particular user is part of a role by using current_user_can() it does work.

And how would this work for subscribers for only index.php and not profile.php?

If you want something to run only on index.php, you can either check the $pagenow global, or use the load-index.php hook.

Is this the wrong way to go about adding custom content to the subscriber's admin index.php page?

I don't know. You haven't given enough information about what you're trying to add and why a widget won't work.

1
  • Thanks, I use a theme options page to load content on index.php, but I need that to be limited to the user entering html and not php, so that's why I'm looking at this. I have unhooked all widgets and show content in one full width widget area. How would I use the load-index.php hook.? Commented Nov 23, 2017 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.