使用2个字段自定义后端Wordpress小部件标题

时间:2018-12-31 04:30:49

标签: javascript jquery wordpress

我有一个自定义窗口小部件,我们希望在窗口小部件标题中显示窗口小部件内两个字段。例如,在所附的图像中,我们希望产品的品牌在Strain名称之后显示,以便它看起来像这样:

花:平​​静114烛光

我们不需要在前端显示它,而仅在后端显示。

谢谢!

enter image description here

class EnhancedTextWidget extends WP_Widget {

/**
 * Widget construction
 */
function __construct() {
    $widget_ops = array('classname' => 'widget_text enhanced-text-widget', 'description' => __('Use this to add Strains to the menu', 'enhancedtext'));
    $control_ops = array('width' => 400, 'height' => 350);
    parent::__construct('EnhancedTextWidget', __('Flower', 'enhancedtext'), $widget_ops, $control_ops);
    load_plugin_textdomain('enhancedtext', false, basename( dirname( __FILE__ ) ) . '/languages' );
}

/**
 * Setup the widget output
 */
function widget( $args, $instance ) {
    $cache = wp_cache_get('EnhancedTextWidget', 'widget');

    if (!is_array($cache)) {
      $cache = array();
    }

    if (!isset($args['widget_id'])) {
      $args['widget_id'] = null;
    }

    if (isset($cache[$args['widget_id']])) {
      echo $cache[$args['widget_id']];
      return;
    }

    ob_start();
    extract($args);

    $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance);    
    $brandName = empty($instance['brandName']) ? '' : $instance['brandName'];
    $THClevel = empty($instance['THClevel']) ? '' : $instance['THClevel'];
    $CBDlevel = empty($instance['CBDlevel']) ? '' : $instance['CBDlevel'];
    $level = empty($instance['level']) ? '' : $instance['level'];
    $strainType = empty($instance['strainType']) ? '' : $instance['strainType'];
    $snapBox = empty($instance['snapBox']) ? '' : $instance['snapBox'];
    $infusedBox = empty($instance['infusedBox']) ? '' : $instance['infusedBox'];
    $houseBox = empty($instance['houseBox']) ? '' : $instance['houseBox'];  
    $price = empty($instance['price']) ? '' : $instance['price'];          
    $text = apply_filters('widget_text', $instance['text'], $instance);


    echo $bare ? '' : $before_widget;
    //Check here after//

    if ($title) {    
        if($strainType) $strain = "<span class='strain super $strainType'>$strainType</span>";
        if($snapBox) $snap = "<span class='snapBox'>SNAP</span>";
        if($infusedBox) $infused = "<span class='infusedBox'>INFUSED</span>";
        if($houseBox) $house = "<span class='houseBox'>HOUSE</span>";            
        if($brandName) $brand = "<span class='brand'>$brandName</span>";
        if($THClevel) $THClevelText = "<span class='level'> $THClevel% THC</span>";
        if($CBDlevel) $CBDlevelText = "<span class='level'>$CBDlevel% CBD</span>";
        if($level) $levelText = "<span class='level'>$level</span>";
        if($price) $priceText = "<span class='price'>$price</span>";            
    } else { 
        echo 'Please enter product name';
    }       
        echo $bare ? $title : $before_title . '<div class="first-line">' . '<span class="spanTitle">' . $title . '</span>' . $strain . $priceText . '</div>' .
            //$snap . $infused . $house 
            '<div class="second-line">' . $brand . $CBDlevelText . $THClevelText . $levelText . '</div>' . $after_title;


    // Parse the text through PHP
    ob_start();
    eval('?>' . $text);
    $text = ob_get_contents();
    ob_end_clean();

    // Run text through do_shortcode
    $text = do_shortcode($text);

    // Echo the content
    $cache[$args['widget_id']] = ob_get_flush();
    wp_cache_set('EnhancedTextWidget', $cache, 'widget');
}

/**
 * Run on widget update
 */
function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    if ( current_user_can('unfiltered_html') )
        $instance['text'] =  $new_instance['text'];
    else
        $instance['text'] = wp_filter_post_kses($new_instance['text']);
    $instance['brandName'] = strip_tags($new_instance['brandName']);
    $instance['THClevel'] = strip_tags($new_instance['THClevel']);
    $instance['CBDlevel'] = strip_tags($new_instance['CBDlevel']);
    $instance['level'] = strip_tags($new_instance['level']);
    $instance['strainType'] = strip_tags($new_instance['strainType']);      
    $instance['snapBox'] = strip_tags($new_instance['snapBox']);    
    $instance['infusedBox'] = strip_tags($new_instance['infusedBox']);  
    $instance['houseBox'] = strip_tags($new_instance['houseBox']);  
    $instance['price'] = strip_tags($new_instance['price']);            
    $this->flush_widget_cache();
    $alloptions = wp_cache_get('alloptions', 'options');
    if (isset($alloptions['EnhancedTextWidget'])) {
      delete_option('EnhancedTextWidget');
    }
    return $instance;
}

/**
 * Flush widget cache
 */
function flush_widget_cache() {
    wp_cache_delete('EnhancedTextWidget', 'widget');
}

/**
 * Setup the widget admin form
 */
function form( $instance ) {
    $instance = wp_parse_args( (array) $instance, array(
        'title' => '',
        'brandName' => '',
        'THClevel' => '',
        'CBDlevel' => '',
        'level' => '',
        'strainType' => '',
        'snapBox' => '',
        'infusedBox' => '',
        'houseBox' => '',
        'price' => '',            
        'text' => ''            
    ));
    $title = $instance['title'];
    $brandName = $instance['brandName'];
    $THClevel = $instance['THClevel'];
    $CBDlevel = $instance['CBDlevel'];
    $level = $instance['level'];
    $strainType = $instance['strainType'];  
    $snapBox = $instance['snapBox'];
    $houseBox = $instance['houseBox'];
    $infusedBox = $instance['infusedBox'];    
    $price = $instance['price'];            
    $text = format_to_edit($instance['text']);
?>
    <style>
        .monospace { font-family: Consolas, Lucida Console, monospace; }
    </style>

    <div class="wrow">
        <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Strain Name', 'enhancedtext'); ?>:</label>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
    </div>

    <div class="wrow">
        <label for="<?php echo $this->get_field_id('brandName'); ?>"><?php _e('Product\'s Brand', 'enhancedtext'); ?>:</label>
        <input class="widefat" id="<?php echo $this->get_field_id('brandName'); ?>" name="<?php echo $this->get_field_name('brandName'); ?>" type="text" value="<?php echo $brandName; ?>" />
    </div>

    <div class="wrow">
        <label for="<?php echo $this->get_field_id('price'); ?>"><?php _e('Price', 'enhancedtext'); ?>:</label>
        <input class="widefat" id="<?php echo $this->get_field_id('price'); ?>" name="<?php echo $this->get_field_name('price'); ?>" type="text" value="<?php echo $price; ?>" />
    </div>

    <div class="wrow">
        <div style="width:48%;margin-right:2%; float: left;">
            <label for="<?php echo $this->get_field_id('THClevel'); ?>"><?php _e('THC %', 'enhancedtext'); ?>:</label>
            <input class="widefat" id="<?php echo $this->get_field_id('THClevel'); ?>" name="<?php echo $this->get_field_name('THClevel'); ?>" type="text" value="<?php echo $THClevel; ?>" />
        </div>
        <div style="width:48%;margin-left:2%;float: left;">
            <label for="<?php echo $this->get_field_id('CBDlevel'); ?>"><?php _e('CBD %', 'enhancedtext'); ?>:</label>
            <input class="widefat" id="<?php echo $this->get_field_id('CBDlevel'); ?>" name="<?php echo $this->get_field_name('CBDlevel'); ?>" type="text" value="<?php echo $CBDlevel; ?>" />
        </div>
    </div>

    <div class="wrow">
        <label for="<?php echo $this->get_field_id('text_area'); ?>">
            <?php echo('Strain Type:'); ?>
        </label><br />
        <div style="width:11%;float: left;margin-right: 2%">
            <label for="<?php echo $this->get_field_id('strainType'); ?>">
                <?php _e('S'); ?><br />
            <input class="" id="<?php echo $this->get_field_id('Sativa'); ?>" name="<?php echo $this->get_field_name('strainType'); ?>" type="radio" value="S" <?php if($strainType === 'S'){ echo 'checked="checked"'; } ?> />
            </label> &nbsp;
        </div>
        <div style="width:11%;float: left;margin-right: 2%">            
            <label for="<?php echo $this->get_field_id('strainType'); ?>">
                <?php _e('Hs'); ?><br />
            <input class="" id="<?php echo $this->get_field_id('Hybrid_Sativa'); ?>" name="<?php echo $this->get_field_name('strainType'); ?>" type="radio" value="Hs" <?php if($strainType === 'Hs'){ echo 'checked="checked"'; } ?> />
            </label> &nbsp;
        </div>
        <div style="width:11%;float: left;margin-right: 2%">                
            <label for="<?php echo $this->get_field_id('strainType'); ?>">
                <?php _e('H'); ?><br />
            <input class="" id="<?php echo $this->get_field_id('Hybrid'); ?>" name="<?php echo $this->get_field_name('strainType'); ?>" type="radio" value="H" <?php if($strainType === 'H'){ echo 'checked="checked"'; } ?> />
            </label> &nbsp;
        </div>
        <div style="width:11%;float: left;margin-right: 2%">            
            <label for="<?php echo $this->get_field_id('strainType'); ?>">
                <?php _e('Hi'); ?><br />
            <input class="" id="<?php echo $this->get_field_id('Hybrid_Indica'); ?>" name="<?php echo $this->get_field_name('strainType'); ?>" type="radio" value="Hi" <?php if($strainType === 'Hi'){ echo 'checked="checked"'; } ?> />
            </label> &nbsp;
        </div>
        <div style="width:11%;float: left;margin-right: 2%">            
            <label for="<?php echo $this->get_field_id('strainType'); ?>">
                <?php _e('I'); ?><br />
            <input class="" id="<?php echo $this->get_field_id('Indica'); ?>" name="<?php echo $this->get_field_name('strainType'); ?>" type="radio" value="I" <?php if($strainType === 'I'){ echo 'checked="checked"'; } ?> />
            </label> &nbsp;
        </div>
        <div style="width:11%;float: left;margin-right: 2%">
            <label for="<?php echo $this->get_field_id('strainType'); ?>">
                <?php _e('CBD'); ?><br />
            <input class="" id="<?php echo $this->get_field_id('CBD'); ?>" name="<?php echo $this->get_field_name('strainType'); ?>" type="radio" value="CBD" <?php if($strainType === 'CBD'){ echo 'checked="checked"'; } ?> />
            </label> 
        </div>
        <div style="width:11%;float: left;">
            <label for="<?php echo $this->get_field_id('strainType'); ?>">
                <?php _e('Clear'); ?><br />
            <input class="" id="<?php echo $this->get_field_id('Clear'); ?>" name="<?php echo $this->get_field_name('strainType'); ?>" type="radio" value="" <?php if($strainType === ''){ echo 'checked="checked"'; } ?> />
            </label> &nbsp;
        </div>

    </div>
    <!--<div class="wrow">
        <div style="width:33%;float: left;">
            <label for="<?php echo $this->get_field_id( 'snapBox' ); ?>">Snap?<br />
            <input class="checkbox" type="checkbox" <?php checked( $instance[ 'snapBox' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'snapBox' ); ?>" name="<?php echo $this->get_field_name( 'snapBox' ); ?>" /></label>
        </div>
        <div style="width:33%;float: left;">
            <label for="<?php echo $this->get_field_id( 'infusedBox' ); ?>">Infused?<br />
            <input class="checkbox" type="checkbox" <?php checked( $instance[ 'infusedBox' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'infusedBox' ); ?>" name="<?php echo $this->get_field_name( 'infusedBox' ); ?>" /></label>
        </div>
        <div style="width:33%;float: left;">
            <label for="<?php echo $this->get_field_id( 'houseBox' ); ?>">House?<br />
            <input class="checkbox" type="checkbox" <?php checked( $instance[ 'houseBox' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'houseBox' ); ?>" name="<?php echo $this->get_field_name( 'houseBox' ); ?>" /></label>
        </div>
    </div>-->

<?php
}
}
/**
* Register the widget
*/

function enhanced_text_widget_init() {
    register_widget('EnhancedTextWidget');
}

add_action('widgets_init', 'enhanced_text_widget_init');

0 个答案:

没有答案