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 have added a custom button to the media actions in a custom theme,

function wp_myplugin_media_button($context) {
    $wp_myplugin_media_button_image = '../wp-content/themes/datamine/images/icon_create_table.gif';
    $wp_myplugin_media_button = ' %s' . '<a id="fluffyRabbit" href="javascript:;" title="Add a Tabletool table to the post.">  <img src="'.$wp_myplugin_media_button_image.'" alt="" /></a>'; // table.php?type=tabletool&amp;TB_iframe=true" class="thickbox"
    return sprintf($context, $wp_myplugin_media_button);
}

add_filter('media_buttons_context', 'wp_myplugin_media_button');

but how can I attach event listeners?

$('#fluffyRabbit').live('click',function() { alert('o hai!'); });

If I use add_action, do I have to include a new script.js file?

If someone can step by step me on this that would be awesome. I just started really tinkering with WP.

Cheers! Bo

share|improve this question

3 Answers 3

Where are you attaching this listener:

$('#fluffyRabbit').live('click',function() { alert('o hai!'); });

I suppose that if you attach within a

$(document).ready(function(){ 
    //attach here 
    $('#fluffyRabbit').live('click',function() { alert('o hai!'); });
}; 

that would do the trick. Is it working for you?

share|improve this answer
    
OK does this get added in the main js? I tried adding it in the functions.php and in the header (both ways you stated above) it didnt work. I was looking for a best practices method/tutorial on adding jQuery to a theme. –  roberthuttinger Mar 3 '12 at 21:29
    
you've to use jQuery instead of $ in wordpress –  Mohit Bumb Sep 11 '12 at 4:10
up vote 0 down vote accepted

I got it to work after playing around for while...

this went in the custom js file

jQuery(document).ready(function(){ 
    jQuery('#fluffyRabbit').live('click',function() { console.log('o hai!'); });

    //console.log('loaded');
});

this went in functions.php

function load_scripts() {
   wp_enqueue_script('jquery');
   wp_enqueue_script('ttcustom', '/wp-content/themes/twitunez/ttcustom.js', array('jquery'));
}  

add_action('init', 'load_scripts');

share|improve this answer

Sorry I didn't got that you needed the wp_enqueue_script practice. I thought you were stuck on how to call bind your event. But you sorted out by yourself. That's the correct way to do it. You could also write the jQuery call in your footer.php in HTML. Not the cleanest but working still.

share|improve this answer
    
thanks for the followup! –  roberthuttinger Mar 5 '12 at 14:19

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.