Wordpress Plugin to add script to headers

15 replies
Hi guys

Is there a plugin to add extra script to headers? I need to add a piece of script to only one post's header and not the whole site.

I don't want this piece of script to be added across the whole site.
#add #headers #plugin #script #wordpress
  • Profile picture of the author dwoods
    dackers, no but you could easily hard code it into your theme file using a simple PHP check to only output the script if the specific page is requested.
    I'd be happy to help you out if you'd like, contact me on skype: darrenwoods80
    Signature
    Need a way to drive traffic to your new site?
    -- Check out HelpMyHits.com!
    {{ DiscussionBoard.errors[7020738].message }}
    • Profile picture of the author dackers911
      Thanks Darren. This is my problem. I have been using Pinoy pop exit plugin on other themes without no problems. I am just building a new wordpress site using a Graphene theme, but the pop up gets stuck whilst redirecting to the specified url. Here's a page to give an example.

      Contact ยป PCOS Polycystic Ovarian Syndrome Treatment

      I am getting so frustrated as it happens with other exit pop up plugins.
      Signature

      Passionate eBook Kindle Author - pen Darren Stock
      Amazon Author Url
      www.ebooksbydarren.com
      Blog www.darrenstock.com

      {{ DiscussionBoard.errors[7021012].message }}
  • Profile picture of the author dwoods
    Dacker, so what you'd want to do is open up the single.php file and add something like this wher eyou'd like to embed the script(s)
    PHP Code:
    <?php if(get_current_page() == 'your_page'): ?>
    ..do something only for this page..
    <?php endif; ?>
    Signature
    Need a way to drive traffic to your new site?
    -- Check out HelpMyHits.com!
    {{ DiscussionBoard.errors[7021096].message }}
    • Profile picture of the author dackers911
      Thanks Darren

      The pop up script is for a page, the home page to be exact. Will this be ok for a page rather than a post as I tried it and nothing happened?
      Signature

      Passionate eBook Kindle Author - pen Darren Stock
      Amazon Author Url
      www.ebooksbydarren.com
      Blog www.darrenstock.com

      {{ DiscussionBoard.errors[7021165].message }}
      • Profile picture of the author dackers911
        Here's the page.php code. I added your code to various places - what happens is that the pop up doesn't show on exit when the code is added. Any idea?


        <?php
        /**
        * The Template for displaying all single pages.
        */

        get_header(); ?>



        <?php
        /* Run the loop to output the pages.
        * If you want to overload this in a child theme then include a file
        * called loop-page.php and that will be used instead.
        */
        the_post();
        get_template_part( 'loop', 'page' );
        ?>



        <?php get_footer(); ?>
        Signature

        Passionate eBook Kindle Author - pen Darren Stock
        Amazon Author Url
        www.ebooksbydarren.com
        Blog www.darrenstock.com

        {{ DiscussionBoard.errors[7021325].message }}
  • Profile picture of the author nimryas
    You need to implement the javascript (I asume thats what you are using for the popup) within the php script I think. Maybe this helps http: // www .koderguru.com/tutorials/phptutorials/php_alertbox.php
    {{ DiscussionBoard.errors[7021287].message }}
  • Profile picture of the author Dan Grossman
    The code needs to be after
    Code:
    the_post
    as that's what populates the global variables identifying what post/page the template is currently showing.

    Code:
    <?php
    
    get_header();
    the_post();
    
    if (is_page('Title Of Your One Page')) {
    ?>
    <!-- Paste Your JavaScript Here -->
    <?php
    }
    
    get_template_part('loop', 'page');
    
    get_footer();
    
    ?>
    Signature
    Improvely: Built to track, test and optimize your marketing.

    {{ DiscussionBoard.errors[7022150].message }}
  • Profile picture of the author wipeoutmedia
    My WordPress plugin will be able to do what you require.
    CSS & JavaScript Toolbox
    Signature
    Start your engines WordPress folks for the most powerful, most flexible, most re-usable, and only CSS & JavaScript plugin you will ever need. With enough muscle to easily allow rich customisations and modifications to your WordPress blog, CJT is a must have for self-taught WordPress web-masters to experienced WordPress developers. http://css-javascript-toolbox.com
    {{ DiscussionBoard.errors[7023606].message }}
    • Profile picture of the author dackers911
      Thanks Guys - but I still cannot get the Pinoy plugin to work on Graphene theme.

      I have tried adding the code below to the page.php and post.php under the-post.

      <?php if(get_current_page() == 'your_page'): ?>
      ..do something only for this page..
      <?php endif; ?>

      I have also added to both php pages
      <?php

      $script = get_post_meta(get_the_id,"script",true);
      switch($script) {
      case "pop_up" : do_something_here(); break;
      case "some_action" : do_something_here(); break;
      }?>

      I have also tried adding a pop up JS to the global footer and header, but again it doesn't work.

      What I find puzzling is that the Pinoy works on every other theme I have tried but not this Graphene one.
      Signature

      Passionate eBook Kindle Author - pen Darren Stock
      Amazon Author Url
      www.ebooksbydarren.com
      Blog www.darrenstock.com

      {{ DiscussionBoard.errors[7023946].message }}
  • Profile picture of the author Johan Efendi
    I have test it and work properly


    #1 Edit or create functions.php on your theme directory
    #2 Add script below

    PHP Code:
    <?php


    function add_custom_script() {
       global $ 
    post ;

       if (
    is_single() && $ post->ID==your_post_id) {
       
    ?>
       <script type="text/javascript"> your script here </script>
       <?php
       
    }
    }

    add_filter('wp_head','add_custom_script');

    ?>
    Please replace your_post_id with your own post id you want to add custom script.
    {{ DiscussionBoard.errors[7024014].message }}
  • Profile picture of the author Evan-M
    mowpop will do this ( WordPress Popup Plugin | Wordpress Popup Plugin Software | MOW POP )

    or :
    Code:
    <?php 
     global  $post; 
     	 $thePostID = 	 $post->ID;
    
    
    if ($thePostID == '35') {
    
    code here
    
    }
    ?>
    Signature

    Evan-M

    Easily The Worlds Best Wordpress Popup plugin

    Visit Website Design Firm For All Your Wordpress Coding Needs

    {{ DiscussionBoard.errors[7032774].message }}
  • Profile picture of the author klickgablow
    Actually the correct way to load scripts (regardless of where, be it header or footer) is to enqueue them.

    Code:
    function add_my_script() {
    	wp_enqueue_script(
    		'script-name',
    		get_template_directory_uri() . '/js/custom_script.js',
    		array('jquery')
    	);
    }
    add_action('wp_enqueue_scripts', add_my_script');
    The above code will add your custom_script.js file to your <head></head> located within your header.php file.

    The last line within the wp_enqueue_script,
    Code:
    array('jquery')
    Specifies that your script requires "jQuery" to work, and it will load "jQuery" before your inserting your script.

    Alternatively you can omit that line if you do not rely on "jQuery" for example sake and do the following,


    Code:
    function add_my_script() {
    	wp_enqueue_script(
    		'script-name',
    		get_template_directory_uri() . '/js/custom_script.js'
    	);
    }
    add_action('wp_enqueue_scripts', add_my_script');
    This line,
    Code:
    get_template_directory_uri() . '/js/custom_script.js
    Refers to your theme directory, i.e. wp-content/themes/your-theme/js/custom_script.js

    You can modify the location according to your preference.

    To selectively load your script on a specific post you would do,

    Code:
    function add_my_script() {
    
    if( is_single() ) {
    	wp_enqueue_script(
    		'script-name',
    		get_template_directory_uri() . '/js/custom_script.js'
    	);
      }
    }
    add_action('wp_enqueue_scripts', add_my_script');
    The is_single() conditional tag can either accept a post ID or slug or title,

    example => is_single(20) //for post ID 20
    example => is_single('my-special-post') //for post with slug my-special-post
    example => is_single('My Special Post') //for post with title My Special Post

    Of course hooking into wp_head would also suffice although its not the correct/preferred method for enqueuing scripts.

    PS. You would apply this to your themes functions.php
    {{ DiscussionBoard.errors[7049343].message }}

Trending Topics