如何使用短代码创建wordpress小部件

时间:2016-05-25 05:30:17

标签: wordpress widget shortcode

我想创建一个小部件,当在帖子中激活短代码时,它将在wordpress中创建。我试图把所有东西都拿到$ all然后返回它但是没有成功。我想在不使用任何插件的情况下这样做。

    function movie($atts) { 


  class wpb_widget extends WP_Widget {

    function __construct() {
    parent::__construct(
    // Base ID of your widget
    'wpb_widget', 

    // Widget name will appear in UI
    __('WPBeginner Widget', 'wpb_widget_domain'), 

    // Widget description
    array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', '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'] );
    // before and after widget arguments are defined by themes
    global $wpdb;
    $b = shortcode_atts(array(
    'name' => 'Deadpool',
    ), $atts);
    $vypis = $wpdb->get_row ("SELECT * FROM hodnoceni WHERE nazev = '" . $atts['name'] . "'", ARRAY_A);

    $all = $args['before_widget'];
    if ( ! empty( $vypis['nazev'] ) )

    // This is where you run the code and display the output
    $all .= $args['before_title'] . $title . $args['after_title'] . '<p style="margin:0px;">' . $vypis['rok'] . '<br>' . $vypis['delka'] . ' min</p><div class="my-image"><img src="' . $vypis['src'] . '" height="130" alt="' . $vypis['nazev'] . '"> </div> <div class="my-content">' . $vypis['clanek'] . '</div> </div>';
    $all .= $args['after_widget'];
    }

    // Widget Backend 
    public function form( $instance ) {
    if ( isset( $instance[ 'title' ] ) ) {
    $title = $instance[ 'title' ];
    }
    else {
    $title = __( 'New title', '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>
    <?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'] ) : '';
    return $instance;
    }
    } // Class wpb_widget ends here
    return $all;
}

function wpb_load_widget() {
    register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );



function register_shortcodes(){
   add_shortcode('bacon', 'bacon_function');
   add_shortcode('movie_form', 'movie_form');
   add_shortcode('movie', 'movie');
}
add_action( 'init', 'register_shortcodes');

1 个答案:

答案 0 :(得分:0)

对于您的小部件类“wpb_widget”,您可以使用以下代码:

<?php add_shortcode( 'show_movie', 'movie_shortcode' );
function movie_shortcode( $atts ) {
    // Configure defaults and extract the attributes into variables
    extract( shortcode_atts(
        array(
            'type'   => 'wpb_widget'
        ),
        $atts
    ));

    $args = array( //optional markup:
        'before_widget' => '<div class="box widget scheme-' . $scheme . ' ">',
        'after_widget'  => '</div>',
        'before_title'  => '<div class="widget-title">',
        'after_title'   => '</div>',
    );

    ob_start();
    the_widget( $type, $atts, $args );
    $output = ob_get_clean();
    return $output;
}

然后使用此代码

[show_movie]

运行小部件。