0
如果插件處於活動狀態,則我的WP插件會將數字1添加到內容中。WordPress插件將數字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 " <a href='office-seating/checkout/' class='btn btn-warning'> Checkout >></a>";
echo '</div><div class="clearFix"></div>';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
所以,要解決這個經過大量的谷歌搜索如下:
$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已成功。 必須使用輸出緩衝到include a PHP file into a string,並且不返回值1與返回的文件內容。
看到我的解決方案添加到上面編輯的問題。 file_get_contents()以文本字符串形式返回文件,不解釋文件中的PHP。 – Grant