1

Im trying to paste my javascript into footer and cant figure out.

function js_enqueue_search(){
    wp_register_script("search", get_stylesheet_directory_uri() . "/js/search.js", "", wp_get_theme()->get("Version"), true);
    wp_localize_script('search', 'search_ajax', array("ajaxurl" =>admin_url("admin-ajax.php")));
}
add_action("wp_enqueue_scripts", "js_enqueue_search");
4
  • Is this code in your theme's functions.php file? Check your browser's dev tools. Are you getting a 404 on search.js? Commented Sep 1, 2019 at 18:29
  • @DaveRomsey Hello, its in functions.php I'm not even getting 404. It isnt showing at all. Commented Sep 1, 2019 at 18:30
  • 1
    Woops ok, I missed this on the first read. Your're missing the call to wp_enqueue_script( 'search'); after it's registered. Commented Sep 1, 2019 at 18:38
  • 1
    @DaveRomsey Thanks a lot. :] Commented Sep 1, 2019 at 18:48

1 Answer 1

1

Your're missing the call to wp_enqueue_script( 'search' ); after it's registered.

function js_enqueue_search() {
    wp_register_script( 'search', get_stylesheet_directory_uri() . '/js/search.js', '', wp_get_theme()->get( 'Version' ), true );
    wp_enqueue_script( 'search' );
    wp_localize_script('search', 'search_ajax', array( 'ajaxurl' =>admin_url( 'admin-ajax.php' )));
}
add_action( 'wp_enqueue_scripts', 'js_enqueue_search' );
2
  • Can I have one more Q please? I'm getting 404 with the AJAX request and can't find out. I got only this code and this JS. :: let ajax = new XMLHttpRequest(); ajax.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { console.log(this.ajax); } }; ajax.open("GET", search_ajax.nonceurl, true); ajax.send(); Commented Sep 1, 2019 at 19:04
  • This answer might be helpful: wordpress.stackexchange.com/a/301736/2807 If not, please create a new question for that issue. Commented Sep 1, 2019 at 20:02

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.