1

I want to create a shortcode for my widget.

This is my code for creating a widget, if I want to create a shortcode for this widget what do I do?

<pre>
function my_slider_post() {
    register_widget('my_slider_post');
    }
    add_action('widgets_init','my_slider_post');




class my_slider_post extends WP_Widget {
    function my_slider_post() {

        $widget_ops = array('classname' => 'my_slider_post','description' => __('Widget display Slider Post','theme'));
         add_action('admin_enqueue_scripts', array($this, 'scripts'));
        parent::__construct('mysliderpost',__('my Slider Post','theme'),$widget_ops);

        }

    function widget( $args, $instance ) {
        extract( $args );
        /* User-selected settings. */
    $title = apply_filters('widget_title', $instance['title'] );
    $img1 = $instance['image_one'];
    $title1 = $instance['title_one'];
    $desc1 = $instance['desc_one'];
    $img2 = $instance['image_two'];
    $img3 = $instance['image_three'];

        /* Before widget (defined by themes). */
        echo $before_widget;

        /* Title of widget (before and after defined by themes). */
        if ( $title )
            echo $before_title . $title . $after_title;



<!-- start -->

// code html to show





        /* After widget (defined by themes). */
        echo $after_widget;

    }

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

        /* Strip tags (if needed) and update the widget settings. */
            $instance['title'] = strip_tags( $new_instance['title'] );
            $instance['image_one'] = $new_instance['image_one'];
            $instance['title_one'] = $new_instance['title_one'];
            $instance['desc_one'] = $new_instance['desc_one'];
            $instance['image_two'] = $new_instance['image_two'];
            $instance['image_three'] = $new_instance['image_three'];

        return $instance;
    }
    public function scripts()
    {
       wp_enqueue_script( 'media-upload' );
       wp_enqueue_media();
       wp_enqueue_script('our_admin', get_template_directory_uri() . '/js/our_admin.js', array('jquery'));
    }
    public function form( $instance ) {
    // code html to show on widget into sidebar control panel 

     }



    }



</pre>

2 Answers 2

1

For your widget class "my_slider_post " you can use this code:

<?php add_shortcode( 'show_slider', 'slider_shortcode' );
function slider_shortcode( $atts ) {
    // Configure defaults and extract the attributes into variables
    extract( shortcode_atts(
        array( 
            'type'   => 'my_slider_post '
            //you can add here more parameters below
        ),
        $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;
}

and then use this code

[show_slider]

to run the widget.

I used this article: https://www.smashingmagazine.com/2012/12/inserting-widgets-with-shortcodes/

Sign up to request clarification or add additional context in comments.

Comments

0

Add

add_shortcode('the_slider_short_code', 'my_slider_post'); after class my_slider_post extends WP_Widget and call shortcode as [the_slider_short_code] in your post, page or custom post type.

Alternatively you can use amr shortcode plugin to generate shortcode for any widgets. You can read more about it from here.

Let me know how it goes.

1 Comment

Hi Zipal_ Thank you for your answer bro, i did what you told me i made add_shortcode('the_slider_short_code', 'my_slider_post'); after class my_slider_post extends WP_Widget and when i call shortcode as [the_slider_short_code] in my custom post type or my post nothing work !! PS : i know amr shortcode but i need to create shortcode without it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.