• Hello,

    Can someone let me know if the code below is added correctly? And if not, what am I doing wrong. Thanks.

    <?php
    
    function lambert_child_enqueue_styles() {
        $parent_style = 'lambert-theme-style'; // This is 'lambert-style' for the Lambert theme.
        wp_enqueue_style ( 
        	$parent_style, 
        	get_template_directory_uri() . '/style.css', 
        	array(
        		'lambert-google-fonts', 
        		'lambert-plugins', 
        	), 
        	null 
        );
    
        wp_enqueue_style( 'lambert-child-style',
            get_stylesheet_directory_uri() . '/style.css',
            array( $parent_style, 'lambert-responsive' , 'lambert-custom'),
            wp_get_theme()->get('Version')
        );
    
        wp_enqueue_script( 'showhide.js',
        	get_stylesheet_directory_uri() . '/js/showhide.js'
    	);
    }
    add_action( 'wp_enqueue_scripts', 'lambert_child_enqueue_styles', 'my_theme_scripts_function' );
    • This topic was modified 4 years, 9 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 6 replies - 1 through 6 (of 6 total)
  • Looks good! I don’t see any issues. Are you currently running into any errors?

    Oh wait, I just noticed that you have an extra argument here:

    add_action( 'wp_enqueue_scripts', 'lambert_child_enqueue_styles', 'my_theme_scripts_function' );

    It should be

    add_action( 'wp_enqueue_scripts', 'lambert_child_enqueue_styles' )

    • This reply was modified 4 years, 9 months ago by Sergio.

    This may not apply to you, it depends on your parent theme and the dependencies that you need from it. But if you don’t want to worry about maintaining the parent theme’s dependencies on your child theme functions.php, then don’t enqueue it in your child-theme functions.php. Simply leave it as a dependency of lambert-child-style. This way, if at some point the parent theme adds or removes dependencies, you don’t have to add/remove them in your enqueue code. This, however, depends if the parent theme is already enqueuing its own style.css with its set of dependencies.

    One way to test this is to remove the enqueue for lambert-theme-style from your child theme’s functions.php. Then inspect your homepage’s code to see if the parent styles are still being called.

    Thread Starter MyWorldz

    (@myworldz)

    To be honest I am just learning WordPress so I’m not too confident yet in what I’m doing.

    The reason why I asked to check the code is because I read somewhere that if I want to use javascript or CSS and I’m using a child theme, which I am, it’s best to link to them in the function.php file.

    I’m trying to use jQyery’s show/hide on my site hence the showhide.js link in the function.php – this is why I’m asking to see if I’m doing it correctly.

    Once I get WordPress to link to the showhide.js file then I will test to see if my code on the page works.

    I updated the code as per your note above. Should this be correct now?

    <?php
    
    function lambert_child_enqueue_styles() {
        $parent_style = 'lambert-theme-style'; // This is 'lambert-style' for the Lambert theme.
        wp_enqueue_style ( 
        	$parent_style, 
        	get_template_directory_uri() . '/style.css', 
        	array(
        		'lambert-google-fonts', 
        		'lambert-plugins', 
        	), 
        	null 
        );
    
        wp_enqueue_style( 'lambert-child-style',
            get_stylesheet_directory_uri() . '/style.css',
            array( $parent_style, 'lambert-responsive' , 'lambert-custom'),
            wp_get_theme()->get('Version')
        );
    
        wp_enqueue_script( 'showhide.js',
        	get_stylesheet_directory_uri() . '/js/showhide.js'
    	);
    }
    add_action( 'wp_enqueue_scripts', 'lambert_child_enqueue_styles' );

    Thanks a lot.

    Thread Starter MyWorldz

    (@myworldz)

    Anyone?

    Hi @myworldz,
    Sorry forgot to check “Notify me of follow-up replies via email” and didn’t see your reply until now.

    Yes, that looks correct!

    ==OPTIONAL==
    What I meant on my previous reply, about not having to worry about the parent theme’s dependencies, is that you can modify your code to look like this:

    
    <?php
    
    function lambert_child_enqueue_styles() {
        $parent_style = 'lambert-theme-style'; // This is 'lambert-style' for the Lambert theme.   
        wp_enqueue_style( 'lambert-child-style',
            get_stylesheet_directory_uri() . '/style.css',
            array( $parent_style, 'lambert-responsive' , 'lambert-custom'),
            wp_get_theme()->get('Version')
        );
    
        wp_enqueue_script( 'showhide.js',
        	get_stylesheet_directory_uri() . '/js/showhide.js'
    	);
    }
    add_action( 'wp_enqueue_scripts', 'lambert_child_enqueue_styles' );

    I simply removed the parent enqueue code. It will still be called since you are adding it as a dependency on the child’s theme styles. You can try this, if after making the above change makes your site look weird you can simply undo it, but this way you don’t have to worry about the parent’s style dependencies in case the parent theme changes them later on.

    Hope this makes sense.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Please Review My functions.php Code’ is closed to new replies.