0

I have tried out a few things to remove the <br> being created in between shortcodes, I found one in another thread, it looked promising, but it didn't work out:

function the_content_filter($content) {
    $block = join("|",array("category_product_list", "category_products"));
    $rep = preg_replace("/(<p>)?\[($block)(\s[^\]]+)?\](<\/p>|<br \/>)?/","[$2$3]",$content);
    $rep = preg_replace("/(<p>)?\[\/($block)](<\/p>|<br \/>)?/","[/$2]",$rep);
return $rep;
}
add_filter("the_content", "the_content_filter");

I was reading that wpautop could do the trick but nothing is working for me. Do I need to modify my actual shortcode somehow?

Here's one of the shortcodes I'm working with:

function category_product_list( $atts ) {

    extract(shortcode_atts(array(
        'category'      => '',
        'per_page'  => -1,
        'orderby' => 'title',
        'order' => 'asc'
    ), $atts));

    $meta_query  = WC()->query->get_meta_query();
    $tax_query   = WC()->query->get_tax_query();
    $tax_query[] = array(
        'taxonomy' => 'product_cat',
        'field'    => 'slug',
        'terms'    => array( esc_attr($category) ),
        'operator' => 'IN',
    );
    $args = array(
        'post_type'   =>  'product',
        'post_status' => 'publish',
        'ignore_sticky_posts'   => 1,
        'posts_per_page' => $per_page,
        'orderby' => $orderby,
        'order' => $order,
        'meta_query'  =>  $meta_query,
        'tax_query'   => $tax_query
    );

    $products = new WP_Query( $args );


    if ( $products->have_posts() ) :

        $term_list = wp_get_post_terms($products->post->ID, 'product_cat', array("fields" => "all"));
        $first_term = $term_list[0];
        $first_term_name = $first_term->name;
        $url = get_permalink();
        $id = $products->post->ID;

        // $html_out = '<h2>Courses</h2>';

        $html_out = '<ul class="simple-sitemap-courses">';

            $html_out .= '<li class="page-item page-item-' . $id . '"><a href="' . $url . '">' . $first_term_name . '</a>';

                $html_out .= '<ul class="children">';

                while ($products->have_posts()) :   
                    $products->the_post();  
                    $product = get_product( $products->post->ID );

                    $course_title = get_the_title();
                    $url = get_permalink();
                    $id = $products->post->ID;

                    // Output product information here

                    $html_out .= '<li class="page-item page-item-' . $id . '"><a href="' . $url . '">' . $course_title . '</a></li>';

                endwhile;

                $html_out .= '</ul>';

            $html_out .= '</li>';

        $html_out .= '</ul>';

    else : // No results
        $html_out = "No Courses Found.";
    endif; 

    wp_reset_postdata();

    return $html_out;
}
add_shortcode( 'category_product_list', 'category_product_list' );
2
  • Maybe there is a reason to modify your shortcode, but we can't see it in your question.
    – Max Yudin
    Commented Mar 9, 2017 at 22:39
  • @MaxYudin added it to my OP. Commented Mar 10, 2017 at 15:00

2 Answers 2

1

I created a simple shortcode and added it two times to a page like so:

[my-shortcode]something[/my-shortcode]
[my-shortcode]else[/my-shortcode]

As you can see, I added a line break (<br />) after the first shortcode. So in the frontend it is also shown like this:

something
else

And in the HTML:

<span>something</span>
<br />
<span>else</span>

Than I also used the the_content filter but with strtr() like this:

    function remove_br_from_shortcodes($content){

        // look for the string "]<br />" and replace it just with "]"
        $replace = array (
            ']<br />' => ']'
        );
        $content = strtr($content, $replace);

        return $content;

    }
    add_filter('the_content', 'remove_br_from_shortcodes');

After refreshing the page, the frontend now shows:

something else

In the HTML:

<span>something</span>
<span>else</span>

As the name of the_content filter already suggests, it is a filter for the normal post/page content, not, for example text-widgets or excerpts.

If you also want to remove paragraphs (<p>) than please try adding this in the $replace array: (watch your comma´s!)

'<p>[' => '[',
']</p>' => ']',

With the PHP strtr() function, you can replace certain characters in a string. More to read here.

0

Try wrapping the shortcodes in <div> or <span> tags, if that's an option.

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.