Skip to content

Instantly share code, notes, and snippets.

@guwordpressbrasil
Created October 16, 2013 19:01
Show Gist options
  • Save guwordpressbrasil/7012965 to your computer and use it in GitHub Desktop.
Save guwordpressbrasil/7012965 to your computer and use it in GitHub Desktop.
Plugin para mudar o tema de acordo com o Browser, usado para temas mobile
<?php
/*
Plugin Name: Browser-Based Themes
Plugin URI: http://code.kuederle.com/browserbasedthemes
Description: This plugin will serve a different theme to specific browsers.
Author: Oliver Kuederle
Author URI: http://www.kuederle.com/
Version: 1.0
*/
// Activate theme switching.
add_filter('template', 'serveBrowserBasedTheme');
add_filter('option_template', 'serveBrowserBasedTheme');
add_filter('option_stylesheet', 'serveBrowserBasedTheme');
/**
* This is the main function of the plug-in which switches to another
* theme based on the user agent.
*/
function serveBrowserBasedTheme($originalTheme) {
// Check if we have a valid theme.
$alternativeTheme = get_option("bbt_current_theme","");
$found = false;
foreach(get_themes() as $theme)
if($theme["Template"] == $alternativeTheme) {
$found = true;
break;
}
if(!$found)
return $originalTheme;
// Compare user agents.
$userAgents = explode("$|$",get_option("bbt_user_agents",""));
foreach($userAgents as $userAgent)
if(strlen($userAgent) > 0 && strpos($_SERVER['HTTP_USER_AGENT'], $userAgent) !== false)
return $alternativeTheme;
return $originalTheme;
}
// Add a configuration screen.
add_action('admin_menu','addBrowserBasedThemeMenu');
/**
* This function adds new adminsitration menus.
*/
function addBrowserBasedThemeMenu() {
add_plugins_page('Settings for the Browser-Based Themes Plugin', 'Browser-Based Themes Config', 'manage_options', 'browser-based-theme', 'displayBrowserBasedThemeOptions');
}
/**
* Renders the browser-based theme options page.
*/
function displayBrowserBasedThemeOptions() {
if (!current_user_can('manage_options')) {
wp_die( __('You do not have sufficient permissions to access this page.') );
}
// Get plugin options.
$currentTheme = get_option("bbt_current_theme","Default");
$userAgents = explode("$|$",get_option("bbt_user_agents",""));
// Were the options changed?
if(isset($_POST["bbt_submitted"]) && $_POST["bbt_submitted"] == "true") {
// Yes. Retrieve user input.
$currentTheme = $_POST["bbt_current_theme"];
$userAgents = explode("\n",$_POST["bbt_user_agents"]);
$agents = array();
foreach($userAgents as $userAgent) {
$agent = trim($userAgent);
if(strlen($agent) > 0)
$agents[] = $agent;
}
$userAgents = $agents;
// Save to database.
update_option("bbt_current_theme",$currentTheme);
update_option("bbt_user_agents",implode("$|$",$userAgents));
// Display success message.
echo '<div class="updated"><p><strong>Settings have been saved.</strong></p></div>';
}
?>
<div class="wrap">
<h2>Browser-Based Themes Plugin Options</h2>
<form method="POST" action="">
<p>
<label>User agent strings to look for (one per line):</label><br/>
<textarea name="bbt_user_agents" cols="40" rows="4"><?php
foreach($userAgents as $userAgent)
echo htmlspecialchars($userAgent)."\n";
?></textarea>
</p>
<p>
<label>Alternative theme to be displayed:</label>
<select name="bbt_current_theme"><?php
$themes = get_themes();
foreach($themes as $theme) {
echo '<option';
if($theme["Template"] == $currentTheme)
echo ' selected="selected"';
echo ' value="'.htmlspecialchars($theme["Template"]).'">'.htmlspecialchars($theme["Name"]).'</option>'."\n";
}
?></select>
</p>
<p class="submit">
<input type="hidden" name="bbt_submitted" value="true"/>
<input type="submit" name="Submit" class="button-primary" value="Save Changes" />
</p>
</form>
<p>For a list of user agents for mobile phones, look <a href="http://en.wikipedia.org/wiki/List_of_user_agents_for_mobile_phones">here</a>.</p>
</div>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment