在小部件中使用Add_action(‘wp_head’)

时间:2013-10-16 作者:asaunders

嗨,我似乎找不到解决我问题的答案。

我正在尝试修复此验证错误“Element link缺少必需的属性属性。”

这是因为样式表不在头部。我想做的是使用wp\\u head操作将其放在标题中,但一旦我将代码添加到小部件中,javascript就会完全中断,所以我不知道如何向标题添加样式?

这是我目前使用的代码:

wp_register_script(\'piechart\', get_template_directory_uri() . \'/js/jquery.easypiechart.min.js\', \'\', \'2.0.4\');
            wp_enqueue_script(\'piechart\');
            wp_register_style(\'piechart-css\', get_template_directory_uri() . \'/css/jquery.easy-pie-chart.css\');
            wp_enqueue_style(\'piechart-css\');
我想做的是得到如下结果:(这些代码都在widget函数中)

add_action(\'wp_head\', \'pieChartScripts\');
            function pieChartScripts() {
                wp_register_style(\'piechart-css\', get_template_directory_uri() . \'/css/jquery.easy-pie-chart.css\');
                wp_enqueue_style(\'piechart-css\');
            }
或者我可以写整个链接标签等,但这样做会添加多次。

编辑:

这是我的小部件功能代码:

public function widget( $args, $instance ) {
        $title = apply_filters( \'widget_title\', $instance[\'title\'] );
        $barOrPie = apply_filters( \'barOrPie\', $instance[\'barOrPie\'] );
        $graphTitle = apply_filters( \'graph1_title\', $instance[\'graph1_title\'] );
        $graphQuote = apply_filters( \'graph1_quote\', $instance[\'graph1_quote\'] );
        $graph1Figure = apply_filters( \'graph1_figure\', $instance[\'graph1_figure\'] );
        $graph1Colour = apply_filters( \'graph1_colour\', $instance[\'graph1_colour\'] );
        $graph2Figure = apply_filters( \'graph2_figure\', $instance[\'graph2_figure\'] );
        $graph2Colour = apply_filters( \'graph2_colour\', $instance[\'graph2_colour\'] );
    ?>

    <?php
        $progressSlector = $args[\'widget_id\'];

        echo $args[\'before_widget\'];
        if ( ! empty( $title ) ) {
            echo $args[\'before_title\'] . $title . $args[\'after_title\'];
        }

        if($barOrPie == \'progressBar\') { ?>
            <script type=\'text/javascript\'>
                function progress(percent, $element) {
                    var progressBarWidth = percent + \'%\';
                    $element.find(\'div\').animate({ width: progressBarWidth }, 500).html(\'<p>\' + percent + "<sup>%&nbsp;</sup>" + \'</p>\');
                }
                jQuery(document).ready(function () {
                    progress(<?php echo $graph1Figure ?>, jQuery("#<?php echo $progressSlector ?> .progressBar1"));
                });
            </script>

        <?php
            echo \'<div class="progress_containers">\';
            echo \'<div class="progressbar \' . $graph1Colour . \'-progress progressBar1"><div></div></div>\';
            if( !empty ($graph2Figure) )  {
                echo \'<div class="progressbar \' . $graph2Colour . \'-progress progressBar2"><div></div></div>\';
            ?>
                <script type=\'text/javascript\'>
                    jQuery(document).ready(function () {
                        progress(<?php echo $graph2Figure ?>, jQuery("#<?php echo $progressSlector ?> .progressBar2"));
                    });
                </script>
            <?php
            }
            echo \'</div>\';
         } else {
            add_action(\'wp_head\', \'pieChartScripts\');
            function pieChartScripts() {
                wp_register_style(\'piechart-css\', get_template_directory_uri() . \'/css/jquery.easy-pie-chart.css\');
                wp_enqueue_style(\'piechart-css\');
            }


            wp_register_script(\'piechart\', get_template_directory_uri() . \'/js/jquery.easypiechart.min.js\', \'\', \'2.0.4\');
            wp_enqueue_script(\'piechart\');


            switch($graph1Colour) {
                case \'purple\':
                    $pieColor = \'#78267B\';
                    break;
                case \'yellow\':
                    $pieColor = \'#ffc000\';
                    break;
                case \'green\':
                    $pieColor = \'#89bf3c\';
                    break;
                default:
                    $pieColor = \'#78267B\';
            }

            echo \'<div class="chart chart1" data-percent="\' . $graph1Figure . \'"><span style="color: \' . $pieColor . \';">\' . $graph1Figure . \'<sup>%</sup></span></div>\';

            if( !empty ($graph2Figure) )  {
                switch($graph2Colour) {
                    case \'purple\':
                        $pieColor2 = \'#78267B\';
                        break;
                    case \'yellow\':
                        $pieColor2 = \'#ffc000\';
                        break;
                    case \'green\':
                        $pieColor2 = \'#89bf3c\';
                        break;
                    default:
                        $pieColor2 = \'#78267B\';
                }

                echo \'<div class="chart chart2" data-percent="\' . $graph2Figure . \'"><span style="color: \' . $pieColor2 . \';">\' . $graph2Figure . \'<sup>%</sup></span></div>\';
            }
            ?>
            <script type="text/javascript">
                jQuery(function() {
                    jQuery(\'#<?php echo $progressSlector ?> .chart1\').easyPieChart({
                        barColor: "<?php echo $pieColor; ?>",
                        lineWidth: 31,
                        lineCap: \'square\',
                        trackColor: \'#e9ecec\',
                        scaleColor: false,
                        size: 143
                    });

                    jQuery(\'#<?php echo $progressSlector ?> .chart2\').easyPieChart({
                        barColor: "<?php echo $pieColor2; ?>",
                        lineWidth: 31,
                        lineCap: \'square\',
                        trackColor: \'#e9ecec\',
                        scaleColor: false,
                        size: 143
                    });
                });
            </script>
        <?php
        }

        echo \'<h5 class="\' . $graph1Colour . \'-title">\' . $graphTitle . \'</h5>\';
        echo \'<p>"\' . $graphQuote . \'"</p>\';

        echo $args[\'after_widget\'];
    }
谢谢大家的帮助

1 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

你可以打电话wp_enqueue_script() 直接在您的小部件输出中,WordPress将使脚本排队。无需将其添加到回调。尝试更改此选项:

else {
    add_action(\'wp_head\', \'pieChartScripts\');
    function pieChartScripts() {
        wp_register_style(\'piechart-css\', get_template_directory_uri() . \'/css/jquery.easy-pie-chart.css\');
        wp_enqueue_style(\'piechart-css\');
    }

    wp_register_script(\'piechart\', get_template_directory_uri() . \'/js/jquery.easypiechart.min.js\', \'\', \'2.0.4\');
    wp_enqueue_script(\'piechart\');

// etc
}
。。。对此:

else {
    wp_register_style(\'piechart-css\', get_template_directory_uri() . \'/css/jquery.easy-pie-chart.css\');
    wp_enqueue_style(\'piechart-css\');

    wp_register_script(\'piechart\', get_template_directory_uri() . \'/js/jquery.easypiechart.min.js\', \'\', \'2.0.4\');
    wp_enqueue_script(\'piechart\');

// etc
}

结束

相关推荐

Custom Post Row Actions

我偶然发现this question 在写这个问题的时候。我有一个问题是关于这个问题的。我发现你用的是get_delete_post_link 筛选为我的操作创建一个新的url(或一个类似的函数——在任何情况下,我都会将该函数与布尔值一起使用)。唯一的问题是,I don\'t know how to capture the event now. 考虑到我在谷歌上找不到很多关于行后操作的例子,我将不胜感激-/public function _wp_filter_get_delete_post_link( $