FacetWP / use-child-theme

A WordPress class to ensure that a child theme is installed and active
65 stars 21 forks source link

Multiple parent styles loaded #4

Closed justintadlock closed 8 years ago

justintadlock commented 8 years ago

For themes that enqeue the parent theme style, this code added to functions.php means that the parent theme's style.css file will be loaded twice:

function child_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'child_enqueue_styles' );

I'm not sure how you account for different methods that themes use to load styles though, so I don't have any brilliant ideas on how to best handle this.

mgibbs189 commented 8 years ago

@justintadlock Thanks. I could be totally off-base, but maybe there's a way to loop through all enqueued styles, and load the style.css only if parent-theme-dir/style.css isn't found...

webmandesign commented 8 years ago

Hi guys,

First, thanks for this helpful tool! Looks very promising.

I would suggest one improvement or start conversation about the best approach.

The theme developer should be able to override the generated child theme's style.css and functions.php file content, I think. That would basically resolve Justin's issue as well.

Now there's a question about how to approach this: we can do use filters on the style_css() and functions_php() methods, or, if you remove the new Use_Child_Theme(); line and leave it for the theme developers to initiate the class in the parent theme themselves, the theme developer could do something like this:

require_once( trailingslashit( get_template_directory() ) . 'use-child-theme.php' );

/**
 * Overriding Use_Child_Theme class
 */
class Parent_Use_Child_Theme extends Use_Child_Theme {

        /**
         * Declaring child theme `style.css` file content
         */
        function style_css() {
            $name = $this->theme->get( 'Name' ) . ' Child';
            $uri = $this->theme->get( 'ThemeURI' );
            $parent = $this->theme->get_stylesheet();

            return "<?php
/*
Theme Name: {$name}
Theme URI:  {$uri}
Template:   {$parent}
Author:     Author Name Here
Version:    1.0
*/
";
        } // /style_css

        /**
         * Declaring child theme `functions.php` file content
         */
        function functions_php() {
            return "<?php

// Parent theme already loads its stylesheet, so we don't need to enqueue it here

";
        } // /functions_php

} // /Parent_Use_Child_Theme

/**
 * Initiate child theme creator
 */
function parent_child_theme_creator() {
    if ( is_admin() ) {
        new Parent_Use_Child_Theme();
    }
} // /parent_child_theme_creator

add_action( 'after_setup_theme', 'parent_child_theme_creator' );

This would be actually quite flexible approach.

What do you say?

Regards,

Oliver

mgibbs189 commented 8 years ago

@webmandesign Thanks for the suggestion.

Honestly, I like the first option (WP filters) better. With the latter option, I feel like most theme developers would be inclined to just modify use-child-theme.php directly, versus extending the class.

webmandesign commented 8 years ago

OK then. I've proceeded and created a pull request for you applying those filters and fixing some issues. See https://github.com/FacetWP/use-child-theme/pull/11

You can basically consider this whole issue resolved I think (what do you say @justintadlock ?)