我使用markItUp用于在WP插件一个textarea(即,是widgets.php页上,而创建和编辑窗口小部件)。

在textarea的是markItUp'ed当我第一次打开窗口小部件,但在我点击保存功能丢失,我回一个普通文本区域。

我比较了前,保存和后保存网页的版本的源代码,并没有差别-obviously,作为页面不重新加载。要调用每一个Ajax调用不jQuery的需求?

我尝试添加

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

里面的小部件的表单处理功能,但没有帮助。 我试图做出改变本次活动结合到Save按钮太多,但似乎并没有有所作为(有一个很好的机会,我完全搞错了)。

有帮助吗?

解决方案

<强> 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。该代码是正确的现在。

其他提示

Doug的解决方案很好工作。我只不得不改变window.setTimeout功能如下:

 window.setTimeout( function(){
   $("textarea.markItUp").each(function () {
      if (!($(this).hasClass('markItUpEditor'))) {
          $(this).markItUp(mySettings);
      }
    });                                    
 }, 200 );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top