jQuery
因此,您需要做的第一件事是挂钩 AJAX 调用,以便在保存小部件时收到通知。为此,我们将使用 jQueryajaxSuccess
函数。把它放在它自己的js
文件中:
// Use a self executing function so we can safely use
// $ inside and know it = jQuery
(function($){
// Tie into all jQuery AJAX requests
$(document).ajaxSuccess(function(e, x, o){
// Make sure our widget is the one being saved
// id_base will equal whatever is set in the PHP for the widget
// In this example, we target the text widget
if(o.data && o.data.indexOf('id_base=text') > -1){
// Now, lets quickly find all the right elements
// and filter out the ones already set up, and finally
// apply the `markItUp` call, but we will delay just to give
// WP a chance to update the widget
window.setTimeout( function(){
$("textareas.markItUp:not(.markItUpEditor)").markItUp(mySettings);
}, 200 );
}
});
})(jQuery);
PHP/WordPress
最后,告诉 WP仅在小部件页面上包含您的新 js 文件。您需要将其合并到functions.php
或者如果您正在构建小部件,则将其合并到小部件 PHP 文件中:
function register_markitup(){
wp_enqueue_script( 'markitup-widgets', WP_PLUGIN_URL . '/your-plugin/js/markitup-ajax.js' );
}
add_action( "admin_print_scripts-widgets.php", 'register_markitup' );
编辑当我发布时,我有一个不正确的add_action
钩子。它需要.php
我刚刚添加的。现在代码是正确的。