I have a large javascript file that I don't want to include on all pages. I would to only load it when the corresponding plugin is loaded. It's loaded when the post contains a specific keyword.
Here's a simplified version of my code:
<?php
if ( !is_admin() )
{
add_action('wp_head', 'set_js_vars');
add_filter('the_content', 'load_php_class_file', 9);
wp_enqueue_script('myJSlibrary', plugins_url('bigfile.js', __FILE__), array('swfobject'), 1.0);
}
function set_js_vars()
{
print "<script type='text/javascript'>\n";
print " foo = 'bar';\n";
print "</script>\n";
}
function load_php_class_file ( $content = '' )
{
if ($content == 'special_code')
{
ob_start();
require_once( dirname(__FILE__).'/plugin.php' );
return ob_get_clean();
}
return $content;
}
?>
When I move the wp_enqueue_script() call inside the load_php_class_file(), it doesn't seem to work. Am I missing something?