How to set child theme path - wordpress

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;
}

Related

Wordpress apply color scheme by category name

Is there a simple way I could apply different color scheme css based on category names in wordpress? For example I have a green.css which changes h2 and block color, I want to apply this when category = green. Is there a solution or addon for this? I'm using multisite.
Hopefully this does what you want:
Without knowing exactly how your theme is set up, you can add something like the following to your theme's functions.php file:
function load_css_theme(){
$categories = get_the_category();
if (!empty($categories)) {
foreach($categories as $c) {
if ($c->name === 'green') {
wp_enqueue_style('style', get_template_directory_uri().'/green.css');
}
}
}
}
add_action('wp_head', 'load_css_theme');
It will check the categories for the current page; if the category "green" is found, it will enqueue the green.css file. (This assumes the green.css file is in the root of the theme folder.) You can add more themes by adding more "if" statements like the example, just changing the category and the css filename.

Wordpress 4.4: Change max_srcset_image_width in media.php from functions.php

Wordpress defines the maximum resolution included in the new srcset attribute in the media.php with the following line:
$max_srcset_image_width = apply_filters( 'max_srcset_image_width', 1600, $size_array );
Is there any way to change that value (via hook etc) from my themes functions.php? Right now I changed it in the media.php itself (which is not recommended – I know...).
I had success adding this to my functions.php file
function new_srcset_max($max_width) {
return 2000;
}
add_filter('max_srcset_image_width', 'new_srcset_max');

Replacing only parts of an archive and single page template of WordPress

I am having a bit of trouble here understanding how to do the following. I have searched for weeks now but cannot seem to find what I am looking for.
I have a custom post type 'product' and want to change which template gets loaded for the single product page as well as the archive for the products. I am using the following code to load include and load templates.
add_filter('template_include', function() {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
if ('product' == get_post_type() ){
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
return $template;
}
});
The problem I am having is that it replaces the current theme's template instead of just the inner part of the content and archive areas.
Here is what I want to achieve:
Create a custom post type 'product' in a plugin - DONE (Was kinda easy!)
When opening a single product only change the content part. - I can do this with the_content filter hook. Simple enough. Any other suggestions is welcome.
When I go to the archive view for the 'product' custom post type I don't want to have it load the theme's default archive (list) view but instead a grid view from my plugin which I cannot seem to get right. I only want to change the inner part of the template, not the whole page.
I have created this plugin a few weeks ago using only shortcodes which works good but want to see if I can do it without the use of shortcodes by means of creating the custom post type and changing the inner template parts of the current active theme.
Can anybody steer me into the right direction here?
If I create a theme I can do what I am looking for but I want to create this into a plugin instead without adding or making changes to the active theme. The plugin should handle what is needed.
The same issue is discussed here but what I want is to develop something that is theme independent. No changes should be made in theme files and no theme files should be copied to the plugin.
WP - Use file in plugin directory as custom Page Template?
Recently I also had the same problem. Here's how I worked it out.
template_include filter accepts a parameter which is the selected template that you want to override (this what you are missing in your code).
I don't know but sometimes the filter hook need higher priority to work like 9999. But first check if it work with default priority, if don't change it.
I assume your both archive and single product template both have include get_header() and get_footer() which can be used for default selected theme (Or if the theme has different setup, setup accordingly).
This is simplified code:
add_filter('template_include', function($default_template) {
if (is_post_type_archive('product')) {
$templatefilename = 'archive-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
} else if ('product' == get_post_type() ) {
$templatefilename = 'single-product.php';
$template = WPVS_PATH . 'templates/' . $templatefilename;
$default_template = $template;
}
// Load new template also fallback if both condition fails load default
return $default_template;
}, 9999); // set priority, only if not worked with default one
The best option in this case is to provide a shortcode to the user. So they can place it on any page that they want (or that you auto generate). That way you will place your content inside their theme.
Something like this:
add_shortcode( 'slotsl-game', 'embed_game' );
/**
* Print the game
* #return false|string
*/
function embed_game(){
ob_start();
$game = get_post();
include_once SLOTSL_PLUGIN_DIR . 'templates/slotsl-single-game.php';
return ob_get_clean();
}

Including from child theme root or else include from parent theme root

at the moment I am working on a Wordpress theme with a functions.php that will include multiple helper functions files. I want this theme to be very flexible and customizable if a child theme is made based on the theme.
I have this piece of code currently in my functions.php:
add_action( 'template_redirect', 'wpf_contact' );
/**
* Include the logic/settings for tpl-contact.php.
*
* Includes the logic/settings for tpl-contact.php. It will load the child's
* mail.inc.php first if that is available.
*/
if ( ! function_exists( 'wpf_contact' ) ) {
function wpf_contact() {
if ( is_page_template( 'tpl-contact.php' ) && is_child_theme() && file_exists( get_stylesheet_directory() . '/inc/wpf/mail.inc.php' ) ) {
include( get_stylesheet_directory() . '/inc/wpf/mail.inc.php' );
} else {
include( get_template_directory() . '/inc/wpf/mail.inc.php');
}
} // end wpf_contact()
}
What the code above does is load mail.inc.php from child if the files exist. If it doesn't load it from the parent theme.
Cause I am having multiple files I want to load like that. I was thinking it there might not be a easier way of doing this? Is there a built in function that does this in WordPress?
Or should I just adjust the above function and add parameter's so it can be used for other files as well? Not sure if this is efficient.
To answer your question directly based on your sample, i would not check to see if the file has been placed in the child theme. If a child-theme developer has customized the file, they can simply declare the function and include their own file (wherever they place it). There is no risk of code duplication because your sample function is simple enough.
Even if you simplified your code for the purposes of placing it here....you can always keep includes wrapped in a very simple function that can be declared by a child theme. This will keep PHP running efficient and not having to check for files all the time.

Having issue with WordPress wp_enqueue_style

I am building a full design into WordPress for the first time and I am trying to load in stylesheets and script files but all I seem to be getting is the text output of the location.
What I have is below..
wp_enqueue_style('reset', bloginfo('template_url') . '/reset.css');
wp_enqueue_style('style', bloginfo('stylesheet_url'), array('reset'));
wp_enqueue_style('rhino', bloginfo('template_url') . '/rhinoslider-1.05.css', array('reset','style'));
Do I need to put this inside the link tags or something? I thought it would do it all itself; as what's the point loading it that way if it doesn't do it itself? I know it makes sure the same file isn't included twice or something, but if you have to include the link tags yourself and then WP decides not to include the file then you are left with blank link tags!?
Lastly, should I set these up beforehand so I can just call them via their handles? If so, where? functions.php?
Edit: I also tried putting the below in my themes functions.php file but got the same results.
add_action( 'after_setup_theme', 'mmw_new_theme_setup' );
function mmw_new_theme_setup() {
/* Add theme support for post formats. */
add_theme_support( 'post-formats' );
/* Add theme support for post thumbnails. */
add_theme_support( 'post-thumbnails' );
/* Add theme support for automatic feed links. */
add_theme_support( 'automatic-feed-links' );
/* Add theme support for menus. */
add_theme_support( 'menus' );
/* Load style files on the 'wp_enqueue_scripts' action hook. */
add_action( 'wp_enqueue_scripts', 'mmw_new_load_styles' );
}
function mmw_new_load_styles() {
$foo = bloginfo('template_url') . '/reset.css';
$bar = bloginfo('template_url') . '/rhinoslider-1.05.css';
wp_enqueue_style('reset', $foo);
wp_enqueue_style('style', bloginfo('stylesheet_url'), array('reset'));
wp_enqueue_style('rhino', $bar, array('reset','style'));
}
When storing values in a variable via PHP use:
get_bloginfo()
So your new function would now look like:
function mmw_new_load_styles() {
$foo = get_bloginfo('template_url') . '/reset.css';
$bar = get_bloginfo('template_url') . '/rhinoslider-1.05.css';
wp_enqueue_style('reset', $foo);
wp_enqueue_style('style', bloginfo('stylesheet_url'), array('reset'));
wp_enqueue_style('rhino', $bar, array('reset','style'));
}
And be more semantic! It makes code for beginners easier to look at. ($foo could be $resetCssUrl)
I was having similar issues.
The register / enqueue scripts are so that you can globally assign your functions to load in the correct order. You can call them from the page that your working in but it is considered better practice do it this way.
My template has a functions.php but its nealry empty! It sepreates the scripts into 7 subchapters, theme-options, theme-functions, themes-js, etc. Here is my themes.js.php file but this could quite easily placed in your file inside your wp-content/themes/functions.php My themes-js.php file

Resources