Programming

Load JS/PHP  for shortcode

Have you ever tried to load the js or a particular file, only if particular short-code is available in the particular page/post ? If not, then you can do it now.

 

add_shortcode('short-code-tag', function(){
 ob_start();
 include __DIR__.'/test.php';
 wp_register_script('test',get_template_directory_uri().'/test.js', array(), '1.0', true);
 return ob_get_clean();
 });

If the shortcode tag [short-code-tag] is available in the post content, the function is called, which loads the necessary php or js files for the particular page/post.
For extended information you can check Codex.

Views: 60

Standard