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 am using the Any Mobile Theme Switcher plugin to switch between full and mobile themes automatically.

The only problem with it is that the 'switch to mobile site' and 'switch to full site' links takes you to the home page instead of the current page.

The relevant bit of code seems to be this:

function show_theme_switch_link_func( $atts ){
        $desktopSwitchLink      = get_option('show_switch_link_for_desktop');
        global $shown_theme;
        global $status;
        if ($shown_theme){
                $return = '<a rel="external" data-ajax="false" href="'.get_bloginfo('url').'?am_force_theme_layout=desktop" class="am-switch-btn godesktop">'.get_option('desktop_view_theme_link_text').'</a>';
        } else {
                if ((!empty($status)) || ($desktopSwitchLink == 'yes')){
                        $return = '<a href="'.get_bloginfo('url').'?am_force_theme_layout=mobile" class="am-switch-btn gomobile">'.get_option('mobile_view_theme_link_text').'</a>';
                }
        }
        return $return;
}

Can I replace '.get_bloginfo('url').' with something that will return the current URL instead of the home address?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

A quick Google search lead me to this little gem from a blog post by Konstantin Kovshenin:

global $wp;
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );

Combine that with your original code, and you have this (updated to match the WordPress coding standards):

function show_theme_switch_link_func( $atts ){
    global $shown_theme, $status, $wp;

    $desktop_switch_link = get_option( 'show_switch_link_for_desktop' );
    $current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );

    if ( $shown_theme ) {
        $return = '<a rel="external" data-ajax="false" href="' . add_query_arg( 'am_force_theme_layout', 'desktop', $current_url ) . '" class="am-switch-btn godesktop">' . get_option( 'desktop_view_theme_link_text' ) . '</a>';
    } else {
        if ( !empty( $status ) || $desktop_switch_link == 'yes' ) {
            $return = '<a href="' . add_query_arg( 'am_force_theme_layout', 'mobile', $current_url ) . '" class="am-switch-btn gomobile">' . get_option( 'mobile_view_theme_link_text' ) . '</a>';
        }
    }

    return $return;
}
share|improve this answer
    
Brilliant - it's working perfectly - thanks! –  paradroid Nov 9 '12 at 19:14
    
Glad to hear, your quite welcome! –  Joseph Nov 9 '12 at 20:01

The code to display your current URL is:

<?php echo $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ?>

So, for your code example, this should work:

function show_theme_switch_link_func( $atts ){
    $desktopSwitchLink      = get_option('show_switch_link_for_desktop');
    global $shown_theme;
    global $status;
    $currentURL = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    if ($shown_theme){
            $return = '<a rel="external" data-ajax="false" href="'.$currentURL.'?am_force_theme_layout=desktop" class="am-switch-btn godesktop">'.get_option('desktop_view_theme_link_text').'</a>';
    } else {
            if ((!empty($status)) || ($desktopSwitchLink == 'yes')){
                    $return = '<a href="'.$currentURL.'?am_force_theme_layout=mobile" class="am-switch-btn gomobile">'.get_option('mobile_view_theme_link_text').'</a>';
            }
    }
    return $return;
}

Try that and see what happens...

share|improve this answer

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.