1

I can't figure out why this script wont load on site. Am I missing something?

function ajax_follow_enqueue_scripts() {

    wp_register_script('follow', plugins_url('the-follow.js', __FILE__));

    wp_enqueue_script('follow');


wp_localize_script( 'the_follow', 'postfollow', array('ajax_url' => admin_url( 'admin-ajax.php' )));

}
add_action( 'wp_enqueue_scripts', 'ajax_follow_enqueue_scripts' );
1
  • HI, I have tried that code, and it still does not work, i even tried without the localization to see if i could see the script -- the js file is in the root directory of the 'the-follow' plugin folder any thoughts - -my plugin structure is - //the-follow -- the-follow.js -- the-follow.php
    – DEM
    Jan 3, 2016 at 15:59

2 Answers 2

0

Two reasons your code is not working.
1. Handle should be same i.e. 'follow' where as you are using the_follow and follow.
2. You have to execute statements in sequence i.e.(not sure)
a) Register Script
b) Localize Script
c) Enqueue Script

Ref: wp_localize_script

// Register the script
wp_register_script( 'follow', plugins_url('the-follow.js', __FILE__) );

// Localize the script with new data
$localize_array = array(
    'ajax_url' => admin_url( 'admin-ajax.php' )
);
wp_localize_script( 'follow', 'postfollow', $localize_array );

// Enqueued script with localized data.
wp_enqueue_script( 'follow' );


Update 1:
Here is my folder structure
enter image description here

and here is the code, its working fine for me
enter image description here

3
  • HI, I have tried that code, and it still does not work, i even tried without the localization to see if i could see the script -- the js file is in the root directory of the 'the-follow' plugin folder any thoughts - -my plugin structure is - //the-follow -- the-follow.js -- the-follow.php
    – DEM
    Jan 3, 2016 at 17:31
  • @EternalNYC Updated my answer, please have a look if still you get issue please update your question, provide some code block and also check in your console there must be some 404 error regarding file not found.. etc
    – Webloper
    Jan 4, 2016 at 3:25
  • thanks for the code, u know what was weird, it was working, but for some reason the script was not outputting, thanks for the tips, made me grasp WP/Ajax a whole bunch more
    – DEM
    Jan 7, 2016 at 20:14
0

Where do ajax_follow_enqueue_scripts function reside? Is it on main plugin file or in sub-directory?

If in sub-directory then add dirname to the __FILE__ constant.

wp_register_script('follow', plugins_url('the-follow.js', dirname(__FILE__)));

Ofcourse your localization won't work until you give the exact handle name(follow)

But before localization try to check whether the script is included. Let us know

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.