【问题标题】:Wordpress shortcode is not workingWordpress 短代码不起作用
【发布时间】:2018-05-24 15:56:29
【问题描述】:

我正在构建一个“添加到购物车”按钮,它将项目(从 XML 代码中提取)放入购物车。按钮的 shortcodes 正在工作并显示按钮,但是当我尝试实现 shortcode function 来显示购物车本身时,页面根本不会呈现按钮或购物车。这是我的一些代码(PHP 和 Wordpress 的新手):

在shortcodes.php 文件中:

add_shortcode('show_cart', 'show_shopping_cart_handler');

在plugin.php主文件中:

function show_shopping_cart_handler($atts) {
    return print_shopping_cart($atts);
}

在functions.php 文件中:

// code for pulling the XML, $product_id is attached to each button
$xml = simplexml_load_file("https://secure.bmtmicro.com/cart?CID=2/WP&PRODUCTID=' . $product_id . '") or wp_die("Error.");

function print_shopping_cart() {
 // handles the cart name/text
    if (!cart_not_empty()) {
        $empty_cart_text = get_option('cart_empty_text');
        if (!empty($empty_cart_text)) {
            echo '<div class="bmt_cart_empty_cart_section">';
            if (preg_match("/http/", $empty_cart_text)) {
                echo '<img src="' . $empty_cart_text . '" alt="' . $empty_cart_text . '" class="empty_cart_image" />';
            } else {
                echo __($empty_cart_text);
            }
            echo '</div>';
        }
     }
     echo $empty_cart_text;

    $title = get_option('cart_title');

    echo '<div class="shopping_cart">';
    if (!get_option('shopping_cart_image_hide')) {
        $cart_icon_img_src = CART_URL . "/images/shopping_cart_icon.png";
        $cart_icon_img_src = apply_filters('cart_icon_image_src', $cart_icon_img_src);
        echo "<img src='" . $cart_icon_img_src . "' class='cart_header_image' value='" . (__("Cart")) . "' alt='" . (__("cart")) . "' />";
    }
    if (!empty($title)) {
        echo '<h2>';
        echo $title;
        echo '</h2>';
    }
    echo '<table style="width: 100%;">';
    if ($_SESSION && is_array($_SESSION)) {
        echo '
        <tr class="cart_item_row">
        <th class="cart_item_name_th">' . (__("Product Name")) . '</th><th class"cart_qty_th">' . (__("quantity")) . '</th><th class="cart_price_th">' . (__("Price")) . '</th><th></th>
        </tr>';
    foreach ($xml->producttable->row() as $product) {
      // I will pull more product info, but just using name for testing
        $product_name = $product->productname.PHP_EOL;

        echo '<tr class="cart_item_thumb"><td class="cart_item_name_td" style="overflow: hidden;">';
            echo '<div class="cart_item_info">';
            echo '<span class="cart_item_name">' . $product_name . '</span>';
            echo '<span class="cart_clear_float"></span>';
            echo '</div>';
            echo '</td>';
        }
    }

    echo "</table></div>";
}

真的是我要问的问题,任何人都可以看到我可能做错或丢失的东西导致什么都没有显示吗?我还尝试将echo 替换为$output 并执行$output .= 'some HTML code'; 之类的操作,然后运行return $output;,但我仍然得到相同的空白页结果。

如果需要,我可以提供更多代码,提前致谢!

【问题讨论】:

  • 短代码不应该echo 任何东西,只返回一个字符串。无论如何,你需要调试正在发生的事情,你当前的代码是不可能重现的......见How do I debug a WordPress plugin?

标签: php wordpress


【解决方案1】:

$xml 不在该函数的范围内。您要么需要传递它,要么在函数中定义 $xml,要么将 $xml 设为全局。另外,正如 brasofilo 所说,短代码处理程序应该返回一个字符串,而不是回显。但是,既然您提到返回字符串或回显会产生相同的结果,那么我会说最高概率是实际错误。就像尝试遍历未定义为 $xml 的对象一样。

这是我的意思的一个粗略的例子......

function print_shopping_cart() {
    global $xml;

但是... $xml 似乎依赖于产品 ID,我们无法知道您是如何填充的。据我们所知,您的 $xml 行正在访问 or die 部分,这就是您什么都看不到的原因。

现在,我正式说你应该重构你如何处理这个问题。您可以直接在插件的 php 文件中定义短代码。

在插件的 php 文件中:

add_shortcode('show_cart', 'show_shopping_cart_handler');

function show_shopping_cart_handler($atts) {
    // please note you're not using $atts. We dont know why
    $xml = simplexml_load_file('whatever');

    return print_shopping_cart($xml);
}

在functions.php中,我们假设购物车存在是有原因的,而不仅仅是因为你想让事情变得不必要地复杂化,我们实际上传递了$xml并修改了函数以期望它......

function print_shopping_cart($xml = false) {
   if($xml)
   {
       return 'xml based string'; // now we know xml was successful
   }
   else
   {
       return 'deal with the cart some other way';
   }
}

简而言之,保持你的插件包含。如果插件处理整个购物车,则将所有内容移动到插件的 php 文件中,并且不要将它们分散到各处。函数有作用域。如果您尝试访问函数范围之外的变量,它将引发错误或直接“不这样做”。您上面的代码中有一些漏洞,使我们无法真正了解它在做什么。我们不知道$product_id 是如何填充的。我们不知道cart_not_empty() 做了什么,等等。所以我的这个小例子是一个简短的例子,说明如何打印几行文本来开始调试。慢慢地用你的真实代码替换我的示例垃圾,并找出它在哪里出错。

【讨论】:

  • $product_id 用于每个按钮 ([add_cart_button product_id=”22804″]),该 ID 用于从我们的系统中提取信息。对于“立即购买”(重定向到我们的购物车的单击按钮,将该 ID 传递给显示正确产品信息的 URL)正在工作,但我在尝试使“添加到购物车”按钮允许时遇到问题不止一种产品,等等。我会看看我能用所提供的信息做什么,并希望能有所作为!感谢您的帮助!
  • 我注释掉了 print_shopping_cart 函数中的所有其他代码,并输入了你所拥有的内容,它返回了“基于 xml 的字符串”,所以我们到了某个地方! @凯青
猜你喜欢
  • 2014-06-04
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 2019-05-24
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多