How can I create shortcode that can be used in WordPress Widget

Question

I want to create a Shortcode for a specific function in my WordPress website so that I can also use the shortcode in my site widget to be displayed a specific variable.
Here the code I tried…

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;
</span><span class="com"><!-- start -->
</span><span class="pln">// 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 
    }
    }

 

How can I create shortcode that can be used in WordPress Widget
4.7 (93.33%) 3 vote[s]

in progress 0
David Alex 1 year 1 Answer 258 views Elder 3

Answer ( 1 )

  1. This answer is edited.

    yes

Leave an answer

About David AlexElder