简体   繁体   English

自定义php文件中的PHP do_shortcode

[英]PHP do_shortcode in custom php file

I've created a little bit of js that passes three variables to my php script loadtable.php.我创建了一点点 js,它将三个变量传递给我的 php 脚本 loadtable.php。 This php file is stored in my child theme folder and its contents are as follows:这个php文件存储在我的子主题文件夹中,其内容如下:

<?php

$d = Date('Y-m-d', strtotime($_GET['d']));
$c = $_GET['c'];
$b = $_GET['b'];

$shortcode = "[wpdatatable id=3 var1=".$b." var2=".$d." var3=".$c."]";
echo do_shortcode('$shortcode');

The js part on the page:页面上的js部分:

window.onload = selectChart;

function selectChart () {
  var date = document.getElementById("date");
  var country = document.getElementById("country");
  var label = document.getElementById("label");
  var xDate = date.value.toString();
  var xCountry = country.value.toString();
  var xLabel = label.value.toString();

if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                 document.getElementById("chartTable").innerHTML = this.responseText;
            }
        };
        xmlhttp.open('GET','/wp-content/themes/Avada-Child-Theme/loadtable.php?d='+xDate+'&c='+xCountry+'&b='+xLabel,true);
        xmlhttp.send();
    }

I just can't get the do_shortcode php script to render the table when the page loads or when selectChart() is called.我只是无法让 do_shortcode php 脚本在页面加载或 selectChart() 被调用时呈现表格。 Do any of you know what my solution is?你们有人知道我的解决方案是什么吗? Thanks in advance.提前致谢。

I think that you have to import the wp-load.php file.我认为您必须导入 wp-load.php 文件。 require_once("../../../wp-load.php" ); require_once("../../../wp-load.php");

And then echo do_shortcode('$shortcode');然后 echo do_shortcode('$shortcode'); I am not sure the code is working.我不确定代码是否有效。 echo do_shortcode("$shortcode"); echo do_shortcode("$shortcode");

You should to use wordpress autoload first to be sure every necessary wordpress files like wp-config.php is loaded to your script and then use the wordpress function do_shortcode().您应该首先使用 wordpress 自动加载以确保将 wp-config.php 等所有必要的 wordpress 文件加载到您的脚本中,然后使用 wordpress 函数 do_shortcode()。

So your loadtable.php script should be something like this:所以你的 loadtable.php 脚本应该是这样的:

<?php

require_once("../../../wp-load.php"); // wordpress autoloader

$d = Date('Y-m-d', strtotime($_GET['d']));
$c = $_GET['c'];
$b = $_GET['b'];

$shortcode = "[wpdatatable id=3 var1=".$b." var2=".$d." var3=".$c."]";
echo do_shortcode('$shortcode');

It works only when your script is in the root of your child theme folder.它仅在您的脚本位于子主题文件夹的根目录中时才有效。 So if your script is located somewhere else, your path (../../../wp-load.php) should be changed.因此,如果您的脚本位于其他地方,则您的路径 (../../../wp-load.php) 应该更改。

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

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