Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want all my users (contributors and authors too) but the admin to be redirected to the homepage if they try to view mysite.com/wp-admin/. Contributors and Authors must be able to add and edit posts as usual, along with others they've to be forced to be redirected... I've already removed the link to the dashboard... I've also tried some plugins but the one that gets closer (Remove Dashboard Access) redirects to main page but prevents the contributors and authors from adding, editing and deleting posts. Thanks!

share|improve this question
add comment

3 Answers

I been using this code for a while I think it was originaly on a plugin called wp block admin but this works. You just have tho change the required compatibility so that it does what you need, look at this

$required_capability = 'edit_others_posts';
$redirect_to = '';
function no_admin_init() {      
    // We need the config vars inside the function
    global $required_capability, $redirect_to;      
    // Is this the admin interface?
    if (
        // Look for the presence of /wp-admin/ in the url
        stripos($_SERVER['REQUEST_URI'],'/wp-admin/') !== false
        &&
        // Allow calls to async-upload.php
        stripos($_SERVER['REQUEST_URI'],'async-upload.php') == false
        &&
        // Allow calls to admin-ajax.php
        stripos($_SERVER['REQUEST_URI'],'admin-ajax.php') == false
    ) {         
        // Does the current user fail the required capability level?
        if (!current_user_can($required_capability)) {              
            if ($redirect_to == '') { $redirect_to = get_option('home'); }              
            // Send a temporary redirect
            wp_redirect($redirect_to,302);              
        }           
    }       
}
// Add the action with maximum priority
add_action('init','no_admin_init',0);
share|improve this answer
    
It's not the way it's meant to be done, but +1 for considering the admin-ajax.php file. –  kaiser Mar 13 '13 at 11:00
add comment

There're only two things you need to check:

  • If the user is in the admin interface
  • If he got the capability or not

As long as we're using the template_redirect hook we don't have to check the login/register/password pages as the redirect will happen invisible to the user.

Here's the idea wrapped up as a little (mu-)plugin.

<?php
/**
 * Plugin Name: (#90535) Redirect Non-Admin users to the "Home"-page.
 * Description: Checks if we're in the admin UI and if the user has the admin only 'manage_options' capability
 * Version:     2013.03.12
 * Author:      Franz Josef Kaiser
 */
add_action( 'template_redirect', 'wpse90535_admin_denied' );
function wpse90535_admin_denied()
{
    is_admin()
    AND ! current_user_can( 'manage_options' )
        AND exit( wp_redirect(
             home_url()
            ,302
        ) );
}
share|improve this answer
add comment
up vote 0 down vote accepted

Unfortunately none of the above codes worked for me as they just redirected non admin to homepage even if I wanted authors and contributors to be able to add/edit and delete their posts... I've ended removing the boxes in the dashboard and I solved (partially) my issue.

I added this in functions.php

function disable_default_dashboard_widgets() {

    remove_meta_box('dashboard_right_now', 'dashboard', 'core');
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'core');
    remove_meta_box('dashboard_incoming_links', 'dashboard', 'core');
    remove_meta_box('dashboard_plugins', 'dashboard', 'core');

    remove_meta_box('dashboard_quick_press', 'dashboard', 'core');
    remove_meta_box('dashboard_recent_drafts', 'dashboard', 'core');
    remove_meta_box('dashboard_primary', 'dashboard', 'core');
    remove_meta_box('dashboard_secondary', 'dashboard', 'core');
}
add_action('admin_menu', 'disable_default_dashboard_widgets');
share|improve this answer
1  
Oh, seems like we both misread your question... Take our answers, modify them and then try again. Or make your question more clear with A) where the login/registration/password should redirect too and B) what exact page the user should get access too and to which one not. –  kaiser Mar 13 '13 at 11:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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