如何修复这个Wordpress小部件

时间:2015-02-23 00:28:56

标签: php wordpress widget

任何人都可以帮助我,如何使这个小部件工作?它是从原始代码复制的,但在一个文件中有许多小部件。我只选了这个,因为我需要这个独立的。 我不能把它作为插件加载。 这是代码:



<?php 

/**
----> Custom Pricing Box Widget
**/

add_action( 'widgets_init', 'load_pricing_box_widget' );

function load_pricing_box_widget() {
	register_widget( 'Price_Box' );
}


class Price_Box extends WP_Widget {

	function Price_Box() {
		$widget_ops = array( 'classname' => 'plan', 'description' => __('Custom Pricing box widget - for the Home-Page only!', 'example') );

		$control_ops = array( 'width' => 300, 'height' => 550, 'id_base' => 'pricing-box' );

		$this->WP_Widget( 'pricing-box', __('Bluz Themes - Pricing Box', 'example'), $widget_ops, $control_ops );
	}

	function widget( $args, $instance ) {
		extract( $args );

		$title = apply_filters('widget_title', $instance['title']);
		$content = $instance['content'];
		$price = $instance['price'];
		$longer = $instance['longer'];
		$per = $instance['per'];
		$button_link = $instance['button_link'];
		$button_color = $instance['button_color'];

		echo $before_widget;
			
		if ( $title )
			echo $before_title . $title . $after_title;
		
		echo '<div class="content">';
		if ( $longer ) { echo '<div class="price" style="font-size:20px;">'; } else {echo '<div class="price">';}
		        
                 echo '<div class="inside">';
					echo $price.'<br><span>'.$per.'</span>';
				 echo '</div>';
					echo '<a href="'.$button_link.'" class="sml-btn '.$button_color.'">Order Now!</a>';
			echo '</div>';
		
		
		if ( $content )
			printf( '%1$s' , $content );

		

		echo '</div>';
		echo $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags for title and name to remove HTML (important for text inputs). */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['content'] = stripslashes( $new_instance['content'] );
		$instance['price'] = strip_tags( $new_instance['price'] );
		$instance['longer'] = $new_instance['longer'];
		$instance['per'] = strip_tags($new_instance['per']);
		$instance['button_link'] = strip_tags($new_instance['button_link']);
		$instance['button_color'] = $new_instance['button_color'];

		return $instance;
	}

	function form( $instance ) {

		$defaults = array( 'title' => __('Pricing Box Title', 'example'), 'content' => __('This is our pricing box content, you can use html to style it', 'example'), 'button_color' => __('red', 'example'));
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

		<!-- Widget Title: Text Input -->
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Pricing Box Title:', 'hybrid'); ?></label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:97%;" class="widefat" />
		</p>
        

		<!-- Your Content: Text Input -->
		<p>
			<label for="<?php echo $this->get_field_id( 'content' ); ?>"><?php _e('Content (You can use HTML):', 'example'); ?></label>
			<textarea id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>" type="textarea" cols="12" rows="12" class="widefat" ><?php echo $instance['content']; ?></textarea>
		</p>
        
        <!-- Widget Price: Text Input -->
		<p>
			<label for="<?php echo $this->get_field_id( 'price' ); ?>"><?php _e('Price:', 'example'); ?></label>
			<input id="<?php echo $this->get_field_id( 'price' ); ?>" name="<?php echo $this->get_field_name( 'price' ); ?>" value="<?php echo $instance['price']; ?>" style="width:97%;" class="widefat" />
		</p>
        
        <!-- Widget Per: Text Input -->
		<p>
			<label for="<?php echo $this->get_field_id( 'per' ); ?>"><?php _e('Price Per... <em>(examples: /month, /year, /day)</em>:', 'example'); ?></label>
			<input id="<?php echo $this->get_field_id( 'per' ); ?>" name="<?php echo $this->get_field_name( 'per' ); ?>" value="<?php echo $instance['per']; ?>" style="width:97%;" class="widefat" />
		</p>

		<!-- Price Longer? Checkbox -->
		<p>
			<input class="checkbox" type="checkbox" <?php if( $instance['longer'] == true ) { ?>checked="checked"<?php } ?> id="<?php echo $this->get_field_id( 'longer' ); ?>" name="<?php echo $this->get_field_name( 'longer' ); ?>" /> 
			<label for="<?php echo $this->get_field_id( 'longer' ); ?>"><?php _e('<strong>Check this if your price is longer the 2 Didgets</strong>', 'example'); ?></label>
		</p>
        
        <!-- Widget Button Link: Text Input -->
		<p>
			<label for="<?php echo $this->get_field_id( 'button_link' ); ?>"><?php _e('The url you want the "order now!" button to link to:', 'example'); ?></label>
			<input id="<?php echo $this->get_field_id( 'button_link' ); ?>" name="<?php echo $this->get_field_name( 'button_link' ); ?>" value="<?php echo $instance['button_link']; ?>" style="width:97%;" class="widefat" />
		</p>
        
        <!-- Button Color: Select Box -->
		<p>
			<label for="<?php echo $this->get_field_id( 'button_color' ); ?>"><?php _e('Choose a color for the "order now!" button <em>(default color is red)</em>:', 'example'); ?></label> 
			<select id="<?php echo $this->get_field_id( 'button_color' ); ?>" name="<?php echo $this->get_field_name( 'button_color' ); ?>" class="widefat" style="width:100%;">
				<option <?php if ( 'red' == $instance['button_color'] ) echo 'selected="selected"'; ?>>red</option>
				<option <?php if ( 'orange' == $instance['button_color'] ) echo 'selected="selected"'; ?>>orange</option>
				<option <?php if ( 'green' == $instance['button_color'] ) echo 'selected="selected"'; ?>>green</option>
				<option <?php if ( 'blue' == $instance['button_color'] ) echo 'selected="selected"'; ?>>blue</option>
				<option <?php if ( 'grey' == $instance['button_color'] ) echo 'selected="selected"'; ?>>grey</option>
				<option <?php if ( 'black' == $instance['button_color'] ) echo 'selected="selected"'; ?>>black</option>
				<option <?php if ( 'special' == $instance['button_color'] ) echo 'selected="selected"'; ?>>special</option>
			</select>
		</p>
?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

更新您的代码,而不是邮政编码。

<?php 

/**
----> Custom Pricing Box Widget
**/

add_action( 'widgets_init', 'load_pricing_box_widget' );

function load_pricing_box_widget() {
    register_widget( 'Price_Box' );
}


class Price_Box extends WP_Widget {

    function Price_Box() {
        $widget_ops = array( 'classname' => 'plan', 'description' => __('Custom Pricing box widget - for the Home-Page only!', 'example') );

        $control_ops = array( 'width' => 300, 'height' => 550, 'id_base' => 'pricing-box' );

        $this->WP_Widget( 'pricing-box', __('Bluz Themes - Pricing Box', 'example'), $widget_ops, $control_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );

        $title = apply_filters('widget_title', $instance['title']);
        $content = $instance['content'];
        $price = $instance['price'];
        $longer = $instance['longer'];
        $per = $instance['per'];
        $button_link = $instance['button_link'];
        $button_color = $instance['button_color'];

        echo $before_widget;

        if ( $title )
            echo $before_title . $title . $after_title;

        echo '<div class="content">';
        if ( $longer ) { echo '<div class="price" style="font-size:20px;">'; } else {echo '<div class="price">';}

                 echo '<div class="inside">';
                    echo $price.'<br><span>'.$per.'</span>';
                 echo '</div>';
                    echo '<a href="'.$button_link.'" class="sml-btn '.$button_color.'">Order Now!</a>';
            echo '</div>';


        if ( $content )
            printf( '%1$s' , $content );



        echo '</div>';
        echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        /* Strip tags for title and name to remove HTML (important for text inputs). */
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['content'] = stripslashes( $new_instance['content'] );
        $instance['price'] = strip_tags( $new_instance['price'] );
        $instance['longer'] = $new_instance['longer'];
        $instance['per'] = strip_tags($new_instance['per']);
        $instance['button_link'] = strip_tags($new_instance['button_link']);
        $instance['button_color'] = $new_instance['button_color'];

        return $instance;
    }

    function form( $instance ) {

        $defaults = array( 'title' => __('Pricing Box Title', 'example'), 'content' => __('This is our pricing box content, you can use html to style it', 'example'), 'button_color' => __('red', 'example'));
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

        <!-- Widget Title: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Pricing Box Title:', 'hybrid'); ?></label>
            <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:97%;" class="widefat" />
        </p>


        <!-- Your Content: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( 'content' ); ?>"><?php _e('Content (You can use HTML):', 'example'); ?></label>
            <textarea id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>" type="textarea" cols="12" rows="12" class="widefat" ><?php echo $instance['content']; ?></textarea>
        </p>

        <!-- Widget Price: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( 'price' ); ?>"><?php _e('Price:', 'example'); ?></label>
            <input id="<?php echo $this->get_field_id( 'price' ); ?>" name="<?php echo $this->get_field_name( 'price' ); ?>" value="<?php echo $instance['price']; ?>" style="width:97%;" class="widefat" />
        </p>

        <!-- Widget Per: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( 'per' ); ?>"><?php _e('Price Per... <em>(examples: /month, /year, /day)</em>:', 'example'); ?></label>
            <input id="<?php echo $this->get_field_id( 'per' ); ?>" name="<?php echo $this->get_field_name( 'per' ); ?>" value="<?php echo $instance['per']; ?>" style="width:97%;" class="widefat" />
        </p>

        <!-- Price Longer? Checkbox -->
        <p>
            <input class="checkbox" type="checkbox" <?php if( $instance['longer'] == true ) { ?>checked="checked"<?php } ?> id="<?php echo $this->get_field_id( 'longer' ); ?>" name="<?php echo $this->get_field_name( 'longer' ); ?>" /> 
            <label for="<?php echo $this->get_field_id( 'longer' ); ?>"><?php _e('<strong>Check this if your price is longer the 2 Didgets</strong>', 'example'); ?></label>
        </p>

        <!-- Widget Button Link: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( 'button_link' ); ?>"><?php _e('The url you want the "order now!" button to link to:', 'example'); ?></label>
            <input id="<?php echo $this->get_field_id( 'button_link' ); ?>" name="<?php echo $this->get_field_name( 'button_link' ); ?>" value="<?php echo $instance['button_link']; ?>" style="width:97%;" class="widefat" />
        </p>

        <!-- Button Color: Select Box -->
        <p>
            <label for="<?php echo $this->get_field_id( 'button_color' ); ?>"><?php _e('Choose a color for the "order now!" button <em>(default color is red)</em>:', 'example'); ?></label> 
            <select id="<?php echo $this->get_field_id( 'button_color' ); ?>" name="<?php echo $this->get_field_name( 'button_color' ); ?>" class="widefat" style="width:100%;">
                <option <?php if ( 'red' == $instance['button_color'] ) echo 'selected="selected"'; ?>>red</option>
                <option <?php if ( 'orange' == $instance['button_color'] ) echo 'selected="selected"'; ?>>orange</option>
                <option <?php if ( 'green' == $instance['button_color'] ) echo 'selected="selected"'; ?>>green</option>
                <option <?php if ( 'blue' == $instance['button_color'] ) echo 'selected="selected"'; ?>>blue</option>
                <option <?php if ( 'grey' == $instance['button_color'] ) echo 'selected="selected"'; ?>>grey</option>
                <option <?php if ( 'black' == $instance['button_color'] ) echo 'selected="selected"'; ?>>black</option>
                <option <?php if ( 'special' == $instance['button_color'] ) echo 'selected="selected"'; ?>>special</option>
            </select>
        </p>
        <?php }
}
?>

因为你永远不会结束你的功能名称和类名。