0

I want to write a plugin in WordPress but I receive a syntax error (unexpected ending).I wonder if the opening and the end tag for the PHP is correct or not. Here is the code:

<?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>
}
?>

1 Answers1

1

You are embedding javascript code in a wrong way.

Try it like this:

<?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
  }
?>

Alternatively, you can use echo statement for each line of js.

Atlas_Gondal
  • 2,512
  • 2
  • 15
  • 25