Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress How to Make a Website with WordPress Customizing WordPress Themes How to Make Child Themes

Jean Bosio
Jean Bosio
1,048 Points

@import no longer best practice in setting up child theme style.css?

Hi.

I’m setting up a child (Create) theme and I notice best practice for calling the parent css content in the style.css file has changed fromt @import to using wp_enqueue_scripts and wp_enqueue_style in the child function-php file. Is it so that every css/js file used in the parent has to be called upon from the child function.php file?

I.e. before this was enough:

@import "../themetrust-create/style.css";

but now this is not, apparently:

https://codex.wordpress.org/Child_Themes

Note that the previous method was to import the parent theme stylesheet using @import: this is no longer best practice. The correct method of enqueuing the parent theme stylesheet is to add a wp_enqueue_scripts action and use wp_enqueue_style() in your child theme's functions.php.

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

}

So, do I need to use a similar code like this, for all js/cs files that the child theme uses, even though they are in the parent setup?

function swell_scripts_n_styles() {

global $wp_version;

// 2.1.1 CSS
wp_enqueue_style( 'swell-style', get_stylesheet_uri(), false );
wp_enqueue_style( 'swell-mmenu-css', get_bloginfo('template_url').'/css/jquery.mmenu.css', false, '4.3.2', 'all' );
wp_enqueue_style( 'superfish', get_bloginfo('template_url').'/css/superfish.css', false, '1.7.3', 'all' );
wp_enqueue_style( 'swell-YTVideo', get_bloginfo( 'template_url' ).'/css/YTPlayer.css', false );

// 2.1.2 Fonts
wp_enqueue_style( 'font-awesome', get_bloginfo( 'template_url' ) . '/css/font-awesome.min.css', false, '4.0.3', 'all' );
wp_enqueue_style( 'font-merriweather', '//fonts.googleapis.com/css?family=Merriweather:300,400,700,300italic,400italic,700italic', false );
wp_enqueue_style( 'font-open-sans', '//fonts.googleapis.com/css?family=Open+Sans:300,400,700,300italic,400italic,700italic', false );
wp_enqueue_style( 'flexslider', get_bloginfo( 'template_url' ) . '/css/flexslider.css', false );

// 2.1.3 Scripts
wp_enqueue_script( 'swell-jquery-ui', '//code.jquery.com/ui/1.10.4/jquery-ui.min.js', array( 'jquery' ), '1.10.4', true );
wp_enqueue_script( 'swell-jquery-actual', get_bloginfo( 'template_url' ).'/js/jquery.actual.js', array( 'jquery' ), '1.0.16', true );
wp_enqueue_script( 'swell-flexslider', get_bloginfo( 'template_url' ).'/js/jquery.flexslider-min.js', array( 'jquery' ), '2.2.2', true);

// Video
wp_enqueue_script( 'swell-YTVideo', get_bloginfo( 'template_url' ).'/js/jquery.mb.YTPlayer.js', array( 'jquery' ), '0.10', true );
wp_enqueue_script( 'swell-froogaloop', 'http://a.vimeocdn.com/js/froogaloop2.min.js', array( 'jquery' ), null, true );

// Others
wp_enqueue_script( 'superfish', get_bloginfo('template_url').'/js/superfish.js', array('jquery'), '1.7.3', true );
wp_enqueue_script( 'swell-scrollto', get_bloginfo( 'template_url' ) . '/js/jquery.scrollTo.min.js', array( 'jquery' ), '1.4.6', true );
wp_enqueue_script( 'swell-isotope', get_bloginfo( 'template_url' ) . '/js/jquery.isotope.js', array( 'jquery' ), '1.3.110525', true );
wp_enqueue_script( 'swell-fitvids', get_bloginfo( 'template_url' ) . '/js/jquery.fitvids.js', array( 'jquery' ), '1.0', true );
wp_enqueue_script( 'swell-mmenu', get_bloginfo( 'template_url' ) . '/js/jquery.mmenu.min.js', array( 'jquery' ), '4.3.2', true);
wp_enqueue_script( 'swell-theme_trust_js', get_bloginfo( 'template_url' ) . '/js/theme_trust.js', array( 'jquery' ), '1.0', true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
if( is_active_widget( false, '', 'ttrust_flickr' ) ) {
wp_enqueue_script( 'flickrfeed', get_bloginfo( 'template_url' ) . '/js/jflickrfeed.js', array( 'jquery' ), '0.8', true);
}

}

Thanks :-)

Hi Jean! I had the similar problem. I was able to refer to a different video that showed me that all I had to do was add the PHP tags in front of the code that Codex gives to put in the functions.php file. Here's some step by step instructions for you and anyone else who is trying to create a Child Theme page for their WordPress site:

Go to Workspaces which is the fifth icon on the left side of the Treehouse website. Click on New to create a new workspace and title it twentythirteenchild-directory. Create a new file and title it style.css. In this folder, copy and paste the code below:

/*
 Theme Name:   Twenty Fifteen Child
 Theme URI:    http://example.com/twenty-fifteen-child/
 Description:  Twenty Fifteen Child Theme
 Author:       John Doe
 Author URI:   http://example.com
 Template:     twentyfifteen
 Version:      1.0.0
 License:      GNU General Public License v2 or later
 License URI:  http://www.gnu.org/licenses/gpl-2.0.html
 Tags:         light, dark, two-columns, right-sidebar, responsive-layout, accessibility-ready
 Text Domain:  twenty-fifteen-child
*/

Replace the example text with the text that matches your site.

Instead of using the @import rule as discussed in the video, you will need to follow these instructions provided by codex.

Codex explains that you must create a separate file called fuctions.php. Create this file in your twentythirteenchild-directoy and then copy and paste this code in the functions.php:

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

}

-If this does not work, then you have to paste this one in the functions.php file:

function theme_enqueue_styles() {

    $parent_style = 'parent-style';

    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style )
    );
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );

What the code in the functions.php file does is it goes into your child theme and loads the stylesheet from the parent theme so that we continue to have the same styling inside of the child theme. -If the first PHP code does not work, you need to use the second one listed above. -If you have chosen a WordPress theme, and not a theme from another website or company, the first code is the one that will most likely work. Otherwise the second PHP code will work.

Once you’ve copied and pasted the functions.php code, you must be sure to add the PHP tags surrounding the WordPress action. This is what the code will look like with added PHP tags:

<?php

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

}

?>

One important thing to note, do not include additional lines after the closing tag (end on line 9 or line 8) because this could lead to breaking your WordPress site.

After creating the files, download the files from the workspace and unzip the files in your download section (like you did when you downloaded WordPress). Within the unzipped file, copy both the style.css and functions.php files and paste them into the twentythirteenchild folder located inside of wordpress>wp-content>themes>twentythirteenchild.

Return to FileZilla. Be sure the Remote Site box still has the public_html folder opened and go to wordpress>wp-content>themes. Click on themes so the folders appear in the lower right hand box. Drag the twentythirteenchild folder into the lower right hand box with all the other theme folders. When doing so, the folder will be transferred to the server of your website.

To check and see if it was transferred correctly, go back into the dashboard of your WordPress site, go to Appearance then Themes, and you should now have a new theme called Twenty Thirteen Child.

Activate Twenty Thirteen Child theme and then click on Visit Site underneath the title of your WordPress Website to preview the page.

The page should look identical to the parent theme (twentythirteen). This is because, with the child theme code we added in the style.css file, we added the HTML code and the PHP code using our Template code (Template: twentythirteen). In the PHP file, we added the code for the styling of the page.

Although the child theme we’ve activated is identical to the parent theme, we can take the child theme and do whatever we want with it inside of the code. If you make any mistakes in the child theme, you can always refer back to the parent theme.

Hope this helps you! Feel free to ask me any questions or if you need any clarifications on this process.

Cheers, Laurie

2 Answers

Jean Bosio
Jean Bosio
1,048 Points

Thanks for your reply, Laurie.

I did read the codex and understood it, however I was looking for confirmation on how to handle all other theme css (and javascript files) besides the style.css theme, with the new wp_enqueue functionality.

If I understand this correctly, using the @import function does not prevent/interfer with the usage of all other parent css/js files, whereas using the new wp_enqueue functionality you have to provide a link to all css/js files used in the child theme, which means all js/css files used in the parent theme as well - an example of which I pasted last in my prev post.

So, I was also wondering if, when using complex themes, using @import might still be a valid option?

Br, Jean

Patrick Boehner
Patrick Boehner
2,005 Points

Hi Jean,

The @import stylesheet function to bring in your paren't themes css file will still work. It is still perfectly valid even if not the recommended method.

We can set aside issues with JS at the moment since the way in which this video is using @import is as a css rule (though other languages use a similar function) and that is obviously only dealing with css.

The only sense in which it would interfere with your paren't theme's main css file is in the descending nature of css. If you import your paren't theme's css file at the top of your child theme's css file then you can write selectors that override/modify the selector found in your parent theme's css file. Thats basic css. But that might not have been how you meant your question.

Using it wont prevent you using your paren't theme's main style sheet, and in this case we are only talking about the parent theme's main style sheet. All other styles (handled by the hook, wp_enqueue_style) and scripts (handled by the hook wp_enqueue_script) coming from your paren't theme will still apply. You do not need to manually add theme to your child theme's style sheet or functions.php file. The only time you would do that in your child theme's functions.php file is if your adding additional js or css files unique to your child theme, or dequeueing (using wp_dequeue_script) a script or style coming from your paren't theme or wordpress itself.

So say for example your parent theme loads a separate css file for a custom font (like Font Awesome) and a separate JS file to add support for controlling video (like fitvid.js). Those same files will still load when your child theme is active without you having to queue them. If you use your browsers web inspector you can look at the css and js files being loaded into the header of your website. Those example files would still be there after activating your child theme.

Part of the confusion might be from the codex's child theme instructions, where it says:

The following example function will only work if your Parent Theme uses only one main style.css to hold all of the css. If your theme has more than one .css file (eg. ie.css, style.css, main.css) then you will have to make sure to maintain all of the Parent Theme dependencies.

This only applies if your paren't theme's main style.css file has been broken up into multiple peaces (it usually isn't). The css files you pointed out in your first post aren't part of the paren't theme's main style sheet. They are separate dependancies and you don't need to worry about loading them in this circumstance.

Hope that answers your question.

Patrick

Jean Bosio
Jean Bosio
1,048 Points

Thanks Patrick! The issue did get clearer :-)