WordPress.org

Ready to get started?Download WordPress

Forums

How to reverse effect of wpautop() and wptexturize() on multi-line shortcode (12 posts)

  1. WillBontrager
    Member
    Posted 1 year ago #

    I have a short code with multi-line content.

    Functions wpautop() and wptexturize() are applied to the shortcode content before my plugin gets a copy of it. The content of this shortcode:

    [shortcode]
    His name is "Able."
    The ball's shape is spherical.
    [/shortcode]

    Becomes:

    <br />
    His name is & #8220;Able.& #8221;<br />
    The ball& #8217;s shape is spherical.<br />

    (Sorry, had to put a space between & and # for entities code to publish.)

    How do I reverse the effect of wpautop() and wptexturize()?

    For the nonce, I'm putting the shortcode content into pre tags with the plugin stripping out the pre tags before using it.

    The content of this shortcode:

    [shortcode]
    <pre>
    His name is "Able."
    The ball's shape is spherical.
    </pre>
    [/shortcode]

    Becomes:

    His name is "Able."
    The ball's shape is spherical.

    I don't like the "use pre tag" solution. It seems so inelegant and there may be issues with it I have not yet encountered.

    Does anyone know how to reverse the effects wpautop() and wptexturize() have on shortcode content?

    Will

  2. Spencer Cameron
    Member
    Posted 1 year ago #

    I'm not sure what your function looks like that creates this shortcode, but you could use something like the following:

    // remove filters.
    remove_filter( 'the_content', 'wpautop' );
    remove_filter( 'the_content', 'wptexturize' );
    
    // no wpautop or wptexturize will be used.
    the_content();
    
    // put things back the way you found them.
    add_filter( 'the_content', 'wpautop' );
    add_filter( 'the_content', 'wptexturize' );

    A simpler solution would be to just use get_the_content(), instead of the_content().

    For instance:

    // this gets the post content before any filters act on it.
    $unfiltered_content = get_the_content();

    Let me know if this helps. :)

  3. WillBontrager
    Member
    Posted 1 year ago #

    Yes, get_the_content() was exactly what I was looking for.

    Thank you, Spencer Cameron!

    Will

  4. Spencer Cameron
    Member
    Posted 1 year ago #

    You're very welcome, Will! If there's anything else you need, just come on back. :D

  5. elibourgault
    Member
    Posted 1 year ago #

    Hi Spencer!

    I didn't understand the solution you gave above... What code do I have to write were exactly? Thanks a lot!!

    Just so you understand my problem better ... I had a problem with <p> tags that were adding automaticaly everywhere by WordPress, even when there was no line-break... Anyways.

    I tried to add remove_filter( 'the_content', 'wpautop' ); (in functions.php) to see what it would do. Then, not liking the result, I erased the line I had just copied in functions.php, but now all the paragraphs of my 120 posts are still all crunched together.

    How do I go back to the way it was before? Thanks!

  6. bcworkz
    Member
    Posted 1 year ago #

    Hi elibourgault,
    This thread related to getting unfiltered shortcode output, so what worked for Will would probably not work for you from the sounds of things. Exactly how to handle your issue will depend on what exactly is generating the undesirable filtered output. As you discovered, removing the wpautop filter affects everything. It must be selectively removed, then added back in in order to only effect a specific behavior. This is why echo get_the_content(); could be preferable to the_content();, but this is only possible where the_content() is used, usually on templates.

    I'm puzzled why the removed filter persisted after you removed the remove_filter() code. Maybe you can add the line add_filter( 'the_content', 'wpautop' ); to functions.php for a while to restore normal behavior. Perhaps when it is later removed, the added filter will also persist? If not, it's not the end of the world to leave it in permanently.

  7. elibourgault
    Member
    Posted 1 year ago #

    Hi bcworkz!

    In the end, add_filter( 'the_content', 'wpautop' ); worked! I also made sure that my p tag had a padding in my style.css file. I was just afraid for a moment that there was no going back after we remove_filter... I had read somewhere that remove_filter( 'the_content', 'wpautop' ); deleated the p tags in the database directly.

    Anyways, everything is back to normal now. Thanks!

    Short survey on this subject :
    Would you guys say it's a good practice to always use remove_filter( 'the_content', 'wpautop' ); ? <p> tags adding anywhere are really not cool...

  8. bcworkz
    Member
    Posted 1 year ago #

    Permanently removing wpautop is an interesting thought. This filter has annoyed me many times, but I've sort of learned to live with it. Removing it now would foul up most of my present posts. If I had known how to remove it from the start, I may very well had done so. Impractical at this point.

    I'd say as a general rule one should not alter the intended operation of WP unless it is necessary to meet your site design and operational goals. Removing this filter would be considered necessary for many, but I would never recommend everyone do so without a good reason.

    We may think this filter is insanely rude, but we know how to code HTML. Considering the majority of WP users cannot code, the filter has it's purposes.

  9. iansane
    Member
    Posted 1 year ago #

    I don't understand why nothing has changed with all the posts on this topic. I never need wordpress altering anything and it is really frustrating. I also think if a client wants a web site they should learn to write basic html or hire someone who knows html to enter all the content.

    There should at least be a setting in the admin panel for 'raw html' that removes all filters for both wpautop and wptexturize and disables the wysiwyg.

    I just added a function to remove all filters to my functions.php file and while I have some concerns about future updates changing the filters, it is working at the moment. Here in the second answer is the function. Stackoverflow

  10. iansane
    Member
    Posted 1 year ago #

    Actually I forgot I modified the above referenced function a little. Here it is.

    function remove_filters() {
        $filters = ['comment_author', 'term_name', 'link_name', 'link_description',
            'link_notes', 'bloginfo', 'wp_title', 'widget_title', 'single_post_title',
            'single_cat_title', 'single_tag_title', 'single_month_title',
            'nav_menu_attr_title', 'nav_menu_description', 'term_description',
            'the_title', 'the_content', 'the_excerpt', 'comment_text', 'list_cats'
        ];
    
        foreach($filters as $filter) {
            remove_filter($filter, 'wpautop');
            remove_filter($filter, 'wptexturize');
        }
    
    }
    add_action('all', 'remove_filters');
  11. thomstark
    Member
    Posted 10 months ago #

    There's a much simpler way, applied per attribute, or to the content, within the shortcode itself. Just throw the following after the atts and before the return:

    $message = str_replace("br", "\n", $message);
    $message = str_replace("br /", "\n", $message);
    $message = str_replace("</p><p>", "\n", $message);

    where $message is the name of the attribute in which you want multiline text. Imagine there are <> around the br tags.

  12. context
    Member
    Posted 9 months ago #

    "Please provide option to disable wptexturize entirely"
    http://core.trac.wordpress.org/ticket/19550

    Bug report is $#awaiting-review and #has-patch.

    Patch works but we need to redo every update.

Topic Closed

This topic has been closed to new replies.

About this Topic