0

I am trying to add an external Javascript file from my plugin. My code is -

<?php 
/******
 * 
 * Plugin Name: Image Zoom
 * Author: Kallol Das   
 * Description: This plugin will zoom an image of WordPress posts.
 * version: 1.0
 * 
 *******/

 function zoom_image_main_js_init(){

 wp_enqueue_script('zoom-script', plugins_url('/js/zoom-script.js', __FILE__), array('jquery'), 1.0, true);

 }

add_action('init', 'zoom_image_main_js_init');

Now the problem is It's only enqueuing in admin footer But not in Frontpage footer. So, how to do that?

2 Answers 2

0

Instead of init use wp_enqueue_scripts, which is used to enqueue styles and scripts in front-end.

Also, for backend enqueue use admin_enqueue_scripts.

6
  • Now it gets worst. Script is not enqueued in Frontpage nor in Admin page.
    – Kallol Das
    Commented May 22, 2017 at 6:04
  • Do you have wp_head() and wp_footer() in your header and footer? Commented May 22, 2017 at 6:22
  • Just let me check.
    – Kallol Das
    Commented May 22, 2017 at 6:34
  • @AniruddhaGawade It's not a good practice to use wp_head() or wp_footer() to enqueue the scripts. May be he is facing the cache issues.
    – CodeMascot
    Commented May 22, 2017 at 6:38
  • 1
    I think wp_head and wp_footer and must use in your theme, without which all the plugin will break. Commented May 22, 2017 at 6:44
0

Check this below code-

function zoom_image_main_js_init() {
    if( is_front_page() || is_home() ) {
        wp_enqueue_script('zoom-script', plugins_url('/js/zoom-script.js', __FILE__), array('jquery'), 1.0, true);
    }
}
add_action( 'wp_enqueue_scripts', 'zoom_image_main_js_init' );

Here I'm hooking zoom_image_main_js_init function to wp_enqueue_scripts and inside zoom_image_main_js_init function I'm checking if it is front page or not by calling is_front_page() function inside if condition.

Hope that helps.

3
  • Still its not working
    – Kallol Das
    Commented May 22, 2017 at 6:14
  • Clear you cache and reload it again. And please tell again clearly what problem you're facing. @KallolDas
    – CodeMascot
    Commented May 22, 2017 at 6:16
  • And also check the updated answer. Hope it helps.
    – CodeMascot
    Commented May 22, 2017 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.