Ibtekarlabs
Ibtekarlabs

Reputation: 43

Empty Paragraph tag before and after Wordpress widget

Am getting empty paragraph tags

before and after the widget content when using shortcode in the widget.

<div class="widget">
    <p></p>
    <div class="social-media">
     <span></span>
    </div>
   <p></p>
</div>

I created the shortcode with the code below

<?php
function social_media_shortcode($atts){
    $atts = shortcode_atts(array(
        'class' => 'social-media',
        'items' => 'facebook,twitter,insta,behance,google,linkedin,phone,mail,whatsapp', // Default items array
    ), $atts, 'social_media');

    $itemsArray = explode(',', $atts['items']);

    ob_start();
   ?>
   <div class="<?= esc_attr($atts['class']) ?>">
       <span><?= social_groups($itemsArray) ?></span>
   </div>
   <?php
  $content = ob_get_clean();
  return $content;
}
add_shortcode('social_media', 'social_media_shortcode');

I need a hook to fix or str_replace the empty p tags

Upvotes: 0

Views: 276

Answers (1)

Ibtekarlabs
Ibtekarlabs

Reputation: 43

I found out that the

tags are added by the default Gutenberg shortcakes widget, solved the issue by creating custom widget and add the shortcodes using it

<?php
 // Register shortcode widget
class Shortcode_Widget extends WP_Widget {
    public function __construct() {
        parent::__construct(
            'shortcode_widget',
            __( 'Shortcode Widget', 'text_domain' ),
            array(
                'description' => __( 'A widget that displays a shortcode.', 'text_domain' ),
            )
        );
    }

    public function widget( $args, $instance ) {
        $shortcode = ! empty( $instance['shortcode'] ) ? $instance['shortcode'] : '';
        echo do_shortcode( $shortcode );
    }

    public function form( $instance ) {
        $shortcode = ! empty( $instance['shortcode'] ) ? $instance['shortcode'] : '';
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'shortcode' ); ?>"><?php _e( 'Shortcode:' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'shortcode' ); ?>" name="<?php echo $this->get_field_name( 'shortcode' ); ?>" type="text" value="<?php echo esc_attr( $shortcode ); ?>" />
        </p>
        <?php
    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['shortcode'] = ! empty( $new_instance['shortcode'] ) ? $new_instance['shortcode'] : '';

        return $instance;
    }
}
function register_shortcode_widget() {
    register_widget( 'Shortcode_Widget' );
}
add_action( 'widgets_init', 'register_shortcode_widget' );

Upvotes: 0

Related Questions