remove empty

tags from wordpress shortcodes via a php functon

前端 未结 7 1848
余生分开走
余生分开走 2021-02-13 10:20

Looking for a php function (non-jQuery or wpautop modification) approach to remove

from within wordpress.

I tried this but it does not w

7条回答
  •  你的背包
    2021-02-13 11:18

    This is an old question, but I solved this today and thought I'd share.

    In my case, I basically want to remove all the poorly formatted

    and
    tags, but then you want to add them back in correctly so that the text in the shortcode gets formatted correctly.

    /*
     * Half column shortcode
     */
        function custom_shortcode_half_column( $atts, $content = '') {
            $content = custom_filter_shortcode_text($content);
            return '
    '. $content .'
    '; } add_shortcode( 'half-column', 'custom_shortcode_half_column' ); /* * Utility function to deal with the way WordPress auto formats text in a shortcode. */ function custom_filter_shortcode_text($text = '') { // Replace all the poorly formatted P tags that WP adds by default. $tags = array("

    ", "

    "); $text = str_replace($tags, "\n", $text); // Remove any BR tags $tags = array("
    ", "
    ", "
    "); $text = str_replace($tags, "", $text); // Add back in the P and BR tags again, remove empty ones return apply_filters('the_content', $text); }

    This really should be the default way WordPress parses the shortcode $content parameter in my opinion.

提交回复
热议问题