我正在制作插件,我想在其中创建选项供用户选择徽章的颜色,并在css中调用它时遇到问题,正在其他地方工作

问题描述 投票:0回答:1

我的主要php文件free-shipping-badge.php

/** REGISTER STYLES **/
add_action( 'wp_enqueue_scripts', 'fsb_stylesheet' );
function fsb_stylesheet() {
    wp_register_style( 'view-style', plugins_url('/view/fsb_badge_style.php', __FILE__) );
    wp_enqueue_style( 'view-style' );
}

/** ADD FREE SHIPPING BADGE SUFFIX AFTER PRICE **/
add_filter( 'woocommerce_get_price_suffix', 'fsb_suffix', 99, 4 );
function fsb_suffix( $html, $product, $price, $qty ){
    $fsb_price = (float) $product->get_price(); // Regular price
    $fsb_limit_price = esc_attr( get_option('fsb_limit_price_option') ); // Limit price
    $fsb_badge_text = esc_attr( get_option('fsb_badge_text_option') ); // Text to display on badge
    $fsb_badge_color = esc_attr( get_option('fsb_badge_color_option') ); // Text to display on badge
    if($fsb_price > $fsb_limit_price){
    $html .='</br>'.'<fsb_badge class="fsb_badge_view">' .$fsb_badge_text.' '.'</fsb_badge>';
    return $html;
}
}

我的样式文件:fsb_badge_style.php。背景未从php变量$ fsb_badge_color获取值,这是主要问题。

<?php
header('Content-type: text/css');
include('free-shipping-badge.php');     
?>
.fsb_badge_view{
    display: inline;
    padding: .3em .6em .3em;
    font-family: Arial;
    font-size: 10.5px;
    font-weight: bold;
    line-height: 1.5;
    color: #fff;
    text-align: center;
    white-space: middle;
    vertical-align: baseline;
    border-radius: .25em;
    background-color:  <?php echo $fsb_badge_color; ?>;

}
php wordpress woocommerce plugins
1个回答
1
投票

正如@Tony Djukic所说,您不能将PHP文件作为样式表排队,它不会处理PHP。虽然可能在标题中内联输出样式。一个例子:

function fsb_badge_style()
{
    echo "<style type=\"text/css\">.fsb_badge_view{background-color: ".$fsb_badge_color.";}</style>";
}
add_action('wp_head', 'fsb_badge_style', 100);

这将直接在标题中写入样式。

<style type="text/css">.fsb_badge_view{background-color: #e7e7e7;}</style>

作为额外的好处,这也将对可能未启用JavaScript的用户起作用。

© www.soinside.com 2019 - 2024. All rights reserved.