1

I want to allow users to sign up, log in and then capture information based on their interaction with a HTML5 game and some front end forms.

I still want to have an admin user who can edit some page content and manage multilingual aspects of the site.

Is there a plugin I can use to prevent users from accessing the Wordpress admin? In the past I've allowed users to register for Wordpress on this site

But when the confirmed users signs in they are presented with the wordpress admin. I don't want this. I want to redirect the user after login to a certain page, but they will still have logged in status and I can interact with their profile (save their score and responses to the front end forms).

I'm not asking for people to give me full answers about how to achieve this rather if you have had some experience with this kind of problem could you provide any pointers for me.

2

1 Answer 1

3
  1. Check for administrator user

    current_user_can ( 'manage_options' )

  2. Remove the admin bar for non-administrators

    show_admin_bar ( false )

  3. Redirect non-administrators to another page

    wp_redirect ( site_url( '/' ), 302 )


Putting it all together

function wpse_20160318_user_checks() {

    // ignore administrators
    if ( ! current_user_can( 'manage_options' ) ) {

        // ignore these events
        if  ((   defined( 'DOING_AJAX' )   && DOING_AJAX )
            || ( defined( 'DOING_CRON' )   && DOING_CRON )
            || ( defined( 'WP_CLI' )       && WP_CLI )
            || ( defined( 'REST_REQUEST' ) && REST_REQUEST )

        ) {
            return;
        }

        if ( is_admin() ) {

            // redirect if on the dashboard / back-end to a front-end page
            wp_redirect( site_url( '/' ), 302 );
            exit;

        }
        else {

            // remove admin bar on front-end
            show_admin_bar( false );
        }
    }
}

add_action( 'init', 'wpse_20160318_user_checks' );

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.