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?
|
|
I'm assuming that you want to load some post-specific scripts (JavaScript) in header. 1. Using Wordpress Custom FieldsOne way you achieve this by using wordpress custom fields, While posting new post just add a custom field with name (i.e - 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 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 - 2Use 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 wordpressIf 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 |
|||||||||||
|
|
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. |
|||
|