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/ワードプレス
最後に、新しい js ファイルをウィジェット ページにのみ含めるように WP に指示します。これをウィジェット PHP ファイルに組み込むか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
追加したばかりの が必要でした。コードは正しいです。