1

First things first,

I am making a theme in which I have make some widgets that are listed below.

  1. About Me Widget
  2. Subscribe Me widget
  3. AD widget

Now what I thought that if I could implement my widget using shortcodes.

So to implement widgets using shortcodes I have made a function that implements the widgets using the classname and it works fine but as not expected.

So what's the problem?

The Problem, widgets that I have made in my theme are different with each other Obesily.

What I mean?

I mean that My all three widgets contains the title, to display.

  1. If I add the about us widget using shortcode it gives me the error undefined index title ( with the directory location of my about us file ). NOTE: The solution of this problem is mentioned below.

  2. If I add the Subscribe me widget using shortcode no undefined title error. The reason is the index is same but.

  3. In AD widget I got three errors and they are undefined index ad_title, ad_img_url, ad_link_url. I know why the errors are coming -> Reason: index name are different.

  4. Two More things.

    1. I am having one more problem with about me widgets and that is the the is only displaying but the about me content is not why? If I try two add the about me index then also nothing got printed.

          extract(shortcode_atts(array(
              'widget_name' => FALSE,
              'title' => '',                  
              'aboutMe' => ''     // BY  ADDING  THE  INDEX  TITLE
          ), $atts));
      
    2. The error occurs for index only in the input fields. It does not occurs in the textarea field that I have used for about me content.

    How I solved the First problem?

    I have solved the first problem by adding the index name.

    function widgetShortcodes( $atts ) {
    
        global $wp_widget_factory;
    
        extract(shortcode_atts(array(
            'widget_name' => FALSE,
            'title' => '',                  // BY  ADDING  THE  INDEX  TITLE
        ), $atts));
        // do shortcode actions here
    
        $widget_name = esc_html($widget_name);
    
        if (!is_a($wp_widget_factory->widgets[$widget_name], 'WP_Widget')):
            $wp_class = 'WP_Widget_'.ucwords(strtolower($class));
    
            if (!is_a($wp_widget_factory->widgets[$wp_class], 'WP_Widget')):
                return '<p>'.sprintf(__("%s: Widget class not found. Make sure this widget exists and the class name is correct"),'<strong>'.$wp_class.'</strong>').'</p>';
            else:
                $class = $wp_class;
            endif;
        endif;
    
        $args = array(
            'before_widget' => '',
            'after_widget' => '',
            'before_title' => '',
            'after_title' => ''
        );
    
        ob_start();
        the_widget($widget_name, $atts, $args);
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    
    }
    add_shortcode( 'widget','widgetShortcodes' );
    

What I want to know?

I want to know that I have to add each and ever index name if I make a new widget with different or new index. Is there would be the easy or more dynamic way to do this.

I think I have one and that is to make the shortcode for each and every widget but that's a long process.

Any Suggessions would be appriciated?

By the way I want to notify you that I have make this widget shortcode function using this link -> Call a Widget with a Shortcode

UPDATE:

The Function is only working for two wordpress inbuilt widgets and they are.

  1. Archives
  2. Recent_Posts

and the function is not giving error but also not previewing any result

  1. RSS
  2. Tag_Cloud
  3. Nav_Menu_Widget

other wp classes triggering the errors.

  1. Calendar
  2. Categories
  3. Links 4.Meta 5.Pages 6.Recent Comments 7.Search (a search from) 8.Text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.