WordPress.org

Make WordPress Core

Opened 23 months ago

Closed 23 months ago

Last modified 23 months ago

#46063 closed defect (bug) (invalid)

echo in functions called by add_shortcode()

Reported by: kaikli Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.0.3
Component: REST API Keywords:
Focuses: javascript, rest-api Cc:

Description

discription: echos in functions called by add_shortcode, will be added in front of the json returned by the rest-api

error: this produces a json parsing error by the reciever


example:

  • site: wp-admin/post-new.php?post_type=page
  • request: /wp-json/wp/v2/pages?per_page=100&exclude=11&parent_exclude=11&orderby=menu_order&order=asc&context=edit&_locale=user
  • answer: 'echoed-data-from-function-called-by-add_shortcode{"json-returned": "from-wordpress"}'

Change History (2)

#1 follow-up: @johnbillion
23 months ago

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

Thanks for the report @kaikli . Shortcode callback functions shouldn't echo their output, only return it. If your shortcode handler returns its output, then the REST API will work as expected.

More info here: https://codex.wordpress.org/Shortcode_API

#2 in reply to: ↑ 1 @kaikli
23 months ago

Replying to johnbillion:

Thanks for the report @kaikli . Shortcode callback functions shouldn't echo their output, only return it. If your shortcode handler returns its output, then the REST API will work as expected.

More info here: https://codex.wordpress.org/Shortcode_API

Thanks for your answer @johnbillion

I know that shortcode callback functions shouldn't echo their output.

In my case, it was a function somwhere included in the callback function and it was not easy to find this issue.


Is it than possible to add a error message, simply saying "Hey, you should not echo something in your callback!" or "Hey, a callback function echos some data!" or (sry, when this is wrong and wouldn't fix this, but I hope you will understand what i want to say) include a ob_get_clean() in the REST-API.

Because this problem makes the field for parent sites, in wp-admin/post-new.php?post_type=page, silently disapper, without any notification.
From this behavior I was really confused.


Only producing a error, even with no detailed information, would make it plugin developers easier to recognize this problem.

Last edited 23 months ago by kaikli (previous) (diff)
Note: See TracTickets for help on using tickets.