Wordpress do_shortcode奇怪的行为

时间:2014-08-25 01:21:52

标签: php wordpress shortcode

我正在开发一个WP插件,它处理短代码并显示亚马逊项目数据来代替它们。该插件正在按照需要工作,除了一点点奇怪的行为。您可以在http://passivetest.themebandit.com/test-post-for-zon-plugin/看到我的测试运行。

如果您向下滚动该页面,则可以看到4" 1" s附加到内容。短代码在此页面中处理4次,每次WP向输出添加不需要的1。我不明白为什么会这样。没有" 1"在我的html文件中的任何地方,它在帖子内容中无处可寻。我的所有函数只返回要替换的内容而不是短代码。有人可以给它一个解释,让我知道如何删除这些?提前谢谢..

我的代码如下:

add_shortcode('zon_product', 'zon_process_shortcode');

// Zon Shortcode processor
function zon_process_shortcode($attributes, $content = null) {
    global $zon_html_path;

    // default options for shortcode
    $options = array('asin' => '', 'style' => 'compact', 'loc' => 'com');
    extract(shortcode_atts($options, $attributes));

    $asin = $attributes['asin'];

    // first find asin in local zon_data table
    $zdb = ZonDbHandler::instance();
    $product = $zdb->findASIN($asin);

    if ($product) {
        // if product exists in db, render template
        return zon_display_product($product, $attributes);
    } else {
        // product does not exist in database, get data through amazon api worflow
        $product = ZonLibrary::getAmazonProduct($asin, $attributes['loc']);
        if ($product) {
            // product data has been successfully retrieved and saved, render template
            return zon_display_product($product, $attributes);
        } else {
            // error in fetching product data, check amazon access key id and api secret
            $content = include($zon_html_path . 'html' . DIRECTORY_SEPARATOR . 'api_error.php');
            return $content;
        }
    }
}

// Renders selected template with product data
function zon_display_product(ZonProduct $product, $attributes) {
    global $zon_html_path;
    global $zon_path;

    // process other shortcode options
    $view_vars = array();
    $view_vars['style'] = (isset($attributes['style'])) ? $attributes['style'] : "default";
    $view_vars['show_price'] = (isset($attributes['show_price']) && $attributes['show_price'] == 0) ? false : true;
    $view_vars['price_updates'] = (isset($attributes['price_updates']) && $attributes['price_updates'] == 0) ? false : true;
    $view_vars['hide_unavailable'] = (isset($attributes['hide_unavailable']) && $attributes['hide_unavailable'] == 1) ? true : false;
    $view_vars['show_desc'] = (isset($attributes['show_desc']) && $attributes['show_desc'] == 0) ? false : true;

    // check if template file exists
    if (!is_file($zon_html_path . 'html' . DIRECTORY_SEPARATOR . $view_vars['style'] . '.php')) {
        $content = 'ERROR! Zon Template not found. Please check you are using a correct value for the "style" parameter.';
        return $content;
    } else {
        // get product array
        $product = $product->getArray();

        // if product is unavailable and hide_unavailable is true, return unavailable template
        if ($view_vars['hide_unavailable']) {
            if ((strpos($product['availability'], "Usually") === false) && strpos($product['availability'], "ships") === false) {
                $content = include($zon_html_path . 'html' . DIRECTORY_SEPARATOR . 'unavailable.php');
                return $content;
            }
        }

        // render chosen template file
        $content = include($zon_html_path . 'html' . DIRECTORY_SEPARATOR . $view_vars['style'] . '.php');
        return $content;
    }
}

0 个答案:

没有答案