Wordpress:在 ajax 函数中执行 do_shortcode() [英] Wordpress: execute do_shortcode() inside ajax function

查看:24
本文介绍了Wordpress:在 ajax 函数中执行 do_shortcode()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个 wordpress 前端 php 页面,其中包含正文中的超链接列表和页脚中的 wpdatatable.

I'm implementing a wordpress frontend php page which holds a list of hyperlinks in the body and a wpdatatable in the footer.

我想在用户每次选择链接时重新加载数据表,这需要通过 ajax 完成,而无需重新加载整个页面.

I want to reload the data table each time the user selects a link and this needs to be done through ajax without reloading the whole page.

到目前为止我做了什么:
我在 \wp-content\themes\Avada\functions.php 中定义了我的操作如下:

What i did so far:
I defined my action in \wp-content\themes\Avada\functions.php as the following:

function tenders_shortcode() {
    echo do_shortcode("[wpdatatable id=49 var1=".$_POST['id']." var2=".$GLOBALS['cgv']['archive']."]");
}

add_action('wp_ajax_tenders_shortcode', 'tenders_shortcode'); // add action for logged users
add_action( 'wp_ajax_nopriv_tenders_shortcode', 'tenders_shortcode' ); // add action for unlogged users

在我的主页中,我按如下方式进行了 ajax 调用:

And inside my home page i made the ajax call as the following:

function toggleTable(ministryId) {


    $.post( '<?php echo admin_url('admin-ajax.php'); ?>', {    
      action: "tenders_shortcode",
      id: ministryId
    }, function(datatable) {
      alert("success");
      jQuery("#tendersResult").html(datatable)
    });

}

其中 "tendersResult" 是一个 div,它应该保存数据表.

where "tendersResult" is a div which should hold the datatable.

问题是返回数据表的html中含有css链接标签,总是显示0".

The problem is that the html of the returned data table holds css link tags and it always display "0".

以下是它在浏览器中的显示方式,请参见 0 文本:

Following is how it's displayed in the browser, see the 0 text:

这就是它在检查器中的显示方式:

And this is how it's displayed in the inspector:

请注意,在发布我的问题之前,我已经进行了大量研究,但没有任何方法可以解决我的问题.任何帮助表示赞赏!

Please note that i've done a lot of research before posting my question and nothing fixes my problem. Any help is appreciated !!

更新:好的,所以我按照 Fencer04 的建议添加了 wp_die(),但我现在的问题是 do_shortcode() 仍然在标题中返回 "" css 标签,并且数据表返回为 "display:none"

Update: Okay, so i've added wp_die() as suggested by Fencer04, but my problem now is that do_shortcode() still returns "" css tags in the header and the datatable is returned as "display:none"

推荐答案

您需要将您的功能更改为:

You need to change your function to:

function tenders_shortcode() {
    echo do_shortcode("[wpdatatable id=49 var1=".$_POST['id']." var2=".$GLOBALS['cgv']['archive']."]");
    wp_die(); //Added
}

add_action('wp_ajax_tenders_shortcode', 'tenders_shortcode'); // add action for logged users
add_action( 'wp_ajax_nopriv_tenders_shortcode', 'tenders_shortcode' ); // add action for unlogged users

如果函数末尾没有 wp_die(),它将永远不会返回除零以外的任何内容.

Without wp_die() at the end of the function it will never return anything other than zero.

这篇关于Wordpress:在 ajax 函数中执行 do_shortcode()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆