• Hi

    I need to add the functions.php file to my child theme. I have been reading how to do it but I’m still not quite getting it and the code I am adding is causing blank pages to load. What I am trying to do is alter the existing viewport meta tag. This is what I want to change it to: <meta name=”viewport” content=”width=device-width, initial-scale=1.0″ />

    Below is the exact code I have added to the functions.php file in my child theme folder. I don’t know what I am doing wrong. I must be missing something. Thanks for any help ??

    <?php
    
    function et_add_viewport_meta(){
    echo '<meta name="viewport" content="width=device-width, initial-scale=1.0" />';
    }
    add_action( 'wp_head', 'et_add_viewport_meta' );
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Welcome to the forums!

    There’s nothing wrong syntactically with the snippet you posted. The cause of the blank screen lies elsewhere. You should get error messages telling you what the problem is by defining WP_DEBUG as true in wp-config.php.

    Even with no errors and correct syntax, your code will not have the effect you desire. You will be adding a second meta tag, not altering the current one. How you alter the current one is theme dependent. If it simply exists on the parent theme’s header.php, copy this file to your child theme and directly alter the HTML on your child’s version.

    If it is inserted by PHP somehow, how to alter it depends on exactly what the PHP looks like. In this case, you should seek help through the theme’s dedicated support forum.

    Thread Starter Mary

    (@ilhaverde)

    Thanks, I’ll check for errors. The meta tag line is in the parent theme functions.php file. When I alter it, it does change on the front end. That’s why I thought adding the functions.php file to child theme would over-ride the line in parent theme functions.php.

    Moderator bcworkz

    (@bcworkz)

    I see. How one overrides parent functions depends on how the function is configured. If it has a filter, that should be used. If it is a “pluggable” function, you can redefine it using the same name. Identify pluggable functions by them having something close to if ( ! function_exists( 'some_functions_name' ) ) : before the function declaration.

    If the parent code itself is adding an action callback, it should be removed in addition to your OP code. The code to remove the parent callback must be executed after the callback was added, which means code removing it must itself be an action callback, this time hook “after_theme_setup”.

    If the parent utilizes none of those, your only option is to copy the header.php file to your child and alter the code calling the parent meta tag function to call your code instead, or to have the template directly output the desired tag.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding functions.php file to child theme’ is closed to new replies.