Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

A client has requested some interesting functionality. THey have about 20 different scripts, most of which are needed by one page only. They would like an area within Wordpress's pages and custom post types to add scripts for THAT PAGE ONLY. So when that page loads, the script loads in the header or footer, NOT inline with the body content. I am looking for an idea or a plugin to make this functionality happen. Any ideas?

share|improve this question
    
Do you want to add ability to choose script in edit-post page ? –  amit Jul 18 '12 at 14:29
    
yes, exactly. In pages, specifically –  JCHASE11 Jul 18 '12 at 14:55
    
This is definitely a better direction than the answers below: wordpress.stackexchange.com/questions/34894/… –  Jake Oct 26 '12 at 18:21
add comment

2 Answers

up vote 0 down vote accepted

I'm assuming that you want to load some post-specific scripts (JavaScript) in header.

1. Using Wordpress Custom Fields

One way you achieve this by using wordpress custom fields, While posting new post just add a custom field with name (i.e - header_script) and enter the Script source in next text-field.

This will store the Script source in wordpress database with name - header_script

To load the script located at that source in header files put the following code in header.php or put in footer.php incase you want that script to load in footer.

Here's the code that will load src information stored in custom field - header_script

Update - The code has been update to work outside Wordpress Loop.

<?php global $wp_query; 
  $postid = $wp_query->post->ID;
  $key="header_script";?><script type="text/javascript" src="<?php echo get_post_meta($postid, $key, true); ?>"></script>

UPDATE - 2

Use this code to show multiple scripts. NOTE - I changed the third value to false which allow us to store and retrive multiple values under same custom field name.

<?php global $wp_query;
$postid = $wp_query->post->ID;
$key="header_script";
$h_scripts=get_post_meta($postid, $key, false);
foreach($h_scripts as $h_scripts) { ?><script type="text/javascript" src="<?php echo $h_scripts ?>"></script><?php }?>

2. Using conditional statments in wordpress

If the scripts are specific per post types or categories means eg - image posts needs jQuery thick-box and video posts needs some script to show videos on page... just an example

You can use conditional statements to load only needed scripts for post types.

<?php if ( 'image' == get_post_type() ) { ?>
<script type="text/javascript" src="scrip-1"></script>
<?php } elseif ( 'video' == get_post_type() ) { ?>
<script type="text/javascript" src="scrip-2"></script>
<?php } else {} ?>

Above code will load script 1 only on image post type and so on. But I think the best way is to save the script source as a custom field.

share|improve this answer
    
sounds promising, let me try this out. –  JCHASE11 Jul 18 '12 at 15:01
    
I tried this, and the page showed this in the src: <script type="text/javascript" src=""></script> It looks like it is not loading the content. I am using advanced custom fields to create the custom field, and that always works great with get_post_meta. Any thoughts? –  JCHASE11 Jul 18 '12 at 15:06
    
There will be a field called - 'custom fileds' try using that. Oops. I think that won't work outside WP loop, wait I'll fix the code to work outside.. –  amit Jul 18 '12 at 15:08
    
ok thanks, let me know when its fixed. –  JCHASE11 Jul 18 '12 at 15:14
    
hey, I've updated the answer with right code to load script in header (i.e outside wordpress loop). –  amit Jul 18 '12 at 15:30
show 4 more comments

I would do this using a custom metabox. I use WPAlchemy MetaBox class, but if you're just creating one metabox it's probably best to just use the Wordpress metabox API.

A Standard text field would suffice, but an upload metabox would be more user friendly.

Then retrieve the URL entered in that metabox either in the header or footer.

Let me know if you need a more technical answer and I'll edit this with more info.

share|improve this answer
    
This sounds great. I can create a custom field with an option to add to both the header or footer. But in the template, I cannot place the loop before the header (and end after the footer), can I? How would I make it work with <?php wp_footer(); ?> and <?php wp_head() ?>? –  JCHASE11 Jul 18 '12 at 14:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.