最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Cannot add javascript to footer

programmeradmin0浏览0评论

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");

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");
Share Improve this question asked Sep 1, 2019 at 18:18 mckvakmckvak 1233 bronze badges 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? – Dave Romsey Commented Sep 1, 2019 at 18:29
  • @DaveRomsey Hello, its in functions.php I'm not even getting 404. It isnt showing at all. – mckvak 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. – Dave Romsey Commented Sep 1, 2019 at 18:38
  • 1 @DaveRomsey Thanks a lot. :] – mckvak Commented Sep 1, 2019 at 18:48
Add a comment  | 

1 Answer 1

Reset to default 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' );
发布评论

评论列表(0)

  1. 暂无评论