如果激活,WordPress插件会将数字1添加到内容中

时间:2014-04-23 20:42:49

标签: php wordpress

如果插件处于活动状态,我的WP插件会奇怪地将数字1添加到内容中。

add_filter( 'the_content', 'poc_shop_cart_button_filter' );

function poc_shop_cart_button_filter($the_content) {

        $new_content = $the_content;

        $new_content .= include(WP_CONTENT_DIR . "/custom_php/shop-cart-button.php");

        return $new_content;

}

这是我在插件中包含的页面:

if(isset($_COOKIE['_abs_34287GjNW'])){
$cookieID = htmlspecialchars($_COOKIE['_abs_34287GjNW'], ENT_QUOTES, 'UTF-8');
} else {
    $cookieID = '';
}

$supplierID = get_bloginfo('name');

require(ABSPATH . 'wp-content/custom_php/includes/connectDB.php');

try {

    $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $conn->exec("SET CHARACTER SET utf8");

    $sql = "SELECT COUNT(*) AS count
                FROM `shoppingCart`
                WHERE supplierID=:supplierID
                AND cookieID = :cookieID";

    $sqlprep = $conn->prepare($sql);

    $ar_val = array(':supplierID'=>$supplierID,
                    ':cookieID'=>$cookieID);

        if($sqlprep->execute($ar_val)) {
            while($row = $sqlprep->fetch(PDO::FETCH_OBJ)){

                if($row->count=='' || $row->count==0){
                    $count='0';
                } else{
                    $count = $row->count;
                }

            }
        }

        if($count == 1){
            $items = "item";
        } else {
            $items = "items";
        }

            if(is_page(array(48)) || $count < 1){
                echo '<div class="shopCartNoButton">';
                echo "<img src='" . get_template_directory_uri() . "/images/cart-icon.png' alt=''>";
                echo " <b>$count $items </b>";
                echo '</div><div class="clearFix"></div>';
            } else {
                echo '<div class="shopCartButton">';
                echo "<img src='" . get_template_directory_uri() . "/images/cart-icon.png' alt=''>";
                echo " <b>$count $items </b>";
                echo "&nbsp; <a href='office-seating/checkout/' class='btn btn-warning'> Checkout >></a>";
                echo '</div><div class="clearFix"></div>';
            }


    } catch(PDOException $e) {
        echo $e->getMessage();
    }

enter image description here

因此,在MUCH Google搜索之后的解决方案如下:

$filename = get_include_contents('somefile.php');

function get_include_contents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        return ob_get_clean();
    }
    return false;
}

代码:

include( $filename );

返回包含的文件+值1,因为包含成功。 必须使用输出缓冲到include a PHP file into a string并且不返回值1和返回的文件内容。

1 个答案:

答案 0 :(得分:1)

我有类似的问题。这是因为include返回调用的值,即“true”或当WordPress通过echo呈现数据时返回1。只需使用file_get_contents即可。