mu_plugin没有通过wp_head在head标签中插入html代码

时间:2016-02-06 22:44:06

标签: php wordpress wordpress-plugin

我正在尝试通过headmu_plugin代码中插入代码。我不知道为什么head代码和init未使用add_action('wp_head', 'my_function')add_action('init', 'my_function2')进行更新。我的代码示例如下:

<?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: http://///////////////");
                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();
    ?>

我还检查wp_head()是否也用于我的wordpress主题。 请帮助我谢谢

1 个答案:

答案 0 :(得分:0)

我通过添加解决了我的问题         $myCode -> change_url();和     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: http://///////////////");
            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();
    $myCode -> change_url();
add_action('wp_head', array($myCode, 'print_ga_scripts'));
    ?>