#59206 closed defect (bug) (fixed)
PHP warning/error when using `[caption]` shortcode without attributes
Reported by: | maltfield | Owned by: | |
---|---|---|---|
Milestone: | 6.4 | Priority: | normal |
Severity: | normal | Version: | 6.3 |
Component: | Media | Keywords: | has-patch |
Focuses: | Cc: |
Description
There appears to be a bug in WordPress 6.3 that causes a PHP Fatal Error when processing a "[caption]" shortcode that does not have an "id" option defined.
Example error
==> tech.michaelaltfield.net/error.log <== [Wed Aug 23 00:00:09.968658 2023] [php:error] [pid 459909] [client 0.0.0.0:0] PHP Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /var/www/html/wordpress/htdocs/wp-includes/media.php:2147\nStack trace:\n#0 [internal function]: img_caption_shortcode()\n#1 /var/www/html/wordpress/htdocs/wp-includes/shortcodes.php(356): call_user_func()\n#2 [internal function]: do_shortcode_tag()\n#3 /var/www/html/wordpress/htdocs/wp-includes/shortcodes.php(228): preg_replace_callback()\n#4 [internal function]: do_shortcode()\n#5 /var/www/html/wordpress/htdocs/wp-includes/class-wp-hook.php(307): call_user_func_array()\n#6 /var/www/html/wordpress/htdocs/wp-includes/plugin.php(191): WP_Hook->apply_filters()\n#7 /var/www/html/wordpress/htdocs/wp-content/themes/redacted/functions.php(309): apply_filters()\n#8 [internal function]: bfa_wp_trim_excerpt()\n#9 /var/www/html/wordpress/htdocs/wp-includes/class-wp-hook.php(309): call_user_func_array()\n#10 /var/www/html/wordpress/htdocs/wp-includes/plugin.php(191): WP_Hook->apply_filters()\n#11 /var/www/html/wordpress/htdocs/wp-includes/post-template.php(429): apply_filters()\n#12 /var/www/html/wordpress/htdocs/wp-includes/feed.php(226): get_the_excerpt()\n#13 /var/www/html/wordpress/htdocs/wp-includes/feed-rss2.php(103): the_excerpt_rss()\n#14 /var/www/html/wordpress/htdocs/wp-includes/template.php(770): require_once('...')\n#15 /var/www/html/wordpress/htdocs/wp-includes/functions.php(1662): load_template()\n#16 [internal function]: do_feed_rss2()\n#17 /var/www/html/wordpress/htdocs/wp-includes/class-wp-hook.php(309): call_user_func_array()\n#18 /var/www/html/wordpress/htdocs/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters()\n#19 /var/www/html/wordpress/htdocs/wp-includes/plugin.php(476): WP_Hook->do_action()\n#20 /var/www/html/wordpress/htdocs/wp-includes/functions.php(1624): do_action()\n#21 /var/www/html/wordpress/htdocs/wp-includes/template-loader.php(48): do_feed()\n#22 /var/www/html/wordpress/htdocs/wp-blog-header.php(19): require_once('...')\n#23 /var/www/html/wordpress/htdocs/index.php(17): require('...')\n#24 {main}\n thrown in /var/www/html/wordpress/htdocs/wp-includes/media.php on line 2147, referer: https://tech.michaelaltfield.net/feed/
I recently upgraded my server to use PHP 8.2. After I did, I started getting these fatal PHP errors in wordpress.
I was able to isolate the issue to one post containing a simple [caption] shortcode. I further distilled the shortcode to the following snippet, which throws the fatal error if it's added to any post
[caption]<a href="http://example.com"><img src="https://example.com/example.jpg" /></a> test[/caption]
Through trial-and-error, I found that the fatal error does not appear if I add an id option to the shortcode. For example, this is fine:
[caption id="whyuno" ]<a href="http://example.com"><img src="https://example.com/example.jpg" /></a> test[/caption]
According to the official wordpress documentation on the caption shortcode, the id option is not required (ie the option is, uh, optional).
I'm running the latest stable version of PHP (8.2) and the latest stable version of WordPress (6.3) on Debian 12.
Change History (10)
#2
@
14 months ago
According to the official wordpress documentation on the caption shortcode, the id option is not required (ie the option is, uh, optional).
Looks like I can't edit the OP, so here's a link to the official wordpress docs on [caption]
#3
@
14 months ago
Update: it appears that the error goes away if the width
option is provided.
Per the documentation (linked-above), the width
option *is* required. So when using the [caption]
shortcode properly, this doesn't result in an error.
But, in any case, this is still a bug as misuse of a shotcode shouldn't result in a PHP Fatal error..
This ticket was mentioned in PR #5095 on WordPress/wordpress-develop by @aslamdoctor.
14 months ago
#4
- Keywords has-patch added
Trac ticket: 59206
#5
follow-up:
↓ 10
@
14 months ago
The error appeared whenever there were no arguments passed to the [caption] shortcode. Which ended up $attr to be undefined variable.
So added a condition to check it and initialize it as an empty array if not defined. Patch above.
This ticket was mentioned in Slack in #core by aslamdoctor. View the logs.
14 months ago
#7
@
14 months ago
- Component changed from General to Media
- Focuses php-compatibility removed
- Milestone changed from Awaiting Review to 6.4
@swissspidy commented on PR #5095:
14 months ago
#8
Committed in https://core.trac.wordpress.org/changeset/56488
#9
@
14 months ago
- Resolution set to fixed
- Status changed from new to closed
- Summary changed from PHP Fatal error: Uncaught TypeError: Cannot access offset of type string on string in Stack trace:\n#0 [internal function]: img_caption_shortcode() to PHP warning/error when using `[caption]` shortcode without attributes
This was fixed in r56488
#10
in reply to:
↑ 5
@
14 months ago
Replying to aslamdoctor:
The error appeared whenever there were no arguments passed to the [caption] shortcode. Which ended up $attr to be undefined variable.
It appears that $attr
is still defined, but as an empty string. Tracked this down to shortcode_parse_atts()
returning an empty string instead of an array if the shortcode does not have any attributes, see #59249.
I confirmed this bug on two different themes. One of which was
twentyeleven