我想用WordPress写一个插件,但是收到语法错误(意外的结尾),我想知道PHP的开头和结尾标记是否正确。这是代码:
<?php
/*
Plugin Name: Chapter 2 - Page Header Output
Plugin URI:
Description: Adding output content to page header
Version: 1.0
Author: Saeid Marouski
Author URI: http://smarouski.com
License: GPLv2
*/
add_action('wp_head','ch2pho_page_header_output');
function ch2pho_page_header_output() {
<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',
'https://www.google-analytics.com/analytics.js','ga');
ga('create','UA-0000000-0', 'auto');
ga('send', 'pageview');
</script>
}
?>
答案 0 :(得分:1)
您以错误的方式嵌入了JavaScript代码。
尝试一下:
<?php
/*
Plugin Name: Chapter 2 - Page Header Output
Plugin URI:
Description: Adding output content to page header
Version: 1.0
Author: Saeid Marouski
Author URI: http://smarouski.com
License: GPLv2
*/
add_action('wp_head','ch2pho_page_header_output');
function ch2pho_page_header_output() {
?>
<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',
'https://www.google-analytics.com/analytics.js','ga');
ga('create','UA-0000000-0', 'auto');
ga('send', 'pageview');
</script>
<?php
}
?>
或者,您可以对echo
的每一行使用js
语句。