0

I have this widget that im working on and it allows the user to input a shortcode and it will display it in the sidebar. But its not working right. I can input the short code but it has no output. Also it seams to strip the [] and if you include size=35, the 35 is stripped after you hit the save button. Here is part of the code:

     //Set Shortcode 1 title
    $sc1title = apply_filters( 'Shortcode 1 Title', $instance['sc1title'] );

    //Display Shortcode 1 title
    if ( ! empty( $sc1title ) )
        echo $args['before_sc1title'] . "<h3>$sc1title</h3>" . $args['after_sc1title'];

    //Do Shortcode 1
    //echo do_shortcode('sc1');
    //echo do_shortcode($this->shortcodes[$instance['sc1']]);
    echo do_shortcode('[sc1]');

echo "<br>";

    //Set Shortcode 2 title
    $sc2title = apply_filters( 'Shortcode 2 Title', $instance['sc2title'] );

    //Display Shortcode 2 title
    if ( ! empty( $sc2title ) )
        echo $args['before_sc1title'] . "<h3>$sc2title</h3>" . $args['after_sc1title'];

    //Do Shortcode 1
    //echo do_shortcode('sc2');
    echo do_shortcode($this->shortcodes[$instance['sc2']]); 

    echo $args['after_widget'];



    // Display after widget code
    echo $after_widget;
}

// Update the widget settings.
public function update( $new_instance, $old_instance ) {
    $instance['title'] = strip_tags( stripslashes($new_instance['title']) );
    $instance['mtitle'] = strip_tags( stripslashes($new_instance['mtitle']) );
    $instance['nav_menu'] = (int) $new_instance['nav_menu'];
    $instance['sc1title'] = strip_tags( stripslashes($new_instance['sc1title']) );
    $instance['sc1'] = strip_tags( stripslashes($new_instance['sc1']) );
    $instance['sc2title'] = strip_tags( stripslashes($new_instance['sc2title']) );
    $instance['sc2'] = strip_tags( stripslashes($new_instance['sc2']) );
    return $instance;

}

// Displays the form on the widget page
public function form( $instance ) {
    $title = isset( $instance['title'] ) ? $instance['title'] : '';
    $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
    $mtitle = isset( $instance['mtitle'] ) ? $instance['mtitle'] : '';
    $sc1title = isset( $instance['sc1title'] ) ? $instance['sc1title'] : '';
    $sc1 = isset( $instance['sc1'] ) ? $instance['sc1'] : '';
    $sc2title = isset( $instance['sc2title'] ) ? $instance['sc2title'] : '';
    $sc2 = isset( $instance['sc2'] ) ? $instance['sc2'] : '';

    // Get menus
    $menus = wp_get_nav_menus( array( 'orderby' => 'name' ) );

    // If no menus exists, direct the user to go and create some.
    if ( !$menus ) {
        echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php') ) .'</p>';
        return;
    }
    ?>
    <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'wpspprofwid' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />   
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'mtitle' ); ?>"><?php _e( 'Menu Title:' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id('mtitle'); ?>" name="<?php echo $this->get_field_name('mtitle'); ?>" value="<?php echo $mtitle; ?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'nav_menu' ); ?>"><?php _e( 'Select Menu:' ); ?></label>
        <select id="<?php echo $this->get_field_id( 'nav_menu' ); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
    <?php
        foreach ( $menus as $menu ) {
            echo '<option value="' . $menu->term_id . '"'
                . selected( $nav_menu, $menu->term_id, false )
                . '>'. $menu->name . '</option>';
        }
    ?>
        </select>
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'sc1title' ); ?>"><?php _e( 'Shortcode 1 Title:' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id('sc1title'); ?>" name="<?php echo $this->get_field_name('sc1title'); ?>" value="<?php echo $sc1title; ?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'sc1' ); ?>"><?php _e( 'Shortcode 1:' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id('sc1'); ?>" name="<?php echo $this->get_field_name('sc1'); ?>" value="<?php echo $sc1; ?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'sc1title' ); ?>"><?php _e( 'Shortcode 2 Title:' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id('sc2title'); ?>" name="<?php echo $this->get_field_name('sc2title'); ?>" value="<?php echo $sc2title; ?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'sc2' ); ?>"><?php _e( 'Shortcode 2:' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id('sc2'); ?>" name="<?php echo $this->get_field_name('sc2'); ?>" value="<?php echo $sc2; ?>" />
    </p>

1 Answer 1

0

I have a feeling that Wordpress does not allow you to use shortcodes in sidebars by default.

Have you added

add_filter('widget_text', 'do_shortcode');

To functions.php

1
  • No I don't want to edit function.php. and the widget will do the shortcodes if they are in the echo do_shortcode function the thing I would like is to be able to change the shortcodes via the admin panel and not have to rewrite the plugin every time shortcodes are added or removed
    – MJ135
    Commented May 15, 2014 at 23:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.