1

In https://codex.wordpress.org/Child_Themes they have given 2 different enqueue codes, what's the difference between them?

This is the first one

<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );

}
?>

This is the second one

<?php
function my_theme_enqueue_styles() {

    $parent_style = 'parent-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.

    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 ),
        wp_get_theme()->get('Version')
    );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>

Whats the difference between the two and which one should be used and would this code work across all the wordpress themes ?

Second doubt I have is about a rtl.css file. I have this file name rtl.css in my theme. Do I need to enqueue rtl.css too using

 wp_enqueue_style( $parent_style, get_template_directory_uri() . '/rtl.css' );

After adding rtl.css in the child theme directory

To support RTL languages, add rtl.css file to your child theme, containing:

/*
Theme Name: Twenty Fourteen Child
Template: twentyfourteen
*/

1 Answer 1

1

The first function should be there in your parent theme functions.php file.

Parent Theme Code

<?php
if ( ! function_exists( 'my_theme_enqueue_scripts' ) ) {
    function my_theme_enqueue_scripts() {
    add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
        function my_theme_enqueue_styles() {
            wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    }
}
?>

Child Theme Code

Now place the following code in your child theme. Setting 'parent-style' as a dependency will ensure that the child theme stylesheet loads after it.

if ( ! function_exists( 'my_theme_enqueue_scripts' ) ) {
    function my_theme_enqueue_scripts() {
        wp_enqueue_style( 'my_parent_style', get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'my_child_style',
        get_stylesheet_directory_uri() . '/style.css',
            array('my_parent_style')
        );
        add_action( 'wp_enqueue_scripts','my_theme_enqueue_scripts' );
    }
}

Hope it's clear.

2nd Doubt It's recommended to add the rtl.css file to your child theme even if the parent theme has no rtl.css file. rtl.css is only loaded by WordPress if is_rtl() returns true.

2
  • I read some tutorials online it doesnt say you have to add the code in the function.php of parent theme but only child theme. Will it not work if i dont add the first code inside the functions.php of the parent theme.
    – user110681
    Commented Jan 11, 2017 at 19:00
  • It works without adding the first code in the parent theme but it's the correct way to enqueue the style in the parent theme.
    – Maqk
    Commented Jan 12, 2017 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.