Kaur Kaelep
Kaur Kaelep

Reputation: 111

Wordpress changing themes based on user agent

I've created two seperate themes. One for mobile, one for desktop. I've managed to change the theme with the function shown below. Changing the theme works, but the function still loads the CSS, HTML, JS and PHP code from the original and main theme.

Both the desktop and mobile theme have the same file structure and linking.

The folder structure is as follows

project
      |
      wp_content
               |
               themes
                    |
                    desktop
                    mobile
function change_theme($current_theme)
{
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    if (strpos($user_agent, 'CostumizedAgent') !== false) {
        return 'mobile';
    } else {
        return $current_theme;
    }
}
add_filter('stylesheet', 'change_theme');
add_filter('template', 'change_theme');

I'd expect to load the themes actual files, not the files from the main theme. Any suggestions?

Upvotes: 0

Views: 304

Answers (1)

Varsha Dhadge
Varsha Dhadge

Reputation: 1751

Try this. wp_is_mobile() function helps to detect device

add_filter('body_class','mobile_theme_body_class');     
function mobile_theme_body_class( $classes ){
    if ( wp_is_mobile() ){
        return 'mobile';
    } else {
        return 'desktop';
    }
    return $classes;
}

Upvotes: 1

Related Questions