Wordpress ウィジェット用の markItUp
質問
WP ウィジェットのテキストエリア (つまり、ウィジェットの作成および編集中に widgets.php ページ上) に markItUp を使用しています。
最初にウィジェットを開いたとき、テキストエリアは markItUp されますが、保存をクリックすると機能が失われ、通常のテキストエリアに戻ります。
ページの保存前と保存後のバージョンのソース コードを比較しましたが、ページがリロードされていないため、明らかに違いはありません。ajax呼び出しごとにjQueryを呼び出す必要がありますか?
追加してみました
jQuery(".markitup").markItUp(mySettings);
ウィジェットのフォーム処理関数内にありましたが、役に立ちませんでした。このイベントを保存ボタンにバインドする変更も加えようとしましたが、違いはないようでした(私がすべて間違っていた可能性が十分にあります)。
解決
jQuery
したがって、最初に行う必要があるのは、ウィジェットが保存されたときに通知されるように、AJAX 呼び出しにフックすることです。これを行うには、jQuery を使用します。 ajaxSuccess
関数。これを単独で入れてください 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
私が追加したばかりです。コードは現在正しいです。
他のヒント
ダグのソリューションはとてもうまくいきました。私は次のようにwindow.setTimeoutは機能を変更する必要がありました。
window.setTimeout( function(){
$("textarea.markItUp").each(function () {
if (!($(this).hasClass('markItUpEditor'))) {
$(this).markItUp(mySettings);
}
});
}, 200 );