jquery - markItUp for Wordpress 小部件

标签 jquery ajax wordpress markitup

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

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

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

我尝试添加

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

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

最佳答案

jQuery

因此,您需要做的第一件事是 Hook 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 Hook 。它需要我刚刚添加的 .php 。现在代码是正确的。

关于jquery - markItUp for Wordpress 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1990335/

相关文章:

javascript - Jquery Modal 点击后只打开一次,如何解决这个问题?

javascript - 在 datePicker 文本框中使用任何分隔符

javascript - 从另一个页面链接到 Accordion 的一部分

ajax - 我需要查看 ColdFusion 2018 中所有登录用户的所有 session 范围

php - 使用插入查询 CI PHP Mysql Ajax

php - 数组中的两个 PHP 值

wordpress - "The Post Content Widget was not found in your template."(元素)

jquery - 下拉菜单不起作用

php - 跨插件的 WordPress 自定义 Hook

jquery - JsLint超出范围警告Ajax错误处理