• Resolved natasha006

    (@natasha006)


    Hello,

    It’s better to add any PHP snippets code on functions.php or copy files to the child theme and change them or it does not matter?
    on Oceanwp Documentation there are many useful articles about adds code through functions.php.

    for instance, now I will to add gtag.js google analytics code to header and as well as add <meta name="theme-color" content="#ff6600" /> before closing </head> tag on header.php
    How can they be added through functions.php?

    Thanks in advance

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    Copy the header.php file into the child theme and add the code in the head section.
    Or install Insert header and footer plugin and add the code in the head section using the plguin.

    Thread Starter natasha006

    (@natasha006)

    Thanks @apprimit
    After a new oceanwp update, should I replace any files that copied into the child theme with the new one?
    (Most files that I had changed on it are into the Partials folder)

    • This reply was modified 5 years, 11 months ago by natasha006.

    No need to replace the file. You can compare the files and if you think some new features added, then replace the files after copying your custom code in the new file.

    Thread Starter natasha006

    (@natasha006)

    Thanks dear @apprimit for your useful info

    Here is the code that combines both code (gtag,theme color) to use on the functions.php

    function ns_google_analytics() { ?>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/xxxxxxxxxxxxx"></script>
    <script>
      window. = .dataLayer || [];
      function gtag(){data.push(arguments);}
      gtag('js',Date());
    
      gtag('config', 'U-xxxxxxxxxx-x');
    </script>
    <meta name="theme-color" content="#000000" />
      <?php
      }
      
    add_action( 'wp_head', 'ns_google_analytics', 10 );
    • This reply was modified 5 years, 11 months ago by natasha006.

    Okay, no problem if you have added the code into the functions.php file of the child theme.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘add any code to functions.php on child theme’ is closed to new replies.