我正在尝试创建一个显示当前窗口小部件及其内容的短代码。它似乎对wordpress默认小部件完全正常,但是当涉及到我的自定义小部件时,它不起作用。关于如何解决它的任何想法?
Shortcode(我认为问题在于$ widget_id无法处理自定义小部件):
function widget_shortcodes($atts) {
global $wp_widget_factory;//Connects to all registered widgets
$parts = shortcode_atts(array( //sets the attributes for the shortcode
'id' => '1', //sets the id field of the shortcode
'type' => 'text' //sets the type field of the shortcode
), $atts );
$widget_id = $atts['id']; //sets the widget id variable to the input id in the shortcode
//this if/elseif statement block checks to see the value of the type field and sets the widget type variable depending on the value
if($atts['type'] == 'calendar'){
$widget_type = 'WP_Widget_Calendar';
} elseif ($atts['type'] == 'posts') {
$widget_type = 'My_Recent_Posts_Widget';
} elseif ($atts['type'] == 'text') {
$widget_type = 'WP_Widget_Text';
} elseif ($atts['type'] == 'form') {
$widget_type = 'quickform_widget';
} elseif ($atts['type'] == 'newsletter') {
$widget_type = 'newsletter_widget';
} elseif ($atts['type'] == 'reviews') {
$widget_type = 'reviews_widget';
}
ob_start(); //initiates the object
if(!empty($widget_type)) {
the_widget($widget_type, $instance, array('widget_id'=> $widget_id)); //gets the widget if the type is correct
$widget_output = ob_get_contents(); //sets the widget output to the object content
} else { //if the type is not supported it displays an error message
echo "<p>Please make sure you have set a valid type in your shortcode.</p>";
echo "<p>Accepted Types: calendar, posts, text, form, newsletter and reviews.<p>";
$widget_output = ob_get_contents(); //sets the widget output to the object content
}
ob_end_clean(); //ends the object
return $widget_output; //returns the widget code
} add_shortcode('widget','widget_shortcodes');
自定义小部件:
<?php // Creating the widget
class newsletter_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'newsletter_widget',
// Widget name will appear in UI
__('Newsletter', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'Newsletter widget', 'wpb_widget_domain' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
$code = apply_filters( 'widget_code', $instance['code'] );
$desc = apply_filters( 'widget_desc', $instance['desc'] );
?>
<aside class="widget widget_newsletter">
<h3><?php echo $title; ?></h3>
<p><?php echo ( $desc ); ?></p>
<?php echo do_shortcode($code);?>
</aside>
<?php }
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Receive our monthly newsletter', 'wpb_widget_domain' );
}
if ( isset( $instance[ 'code' ] ) ) {
$code = $instance[ 'code' ];
}
else {
$code = __( 'Form Code...', 'wpb_widget_domain' );
}
if ( isset( $instance[ 'desc' ] ) ) {
$desc = $instance[ 'desc' ];
}
else {
$desc = __( 'Newsletter description...', 'wpb_widget_domain' );
}
// Widget admin form?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></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 esc_attr( $title ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'desc' ); ?>"><?php _e('Newsletter Description:'); ?></label>
<textarea class="widefat" id="<?php echo $this->get_field_id( 'desc' ); ?>" name="<?php echo $this->get_field_name( 'desc' ); ?>" type="text" value="<?php echo esc_attr( $desc ); ?>"><?php echo $desc;?></textarea>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'code' ); ?>"><?php _e( 'Form code:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'code' ); ?>" name="<?php echo $this->get_field_name( 'code' ); ?>" type="text" value="<?php echo esc_attr( $code ); ?>" />
</p>
<?php }
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['code'] = ( ! empty( $new_instance['code'] ) ) ? strip_tags( $new_instance['code'] ) : '';
$instance['desc'] = ( ! empty( $new_instance['desc'] ) ) ? strip_tags( $new_instance['desc'] ) : '';
return $instance;
}
}
// Register and load the widget
function newsletter_load_widget() {
register_widget( 'newsletter_widget' );
}
add_action( 'widgets_init', 'newsletter_load_widget' );?>