0

my suggest.js is not being added in front-end. All scripts are correctly loaded but this one. I want to load it only on frontpage so:

add_action('wp_enqueue_scripts', 'homescripts');
function homescripts()  
{ 
   if (is_front_page()):
   wp_enqueue_script('suggest');
   endif;
}

and in my home's html

<script>
jQuery("#my_input").suggest("<?php echo get_bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php?action=ajax-tag-search&tax=my_taxonomy", {multiple:true, multipleSep: ","});
</script>

where #my_input is my input field I want with suggestions and tax=my_taxonomy is the source taxonomy.

Is there something worng I'm doing? Thanks in advance.

  • Have you registered the script (developer.wordpress.org/reference/functions/wp_register_script) before enqueuing it? – Caspar Apr 16 '16 at 14:22
  • I'm trying to use the internal wordpress script "suggest.js". Should I register even it? @Caspar – Kleeia Apr 16 '16 at 15:21
  • In that case, no, you shouldn't need to register it. Next place I'd look is whether I've got the right template... check difference/usage between home.php and front-page.php. Is the suggest script present on the output page? If so, the problem is with home html script. – Caspar Apr 16 '16 at 15:31
  • Ok but that is working fine. I've just checked. Now I see the sugges.min.js loaded but I get this error "TypeError: jQuery(...).suggest is not a function"... JQuery and Suggest.min.js are loaded in the correct order. Any suggestion? @Caspar – Kleeia Apr 16 '16 at 15:39
  • With the disclaimer that I'm not really a jQuery expert, see the answer below. Probably the best I've got at this point. – Caspar Apr 16 '16 at 17:47
0

Provided you've confirmed that WP's native 'suggest' script is actually being rendered on the page properly (ie. you're using the correct template for home vs front-page), sometimes to avoid script conflicts on the page, you need to wrap your script in an anonymous function. So something like:

<script>
(function($) {
    $("#my_input").suggest("<?php echo get_bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php?action=ajax-tag-search&tax=my_taxonomy", {
        multiple: true,
        multipleSep: ","
    });
})(jQuery)
</script>

Ideally, this should also be separated off into its own .js file and registered/enqueued properly on the wp_enqueue_scripts hook with 'suggest' declared as a dependency:

wp_enqueue_script( 'my-suggest', 'path/to/my-suggest.js', array( 'suggest' ), false, true );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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