Make WordPress Core

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#28030 closed defect (bug) (invalid)

cast $content as a string when calling do_shortcode()

Reported by: webstract's profile webstract Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.9
Component: Shortcodes Keywords:
Focuses: Cc:

Description

There is a warning message in 3.9...

Warning: strpos() expects parameter 1 to be string, array given in /public_html/wp-includes/shortcodes.php on line 193

To fix this we should cast $content as a string in /wp-includes/shortcodes.php on line 193

Change:
if ( false === strpos( $content, '[' ) ) {

To:
if ( false === strpos( (string) $content, '[' ) ) {

Change History (8)

#1 @elliott-stocks
11 years ago

Should content ever be an array?

#2 @channeleaton
11 years ago

What data are you passing as the content?

#3 @webstract
11 years ago

There are numerous people getting this errors from themes and plugins...
http://wordpress.org/support/topic/wp-39-warning-strpos-expects-parameter-1-to-be-string?replies=17

In my case it was this function. $atts is an array but gets passed to make_more_shortcodes($atts) which runs do_shortcode() - this was created for nested shortcodes.

add_shortcode('action', 'sc_action');
function sc_action($atts, $content=null) {
	$atts = make_more_shortcodes($atts);
	extract(shortcode_atts(array(
		'name' => 'scroll_form',
		'text' => 'click here to schedule'
	), $atts));
	
	if($name == 'scroll_form') $anchor = '#bigOffer';
	
	return '<a href="'.$anchor.'">'.$text.'</a>';
}

#4 follow-up: @ocean90
11 years ago

You can't cast an array to a string. It should give you a array to string conversion notice. $content should be always a string.

What is the code of make_more_shortcodes()?

#5 @ocean90
11 years ago

The line was introduced in [27394] for #23855.

#6 in reply to: ↑ 4 @webstract
11 years ago

Replying to ocean90:

You can't cast an array to a string. It should give you a array to string conversion notice. $content should be always a string.

What is the code of make_more_shortcodes()?

This is the make_more_shortcodes() function...

function make_more_shortcodes($atts)
{
	return do_shortcode(preg_replace('/~([^{|}]*)~/', "[$1]", $atts));
}

#7 @TobiasBg
11 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

do_shortcode() is clearly documented and only accepts strings. That make_more_shortcodes() is doing it wrong and should cast that array to a string itself (probably in a loop).

#8 @nacin
11 years ago

We don't like to suppress errors like this as it is clear that a plugin or theme is at fault. Hiding it means that a developer will not realize they made an error.

Note: See TracTickets for help on using tickets.