我想插入放置在 WordPress 文件夹中的外部 JavaScript。 如果我包括使用脚本标签,它会显示“<”预期错误 下面是我的代码
global $wpdb;
include("/asset/php/chart4php/inc/chartphp_dist.php");
$p = new chartphp();
$p->data_sql = $wpdb->get_results("select t2.name, count(t1.id) as score from custom_status as t2 left join wpsp_ticket as t1 on t2.name = t1.status group by t2.name");
$p->chart_type = "bar";
// Common Options
$p->title = "Category Sales";
$p->xlabel = "Category";
$p->ylabel = "Sales";
$color = array("#1AAF5D","#F2C500","#F45B00","#8E0000","#0E948C");
$p->color = $color[rand(0,4)];
$out = $p->render('c1');
尝试包含 js 文件
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'ajax_search' );
wp_register_script( 'chartphp', plugins_url('/asset/php/chart4php/chartphp.js', __FILE__), array('jquery'));
wp_enqueue_script( 'chartphp' );
我还想包含 3 个移动文件
<script src="/asset/php/chart4php/jquery.min.js"></script>
<script src="/asset/php/chart4php/chartphp.js"></script>
<link rel="stylesheet" href="/asset/php/chart4php/chartphp.css">
最佳答案
在 WordPress 中包含脚本的正确方法是使用 wp_enqueue_script() .
如果您之前注册了脚本,您可以简单地引用它:
You could either link a script with a handle previously registered using the wp_register_script() function, or provide this function with all the parameters necessary to link a script.
请注意,如果您在 dependencies 参数中包含默认脚本(例如 jQuery),则无需单独注册和/或入队。
关于javascript - 如何在 WordPress 中添加外部 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42224845/