shortcode - Wordpress Yoast SEO plugin Post SaveUpdate Issue

admin2025-06-06  2

The Problem

Whenever the Yoast Premium SEO plugin is active, posts are unable to save/update properly in the Admin Menu. After hitting the Update button, I found that all the custom theme shortcodes within the post were being rendered (without the CSS) instead of me being returned to the post editor. With debugger enabled, I was receiving errors like:

Cannot modify header information – headers already sent by (some/file.php)

The Problem

Whenever the Yoast Premium SEO plugin is active, posts are unable to save/update properly in the Admin Menu. After hitting the Update button, I found that all the custom theme shortcodes within the post were being rendered (without the CSS) instead of me being returned to the post editor. With debugger enabled, I was receiving errors like:

Cannot modify header information – headers already sent by (some/file.php)

Share Improve this question edited Oct 5, 2017 at 17:35 jypweb asked Oct 5, 2017 at 9:21 jypwebjypweb 214 bronze badges 1
  • This is a great solution, thank you @jypweb! You might want to change your question to only contain the question, and then move your "after" code to your answer. This should help future users find the answer easier (by looking below) and understand the problem better (by looking at the question) – skplunkerin Commented Oct 5, 2017 at 15:43
Add a comment  | 

1 Answer 1

Reset to default 1

The Solution

After WAY more research than I cared for and ultimately not getting a straight answer, I started to realize that the custom shortcode I was using to create HTML might be to blame. I was creating content by closing the <?php tag and reopening it after the html was finished. Turns out, I should have been using an output buffer like ob_start()/ob_get_clean() and returning the code instead.

Before:

if (argument > 0) { ?> <p>Some text</p> <?php }

After:

if (argument > 0) { ob_start(); ?> <p>Some text</p> <?php return ob_get_clean(); }

This returns the buffered HTML and allows any filters or echos to take place on the shortcode properly. Once this change was made, Yoast (and a couple others like Relevanssi) started working as they were intended to.

Now it's possible that you might get the same debug errors for other reasons, but in this instance, it boiled down to my custom theme not producing shortcodes correctly.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749156495a316845.html

最新回复(0)