• HYello i need to insert code in the head section and iht body section (tracking google code and similar)

    I don’t want to use a plugin and i want to write straigh tin thephp file sof my theme

    I have created a child theme…but don’t know how to go on

    I have tried different tutorial form google , iset codes infunction-php but the site break down

    Any help?thanks

Viewing 15 replies - 1 through 15 (of 24 total)
  • Hallo @stefanocps,

    you can use the hooks: wp_head and wp_footer and .wp_body_open
    You can add this Code to your functions.php in your Child Theme.
    For example:

    Header

    add_action('wp_head', function() {
    // Your HTML Code / Example
    ?>
    <script type="text/javascript" src="https://ww.wp.xz.cn/your-files.js"></script>
    <?php
    });

    Footer

    add_action('wp_footer', function() {
    // Your HTML Code / Example
    ?>
    <script type="text/javascript" src="https://ww.wp.xz.cn/your-files.js"></script>
    <?php
    });

    Body

    add_action( 'wp_body_open', function() {
    ?>
    <script>// Your script code here</script><
    ?php});

    Thread Starter stefanocps

    (@stefanocps)

    i tried the code for body and get error

    i have added in the child function.php

    i think it is something related to hte <?php at the end and to the <? at the beginning?

    ythe error is either with heder and body

    • This reply was modified 2 years, 1 month ago by stefanocps.
    • This reply was modified 2 years, 1 month ago by stefanocps.

    @stefanocps Do you can add you code from the functions.php here ? Than I can help you 🙂

    Make sure that the beginning of the functions.php is beginning with <?php and than to add and customize your Code.

    Thread Starter stefanocps

    (@stefanocps)

    this is the funcion.php in child theme, where i am adding the code

    <?php
    
    // Exit if accessed directly
    
    if ( !defined( 'ABSPATH' ) ) exit;
    
    // BEGIN ENQUEUE PARENT ACTION
    
    // AUTO GENERATED - Do not modify or remove comment markers above or below:
    
    if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
    
        function chld_thm_cfg_locale_css( $uri ){
    
            if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
    
                $uri = get_template_directory_uri() . '/rtl.css';
    
            return $uri;
    
        }
    
    endif;
    
    add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
    
    if ( !function_exists( 'child_theme_configurator_css' ) ):
    
        function child_theme_configurator_css() {
    
            wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'hello-elementor','hello-elementor','hello-elementor-theme-style','hello-elementor-header-footer' ) );
    
        }
    
    endif;
    
    add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 10 );
    
    // END ENQUEUE PARENT ACTION

    @stefanocps Try this 🙂 In the line: if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . ‘/rtl.css’ ) ) {

    Was mission the beginning and ending PHP Tags.

    <?php
    
    // Exit if accessed directly
    
    if ( !defined( 'ABSPATH' ) ) exit;
    
    // BEGIN ENQUEUE PARENT ACTION
    
    // AUTO GENERATED - Do not modify or remove comment markers above or below:
    
    if ( !function_exists( 'chld_thm_cfg_locale_css' ) ) {
    
        function chld_thm_cfg_locale_css( $uri ){
    
            if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) ) {
    
        $uri = get_template_directory_uri() . '/rtl.css';
    
            }
    
           return $uri;
        }
    
    }
    add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
    
    if ( !function_exists( 'child_theme_configurator_css' ) ) {
    
       function child_theme_configurator_css() {
    
            wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'hello-elementor','hello-elementor','hello-elementor-theme-style','hello-elementor-header-footer' ) );
    
        }
    
    }
    add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 10 );
    
    // END ENQUEUE PARENT ACTION
    
    // ======== ADD YOUR CODE HERE (FOR EXAMPLE) ======== 
    add_action('wp_head', function() {
    // Your HTML Code / Example
    ?>
    <script type="text/javascript" src="https://ww.wp.xz.cn/your-files.js"></script>
    <?php
    });
    
    
    
    
    

    Thread Starter stefanocps

    (@stefanocps)

    i copied just this code to the function.php (did not add any script yet and i get again error

    without the code you wrote it works

    • This reply was modified 2 years, 1 month ago by stefanocps.

    Do you can send me the error message or copy it from the debug.log if it’s active. Which parent theme you use ?

    Thread Starter stefanocps

    (@stefanocps)

    hello elementor

    don t know where to get th elog

    Ok, in the Pro Version of Elementor you can add Custom Header / Footer Code inside the settings.

    The debug.log you can find under /wp-content/debug.log
    See this documentation: https://developer.ww.wp.xz.cn/advanced-administration/debug/debug-wordpress/

    Thread Starter stefanocps

    (@stefanocps)

    but i don t have tge pro version

    That’s no problem 🙂 The debug.log is one of the part of WordPress.

    The Pro Version allows you to add code to WP Head and Footer from the Backend. But first I need to see the debug.log or the error message. Than I can help you.

    Thread Starter stefanocps

    (@stefanocps)

    please help me

    i have added this in wp-config.php

    // Enable Debug logging to the /wp-content/debug.log file

    define( ‘WP_DEBUG_LOG’, true );

    but don t get any log

    • This reply was modified 2 years, 1 month ago by stefanocps.
    • This reply was modified 2 years, 1 month ago by stefanocps.
    Thread Starter stefanocps

    (@stefanocps)

    i got this

    Parse error: syntax error, unexpected token “<” in /home/greenartcoin/domains/greenartcoin.eu/public_html/wp-content/themes/hello-elementor-child/functions.php on line 42

    Ok but where you saw the error after you added the code in the functions.php?

    Thread Starter stefanocps

    (@stefanocps)

    this if i only leave the plain code you gave me(no script)

    Warning: Constant WP_DEBUG_LOG already defined in /home/greenartcoin/domains/greenartcoin.eu/public_html/wp-config.php on line 97

    Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/greenartcoin/domains/greenartcoin.eu/public_html/wp-includes/formatting.php on line 4494

Viewing 15 replies - 1 through 15 (of 24 total)

The topic ‘insert code in header and body’ is closed to new replies.