• Resolved ehtsham

    (@ehtsham)


    I am trying to insert code in head tag through mu_plugin. I don’t know why head tag and init is not updating by using add_action('wp_head', 'my_function') and add_action('init', 'my_function2'). My code example as following:

    <?php
        class some_code
        {
            public
            function __construct()
            {
                add_action('init', 'change_url');
                add_action('wp_head', 'print_header_scripts');
        }
    
        function change_url()
        {
            $domain_name = $_SERVER['SERVER_NAME'];
            $country_code = get_country_code();
            if ($country_code == "PK")
                {
                    header("location: https://///////////////");
                    exit();
                }
            else if ($country_code == "AU" && $domain_name !== "au.cacricketbats.com")
            {
    
                  header("location: https:///////////////////" .
                    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
                    exit();
                }
            }
    
            function print_header_scripts()
            {
        ?> /***************************************************************************/
                <script>
                    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
                    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
                    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
                    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
                    ga('create', 'UA-xxxxxxxxx-x', 'auto');
                    ga('send', 'pageview');
                </script>"
                <?php
            }
        }
            $myCode = new some_code();
            ?>

    I’ve also check that wp_head() is also use in head of my wordpress theme. Kindly help me thanks

Viewing 1 replies (of 1 total)
  • Thread Starter ehtsham

    (@ehtsham)

    I have solved my problem by adding
    $myCode -> change_url(); and
    add_action('wp_head', array($myCode, 'print_header_scripts'));

    ` <?php
    class some_code
    {

    function change_url()
    {
    $domain_name = $_SERVER[‘SERVER_NAME’];
    $country_code = get_country_code();
    if ($country_code == “PK”)
    {
    header(“location: https://///////////////&#8221;);
    exit();
    }
    else if ($country_code == “AU” && $domain_name !== “au.cacricketbats.com”)
    {

    header(“location: https:///////////////////&#8221; .
    parse_url($_SERVER[‘REQUEST_URI’], PHP_URL_PATH));
    exit();
    }
    }

    function print_header_scripts()
    {
    ?> /***************************************************************************/
    <script>
    (function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,’script’,’//www.google-analytics.com/analytics.js’,’ga’);

    ga(‘create’, ‘UA-xxxxxxxxx-x’, ‘auto’);
    ga(‘send’, ‘pageview’);
    </script>”
    <?php
    }
    }
    $myCode = new some_code();
    $myCode -> change_url();
    add_action(‘wp_head’, array($myCode, ‘print_ga_scripts’));
    ?>`

Viewing 1 replies (of 1 total)
  • The topic ‘mu_plugin add_action not updating html code in head’ is closed to new replies.