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


Where are you attaching this listener:

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

I suppose that if you attach within a

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

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

  • 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

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

this went in the custom js file

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


this went in functions.php

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

add_action('init', 'load_scripts');


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.

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.