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?
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 -
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 -
Update - The code has been update to work outside Wordpress Loop.
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.
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.
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.
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.