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'm developing a website for a product (audio speaker) I'm launching and I'm experiencing an issue I can't resolve (on my own ;) ).

I've added this code to functions.php to disable dashboard access for subscribers.

add_action('admin_init', 'no_mo_dashboard');
function no_mo_dashboard() {
    if (!current_user_can('manage_options') && $_SERVER['DOING_AJAX'] != '/wp-admin/admin-ajax.php') {
        wp_redirect(home_url()); exit;
    }
}     

It works like a charm except for the fact that it resets language. i.e.: a user is logging in from page www.example.com/en/new-user the code above redirects him to www.example.com instead of www.example.com/en.

Is there a way to implement this?

share|improve this question
add comment

3 Answers 3

You need to use the same method qtranslate uses to get the language parameter in the url. Quickly glancing through the source it seems to be qtrans_getLanguage()

$url = home_url( qtrans_get_language() );
wp_redirect( esc_url( $url ) );
share|improve this answer
    
Isn't there an exit() missing? :) –  kaiser Oct 27 '13 at 15:41
1  
Yes along with the rest of the code that makes up the function. I was just giving an example. –  Chris_O Oct 28 '13 at 3:16
add comment

You could use the $path parameter with home_url as follows:

$url = home_url('/en');
wp_redirect($url);

See http://codex.wordpress.org/Function_Reference/home_url for more info

But I've just realised you wouldn't be able to hard-code the 'en' in would you. Perhaps you could try this instead which will give you the current URL?

$url = home_url(add_query_arg(array()));

Failing that, please clarify the process. I've not used qtranslate and unsure if it uses cookies or just the URL query to find the location?

share|improve this answer
add comment

Thanks for your replies. When changing my code into:

add_action('admin_init', 'no_mo_dashboard');
function no_mo_dashboard() {
if (!current_user_can('manage_options') && $_SERVER['DOING_AJAX'] != '/wp-admin/admin-ajax.php') {
$url = home_url( qtrans_get_language() );
wp_redirect( esc_url( $url ) ); exit;
 }
}

I get the following error:

Fatal error: Call to undefined function qtrans_get_language() in /home/timvauk2/domains/dutchaudioworks.com/public_html/wp-content/themes/flexform/functions.php on line 1715

Did I forgot something?

Nevermind on this reply, I found out the redirect code messed up my Woocommerce checkout page somehow. I will find another way to disable admin access… Thanks!

share|improve this answer
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.