Adding javascript to internal page on wordpress

Home Forums WordPress Adding javascript to internal page on wordpress

This topic contains 8 replies, has 2 voices, and was last updated by  yogi 2 years, 11 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #4269

    yogi
    Participant

    Hi Elena

    I want to add a javascript tracking code to one of my internal page but am struggling to do it on wordpress.

    I checked with the theme provider and he asked me to activate a child theme and add the coding to the function.php of the child theme.

    This is the code i am trying to add –

    ————————————–
    function insert_custom_code_to_head() {

    if ( is_page( 569 ) ) {
    <!– Facebook Conversion Code for AnalyticsConsulting –>
    <script type=”text/javascript”>
    var fb_param = {};
    fb_param.pixel_id = ‘6013675945414’;
    fb_param.value = ‘0.01’;
    fb_param.currency = ‘GBP’;
    (function(){
    var fpw = document.createElement(‘script’);
    fpw.async = true;
    fpw.src = ‘//connect.facebook.net/en_US/fp.js’;
    var ref = document.getElementsByTagName(‘script’)[0];
    ref.parentNode.insertBefore(fpw, ref);
    })();
    </script>
    <noscript></noscript>
    }

    }

    add_action( ‘udesign_head_bottom’, ‘insert_custom_code_to_head’ );

    ——————————————

    But, on uploading the code the site goes invisible and I see this error on the screen in my browser –

    Parse error: syntax error, unexpected ‘<‘ in /home1/yogi/public_html/wp-content/themes/u-design-child/functions.php on line 16

    Can you help me please?

    Many Thanks

    Yogi

    #4270

    Elena
    Keymaster

    Did you back up your functions.php before modifying? If yes, reupload it and site will go back online once you reupload it.

    Regarding the code, does it have to be uploaded site-wide or just on one page. Does it have to go into

    head

    or can be anywhere on page?

    • This reply was modified 2 years, 11 months ago by  Elena.
    #4272

    yogi
    Participant

    Morning Elena

    Thanks for such a quick reply 🙂

    I managed to do that, as in re-upload the old function.php and get the site back up, but haven’t been able to add the code yet.

    The code needs to go in the head of a particular page on the site.

    I received some help from the theme provider…he asked me to add this to the function.php of the child theme –

    function insert_custom_code_to_head() {

    if ( is_page( 123 ) ) {

    // Your code goes here…

    }

    }

    add_action( ‘udesign_head_bottom’, ‘insert_custom_code_to_head’ );

    replacing 123 with the page id

    but i get the error when i do that

    #4273

    Elena
    Keymaster

    Aplogies for delay, my family got me seriously distracted.

    I am not sure why he told you to use functions.php. I believe there are plugins that will allow you to modify head section for each page individually.

    I made a quick search and found this one
    https://wordpress.org/plugins/add-code-to-head/

    I never used it, but you can try or research other plugins that do same thing. By default any changes to head code in WP will be site-wide but these plugins may allow you do so only for select pages.

    #4274

    yogi
    Participant

    Please don’t apologise. Your forum is a great help to people like me and you do more than enough by answering our queries.

    Thank you for being there 🙂

    I think that plugin looks ideal…i may just do that.

    I knew you would have a solution for my problem…i spent quite a while trying to get this working…Thanks very much for your help Elena.

    Regards

    Yogi

    #4275

    Elena
    Keymaster

    No problem Yogi, try it and if it doesn’t work there should be must be another option. I am glad to help :))

    #4276

    yogi
    Participant

    Thanks Elena….i should have just checked with you 2 days ago…lol….thanks again.

    Regards

    Yogi

    #4277

    Elena
    Keymaster

    Yogi, this looks even better because it has positive reviews. I quickly tested it and it works:

    https://wordpress.org/plugins/per-page-add-to/

    Also if you or anyone reading this uses Genesis, they have it as inbuilt feature so you don’t need any plugins. Just scroll down on post/page edit page for page you want to add code to and you will see empty scripts field where you can add you tracking code or whatever you wish to add.

    Under it you will see:

    Suitable for custom tracking, conversion or other page-specific script. Must include script tags.

    #4279

    yogi
    Participant

    Thats the one i have installed Elena, and its working great by the looks of it.

    Regards

    Yogi

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Adding javascript to internal page on wordpress’ is closed to new replies.