Support » Plugins » Add custom JavaScript to <head> from a post

  • Hi

    I’m new to WordPress and have been searching for a solution to this without any joy.

    Basically I have about 100 pages, and in one particular page only I need to use some JQuery.

    First therefore I need to link to the JQuery library, then to the custom JQuery plugin .JS file, and lastly to add some custom script to execute the JQuery functionality in the page.

    Because the custom script only applies to that one single page, I’d like to keep it within the page if possible, and not have it as a seperate .JS file.

    Is this possible to achieve please? I know I could create a new template file with all the links in there, but it seems longwinded and in future I might end up with dozens of template files for this purpose.

    As I said I’m very new, so please keep your explanations simple!

    Thanks 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • How about using:

    <?php if( is_page('This Page') ) {?> [ link to custom script ] <?php }?>

    in header.php?

    Hi.

    Thanks for your suggestion. That would have been my final way to do this.
    I would imagine though that in time, if JQuery was to become more widely used in the site, that the template file would become a bit busy with these types of statements…?

    I presume therefore that there isn’t a way to just ‘inject’ custom code into the template’s <head> area?

    If you’re looking for a single “flag” that can be used on multiple pages/posts, what about using a custom field? Not tried testing for the presence/value of a custom field in the header but that might work.

    Heve you tried to save the custom javascript’s link in a custom field to the post? then you can get it in the head when in a single page.

    something like that:

    <? if( is_single() and
           $customjs = get_post_meta($post->ID, 'custom_js', true) )
       echo "<script src='".$customjs."' language='javascript'></script>";
    ?>

    haha, sorry esmi. I was writing that when you post. ^^

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add custom JavaScript to <head> from a post’ is closed to new replies.