WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#19411 closed defect (bug) (duplicate)

do_shortcode() runs before wpautop() in the text widget

Reported by: nacin Owned by:
Milestone: Priority: high
Severity: major Version: 3.3
Component: General Keywords: has-patch dev-feedback
Focuses: Cc:

Description

Follow-up from #10457, where do_shortcode was added to widget_text.

wpautop() is conditionally run against widget text after the widget_text filter fires. This means that do_shortcode() will run before wpautop(), and it's possible for shortcode_unautop() to run when wpautop() isn't run at all.

Coming up with a patch with a suggested solution.

Attachments (5)

19411.diff (1.9 KB) - added by nacin 3 years ago.
19411.2.diff (2.3 KB) - added by nacin 3 years ago.
19411.3.diff (2.4 KB) - added by nacin 3 years ago.
Alternative also wraps shortcode_unautop(). Not sure if shortcode_unautop() can/should run without wpautop() in the picture.
19411.4.diff (1.0 KB) - added by aaroncampbell 3 years ago.
Manual do_shortcode & shortcode_unautop
19411.5.diff (1.7 KB) - added by aaroncampbell 3 years ago.
Manual do_shortcode & shortcode_unautop

Download all attachments as: .zip

Change History (11)

@nacin3 years ago

comment:1 @nacin3 years ago

  • Keywords has-patch dev-feedback added

19411.diff is based on two things I noticed while creating this patch:

  • Anyone currently adding do_shortcode to widget_text is doing it on priority 10. That means moving it to priority 11 might mean do_shortcode() gets called twice.
  • It is easier to conditionally remove wpautop() than to add it. That is because we need all of them to be on priority 10, but wpautop() added first. Conditionally adding it means removing all three, then conditionally adding them back (perhaps even a has_filter() beforehand, in case a plugin has altered which are added).

In light of that, it seems like the best approach.

comment:2 @westi3 years ago

I don't think this will work after the first text widget with filters disabled because as far as I remember we process filters in the order they were added and therefore the following happens:

  • First widget with filters enabled - wpautop, shortcode_unautop, do_shortcode
  • Second widget with filters disabled - do_shortcode
  • Third widget with filters enabled - do_shortcode, wpautop, shortcode_unautop

This is because when we re-add the filters they will end up behind do_shortcode in ordering.

Alternative solutions to address this:

  • Run wpautop before the widget_text filter - screws over anyone that was expecting it to run afterwards
  • Also do the add remove dance with the do_shortcode filtering in the post widget_text add_filter second - so remove it, add the other two back and then add it again.

comment:3 @aaroncampbell3 years ago

Couldn't we just use a has_filter check to see if we want to add do_shortcode, then add it to priority 11?

if ( ! has_filter( 'widget_text', 'do_shortcode' ) )
	add_filter( 'widget_text', 'do_shortcode', 11 );

Then we're not undoing something that a plugin or theme is already doing, we're not running do_shortcode twice, and we're always running it after wpautop.

comment:4 @nacin3 years ago

westi: Ah, yes, that was the same mess I was trying to avoid.

I like aaroncampbell's ideas. There's also the opportunity to do a maybe_wpautop callback that accepts 2 parameters, thus receiving $instance, and thus being able to determine whether wpautop should be applied.

Example incoming.

@nacin3 years ago

@nacin3 years ago

Alternative also wraps shortcode_unautop(). Not sure if shortcode_unautop() can/should run without wpautop() in the picture.

comment:5 @nacin3 years ago

  • Milestone 3.3 deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Had an IRC discussion with westi. Too many problems with shortcodes in widgets. Closing this as a duplicate of #10457 and re-opening for a revert. Will summarize further problems there.

https://irclogs.wordpress.org/chanlog.php?channel=wordpress-dev&day=2011-12-02&sort=asc#m341059

@aaroncampbell3 years ago

Manual do_shortcode & shortcode_unautop

@aaroncampbell3 years ago

Manual do_shortcode & shortcode_unautop

comment:6 @westi3 years ago

In [19547]:

Revert [18592] because there are issues to solve with autop/shortcode parsing ordering before we add this. See #10457 and #19411

Note: See TracTickets for help on using tickets.