I am developing a plugin for wordpress that loads javascript in a page.
But i want this plugin to load only on selected pages. Not all pages.
Can someone suggest how to do that?
Here is the code.
add_action('wp_enqueue_scripts', 'soundninja_enqueue');
function soundninja_enqueue($hook)
{
wp_enqueue_script('soundninja', // id
'http://soundninja.github.io/SNtest/build/Soundninja.min.js', // path
array('jquery'), // dependencies
0, // appends ?ver=$wordpress_version
true // in_footer
);
}
Another possible workaround would be to keep the plugin deactivated and activate it only for the required pages. This would prevent the additional overhead involved in loading the plugin on pages where the plugin is not required. And most importantly you do not have to tweak the code of the existing plugin.
Here is the post which can give you more idea http://shibashake.com/wordpress-theme/how-to-selectively-load-plugins-for-specific-pages
Depending on where you will call your function
soundninja_enqueue($hook)
you can easily add anif
statement asking if the current page/post id is in an allowed list of ids.But first you need to get the current page/post id, for this you have a couple of options, depending if are calling the function inside or outside of
The loop
.see here on how to get the current page id in wordpress
Another option is to pass the the current page/post id as a parameter to the function and do the same
if
test inside the function, your choice.