How can I use multiple themes for buddypress mobile, I want to switch quickly between those themes. This is for the purpose of creating styles and testing theme. Also comparing those themes.
Create a file /wp-content/sunrise.php. There you can write smth like this:
add_filter('option_template', 'filter_get_option');
add_filter('option_stylesheet', 'filter_get_option');
function filter_get_option($value){
// here you can based on url or any other thing
// change the theme on a fly, like this:
if(true){
$value = 'theme_1';
}else{
$value = 'theme_2';
}
return $value;
}
This is rather like a hack, but it works. theme_1 and theme_1 are themes folders (those inside /wp-content/themes/
Related
I am using a theme that has used get_template_part() to load certain areas of the theme. I want to override some areas with an alternate content. For example, my theme has this line in header.php.
get_template_part('parts/header/header.php');
I want to change parts/header/header.php with something else like <p>New Content</p>. I am trying to do this using a filter.
add_filter('get_template_part', 'xyz3021x');
function xyz3021x($part) {
if($part === 'template-parts/header/header') return '<p>New Content</p>';
}
But this is not working. I am still getting the content of parts/header/header.php. How can this be done?
I am trying to set the child theme path. Instead of having child theme templates loaded from the standard location I want to load them from a different location.
For example I have themes:
wp-content/themes/twentyfifteen
wp-content/themes/twentyfifteen_child
where twentyfifteen_child has style.css and functions.php along with any override templates. This is working fine, but I want to programatically set the child theme path to be this instead:
wp-content/themes/twentyfifteen_child/twentyfifteen_grandchild
Is this possible?
I just figured out the answer although there might be a better way of doing this. I tried the following in functions.php and that didn't work. But if I add the same thing in a plugin it works.
add_filter( 'stylesheet_directory', 'custom_stylesheet_directory' );
function custom_stylesheet_directory($dir)
{
$project = $dmmEnv->getProject();
$dir = str_replace(
'twentyfifteen_child' ,
'twentyfifteen_child/twentyfifteen_grandchild', $dir);
return $dir;
}
Right now I'm trying to switch the template that Wordpress uses depending on the device that is viewing the site.
The exact issue here is that the ONLY thing that seems to be switching are the scripts and stylesheets. The actual templates them selves (index, header, footer) stay the same.
Here is the function I'm using to do this:
<?
function fxn_change_theme($device) {
$header = $_SERVER['HTTP_X_UA_DEVICE'];
if ($header === 'mobile') {
$theme = 'jankness-mobile';
} elseif ($header === 'tablet') {
$theme = 'jankness-tablet';
} else {
$theme = 'jankness-desktop';
}
return $theme;
}
add_filter('template', 'fxn_change_theme');
add_filter('option_template', 'fxn_change_theme');
add_filter('option_stylesheet', 'fxn_change_theme');
?>
Also, the only filter that's doing anything seems to be 'template', the option filters don't do much. I've tried looking up what they do and it's not clear to me at the moment.
What might be the issue here?
Basically, since the code I was using was sitting in functions.php it was not able to manipulate the theme content earlier enough in the Wordpress core process. So the solution was to move the code into a very simple plugin exactly as it is, and hook in to the theme_setup point using the same code I used here.
I'm currently coding a WP plugin and would need to override templates.
My filter hook looks like that - and it executes:
add_filter('template_include', 'mcd_set_template',10);
function mcd_set_template() just returns the required path as string - or the default WP template in case the file not exists.
I'm toying with this for hours already, even could include that alternate template (but it appears at the bottom of the page).
So my question is, how to force WP 3.2.1 to just load another template file instead - and which priority is required??
Update:
Also I noticed when using var_dump ... it outputs almost at the end of the file - but should appear before the opening HTML tag...
According to this ticket it should work with template_include hook: http://core.trac.wordpress.org/ticket/11242
Or is the only way to hook these filters instead:
http://codex.wordpress.org/Template_Hierarchy#Filter_Hierarchy
?
You could use template_redirect as shown above, but that does require exit, and it does trample on everything else WordPress would normally do to find the current template. You may want to let that happen and then apply logic to the current template.
Using some of what is above...
add_action('template_include', 'mcd_set_template');
function mcd_set_template() {
return locate_template('templatename.php');
}
That is fairly simple, you can also pass an array to locate_template() to define a hierarchy. If you were to use 'template_redirect as shown above, you should still be using locate_template, and this is how.
add_action('template_redirect', 'mcd_set_template');
function mcd_set_template() {
/**
* Order of templates in this array reflect their hierarchy.
* You'll want to have fallbacks like index.php in case yours is not found.
*/
$templates = array('templatename.php', 'othertemplate.php', 'index.php');
/**
* The first param will be prefixed to '_template' to create a filter
* The second will be passed to locate_template and loaded.
*/
include( get_query_template('mcd', $templates) );
exit;
}
Finally, the best way would be to filter specific types instead of the whole hierarchy. For example you could filter 'category_template' or 'page_template'. That would be more specific, it would avoid messing with the whole template hierarchy if you don't want to - and it lets WordPress do more of the heavy lifting
For example:
add_filter('category_template', 'filter_category_template');
function filter_category_template($template){
/* Get current category */
$category = get_queried_object();
/* Create hierarchical list of desired templates */
$templates = array (
'category.php',
'custom-category-template.php',
'category-{$category->slug}.php',
'category-{$category->term_id}.php',
'index.php'
);
return locate_template($templates);
}
You can of course create that array of hierarchical templates any time you use locate_template(). Using this method, its easy to see how easily you could create all sorts of very detailed and specific hierarchies either as part of or separate from the native Template Hierarchy.
Have you tried using an add_action instead? For example, you might want to try something like the following in your plugin:
add_action('template_redirect', 'mcd_set_template');
//Redirect to a preferred template.
function mcd_set_template() {
$template_path = TEMPLATEPATH . '/' . "templatename.php";
if(file_exists($template_path)){
include($template_path);
exit;
}
}
Here is a helpful reference: http://www.mihaivalentin.com/wordpress-tutorial-load-the-template-you-want-with-template_redirect/
Within the context of a module, how can you determine what theme is loaded for the current user?
drupal_get_path
path_to_theme
Neither of these are any good because they seem to only work in the template.php of a theme.
If users are allowed to select a theme for themselves, the theme they selected is saved in $user->theme, where $user is the user object.
The global variable $custom_theme contains the name of the theme currently set, if a module has set a custom theme.
The following snippet saves in $current_theme the name of the theme currently active:
global $custom_theme, $theme, $user;
if (!empty($user->theme)) {
$current_theme = $user->theme;
}
elseif (!empty($custom_theme)) {
$current_theme = $custom_theme;
}
else {
$current_theme = $theme ? $theme : variable_get('theme_default', 'garland');
}
path_to_theme should work just fine, I tested it on two Drupal installs, and both worked. If the theme has not been initialized yet, path_to_theme will do that, which is what Drupal uses internally to set different global theme variables like $theme_path, which is the variable you are looking for.