0

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

3 Answers 3

0

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?

2
  • 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. Commented Mar 3, 2012 at 21:29
  • you've to use jQuery instead of $ in wordpress Commented Sep 11, 2012 at 4:10
0

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

0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.