简体   繁体   English

Wordpress:在ajax函数中执行do_shortcode()

[英]Wordpress: execute do_shortcode() inside ajax function

I'm implementing a wordpress frontend php page which holds a list of hyperlinks in the body and a wpdatatable in the footer. 我正在实现一个wordpress前端php页面,该页面在正文中包含一个超链接列表,在页脚中包含一个wpdatatable。

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. 我想在用户每次选择链接时重新加载数据表,而这需要通过ajax完成而不重新加载整个页面。

What i did so far: 我到目前为止所做的:
I defined my action in \\wp-content\\themes\\Avada\\functions.php as the following: 我在\\ wp-content \\ themes \\ Avada \\ functions.php中定义了以下操作:

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

And inside my home page i made the ajax call as the following: 在我的主页中,我进行了如下的ajax调用:

function toggleTable(ministryId) {


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

}

where "tendersResult" is a div which should hold the datatable. 其中“ tendersResult”是应保存数据表的div。

The problem is that the html of the returned data table holds css link tags and it always display "0". 问题是返回的数据表的html包含css链接标记,并且它始终显示为“ 0”。

Following is how it's displayed in the browser, see the 0 text: 以下是它在浏览器中的显示方式,请参见0文本:

在此处输入图片说明

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 !! 任何帮助表示赞赏!

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" 更新:好的,所以我按照Fencer04的建议添加了wp_die(),但是我现在的问题是do_shortcode()仍然在标题中返回“” css标签,并且数据表返回为“ 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

Without wp_die() at the end of the function it will never return anything other than zero. 如果在函数末尾没有wp_die(),它将永远不会返回零以外的任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM