1

WPウィジェットのテキストエリアにmarkItUpを使用しています(つまり、ウィジェットの作成および編集中にwidgets.phpページで)。

ウィジェットを最初に開いたとき、テキストエリアは markItUp されますが、保存をクリックすると機能が失われ、通常のテキストエリアに戻ります。

ページの保存前と保存後のバージョンのソース コードを比較しましたが、ページがリロードされていないため、明らかに違いはありません。ajax呼び出しごとにjQueryを呼び出す必要がありますか?

追加してみました

jQuery(".markitup").markItUp(mySettings);

ウィジェットのフォーム処理機能の中にありましたが、それは役に立ちませんでした。このイベントを保存ボタンにもバインドする変更を加えようとしましたが、違いはないようです (すべて間違っている可能性が高いです)。

4

2 に答える 2

3

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追加したばかりの が必要でした。コードは正しいです。

于 2010-01-03T04:53:43.710 に答える
1

ダグのソリューションはうまくいきました。次のように window.setTimeout 関数を変更するだけで済みました。

 window.setTimeout( function(){
   $("textarea.markItUp").each(function () {
      if (!($(this).hasClass('markItUpEditor'))) {
          $(this).markItUp(mySettings);
      }
    });                                    
 }, 200 );
于 2010-01-05T00:10:39.793 に答える