markItUp for Wordpress 小部件

发布于 2024-08-17 02:17:38 字数 346 浏览 8 评论 0原文

我在 WP 小部件中使用 markItUp 作为文本区域(即在 widgets.php 页面上,创建和编辑小部件时)。

当我第一次打开小部件时,文本区域被标记,但在单击“保存”后,功能丢失,我回到常规文本区域。

我比较了页面保存前和保存后版本的源代码,显然没有区别,因为页面没有重新加载。每次 ajax 调用都需要调用 jQuery 吗?

我尝试

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

在小部件的表单处理函数中添加,但这没有帮助。 我也尝试进行更改,将此事件绑定到保存按钮,但这似乎没有什么区别(很有可能我全错了)。

I am using markItUp for a textarea in a WP widget (that is, on widgets.php page, while creating and editing a widget).

The textarea is markItUp'ed when I first open the widget, but after I click save the functionality is lost and I am back to a regular textarea.

I compared the source code for the before-save and after-save versions of the page and there is no difference -obviously, as the page isn't reloaded. Does jQuery need to be invoked for every ajax call?

I tried adding

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

inside the widget's form handling function but that didn't help.
I tried to make changes binding this event to the save button too but that didn't seem to make a difference (there is a good chance that I got it all wrong).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-08-24 02:17:38

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

The jQuery

So, the first thing you need to do is hook into the AJAX call so you are notified when the widgets have been saved. To do this, we will use the jQuery ajaxSuccess function. Put this in its own js file:

// 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);

The PHP/WordPress

Finally, tell WP to include your new js file only on the widgets page. You will need to incorporate this either into functions.php or if you are building a widget, into the widgets PHP file:

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' );

Edit I had an incorrect add_action hook when I posted. It needed the .php which I just added. The code is correct now.

如果没结果 2024-08-24 02:17:38

道格的解决方案效果很好。我只需更改 window.setTimeout 函数,如下所示:

 window.setTimeout( function(){
   $("textarea.markItUp").each(function () {
      if (!($(this).hasClass('markItUpEditor'))) {
          $(this).markItUp(mySettings);
      }
    });                                    
 }, 200 );

Doug's solution worked great. I only had to change the window.setTimeout function as follows:

 window.setTimeout( function(){
   $("textarea.markItUp").each(function () {
      if (!($(this).hasClass('markItUpEditor'))) {
          $(this).markItUp(mySettings);
      }
    });                                    
 }, 200 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文