Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to use jQuery in my Wordpress plugin. I'm trying to load the jquery library using the enqueue script but its throwing an error.

Error: $ is not a function

Here's the code snippet that's inside my main plugin.php file...

function ($post)
{
    wp_enqueue_script('jquery');
    ?>
    <script type="text/javascript">
    function doTestParse(searchString){
        var rx = new RegExp('(?![^<]+>)'+searchString, "gi");
        $(this).html($(this).html().replace(rx, '<b>$&</b>'));
    }
    </script>
<?php ?>
share|improve this question

2 Answers 2

up vote 4 down vote accepted

jQuery is included in noConflict mode in WordPress, so you can't use the shorthand $, but must write jQuery(this).html(jQuery(this).html().replace(rx, '<b>$&</b>'));

share|improve this answer
    
Thanks Jan, does <b>$&</b> become <b>jQuery&</b> ? –  Scott B Nov 3 '10 at 19:56
    
@Scott: html() returns a Javascript String object, and replace() is a standard function. &$ means "the matched substring" in this context, so the $ has nothing to do with jQuery and has to stay. –  Jan Fabry Nov 3 '10 at 20:17

I am not sure when exactly your code fires, but it is good practice to add enqueue directives to init hook, instead of trying to call them in-place.

wp_enqueue_script docs in Codex have plenty of useful examples on topic.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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