execution of javascript when a widget is added

source : wp-hackers digest # 67,10,7.

You can try taking over the wpWidgets.save function. The function's call signature when adding a new widget looks a little fragile, but this seems to work.

I tested it with the core calendar widget by replacing the 'my_widget' id_base in the JS below with 'calendar'.

/*
Plugin Name: JS Callback for My Widget
*/

function my_widget_add_js_callback() {
?>
<script type="text/javascript">
/* <![CDATA[ */
jQuery( function($) {
var origSave = wpWidgets.save;
wpWidgets.save = function( widget, del, animate, order ) {
if (
// It's one of my widgets
'my_widget' == widget.find( 'input[name=id_base]' ).val()
&&
// This seems to be the signature of a just added widget
!del && !animate && order
) {
alert( 'Widget Added!' );
// Do something
}
origSave.call( wpWidgets, widget, del, animate, order );
}
} );
/* ]]> */
</script>
<?php
}

add_action( 'admin_head-widgets.php', 'my_widget_add_js_callback' );
 
 
Creative Commons License
This work by maniac.vardhan is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
 
 

0 comments :: execution of javascript when a widget is added

Post a Comment