WP REST API returns HTML instead of JSON - wordpress

An endpoint of https://example.com/wp-json/wp/v2/pages/123 returns a combination of HTML and JSON format such as:
<div class="my-class">HTML Content</div>
{ "id" : 123, ... }
There is an excerpt filter in functions.php that set the outputted HTML, in this line:
function my_custom_filter(){
echo '<div class="my-class">HTML Content</div>';
}
add_filter ('the_excerpt', 'my_custom_filter' );
How to prevent such filter from intervening with the JSON response?

The real issue here is echoing content during a filter, with the assumption it is being output to the same place echo would. One should only ever modify filtered content. To correct that code:
function my_custom_filter($excerpt) {
return '<div class="my-class">HTML Content</div>' . "\n$excerpt";
}
add_filter ('the_excerpt', 'my_custom_filter');
In this case, OP probably still wants the conditional:
function my_custom_filter($excerpt) {
return !defined('REST_REQUEST') || !REST_REQUEST ?
'<div class="my-class">HTML Content</div>' . "\n$excerpt" :
$excerpt;
}
add_filter ('the_excerpt', 'my_custom_filter');

If you can modify the filter, then you can detect the REST API inside the filter function, like this:
function my_custom_filter(){
if (! defined("REST_REQUEST")) {
echo '<div class="my-class">HTML Content</div>';
}
}
add_filter ('the_excerpt', 'my_custom_filter' );
The constant REST_REQUEST is only defined inside a request that was handled by the REST API. So, with the code above, if a request is being handled by the normal WP request cycle, the constant will not be defined, so the if statement we added will evaluate as true and the echo will occur like normal. If the constant is defined, then the if will evaluate as false and the filter will not add any output to the response.
See https://wpseek.com/constant/rest_request/
UPDATE: while the code above will work, I think we can do better.
The problem is that if you have many filters, every one of them needs to duplicate the if statement and your code become littered with checks for REST_REQUEST, which
is repetitive (not DRY)
not the concern of the filters themselves
untidy
Instead, we can introduce the REST_REQUEST checks earlier in the cycle by making it a concern of the code that adds the filter.
This would look like:
function my_custom_filter(){
echo '<div class="my-class">HTML Content</div>';
}
if (! defined("REST_REQUEST")) {
add_filter ('the_excerpt', 'my_custom_filter' );
//add other HTML filters here
} else {
//attach REST API actions
}
This seems like a small change (if you catch it early, when the number of filters is small), but it potentially brings some big advantages:
it keeps the REST_REQUEST checks all in one place: maintainability
the filters don't need to know or care about whether it's a REST_REQUEST or not - if they get called, they're good to go. Keeping it simple.
you can potentially do other things like logging or role-based access checks in that place: extensibility
It's a relatively easy change to make early on in your application, but becomes more difficult and error prone as you add further filters and actions. Always better to do it right the first time!

Related

Replace Wordpress URL domain in HTML output (for SEO optimisation) [duplicate]

WordPress has great filter support for getting at all sorts of specific bits of content and modifying it before output. Like the_content filter, which lets you access the markup for a post before it's output to the screen.
I'm trying to find a catch-all filter that gives me one last crack at modifying the final markup in its entirety before output.
I've browsed the list of filters a number of times, but nothing jumps out at me:
https://codex.wordpress.org/Plugin_API/Filter_Reference
Anyone know of one?
WordPress doesn't have a "final output" filter, but you can hack together one. The below example resides within a "Must Use" plugin I've created for a project.
Note: I haven't tested with any plugins that might make use of the "shutdown" action.
The plugin works by iterating through all the open buffer levels, closing them and capturing their output. It then fires off the "final_output" filter, echoing the filtered content.
Sadly, WordPress performs almost the exact same process (closing the open buffers), but doesn't actually capture the buffer for filtering (just flushes it), so additional "shutdown" actions won't have access to it. Because of this, the below action is prioritized above WordPress's.
wp-content/mu-plugins/buffer.php
<?php
/**
* Output Buffering
*
* Buffers the entire WP process, capturing the final output for manipulation.
*/
ob_start();
add_action('shutdown', function() {
$final = '';
// We'll need to get the number of ob levels we're in, so that we can iterate over each, collecting
// that buffer's output into the final output.
$levels = ob_get_level();
for ($i = 0; $i < $levels; $i++) {
$final .= ob_get_clean();
}
// Apply any filters to the final output
echo apply_filters('final_output', $final);
}, 0);
An example of hooking into the final_output filter:
<?php
add_filter('final_output', function($output) {
return str_replace('foo', 'bar', $output);
});
Edit:
This code uses anonymous functions, which are only supported in PHP 5.3 or newer. If you're running a website using PHP 5.2 or older, you're doing yourself a disservice. PHP 5.2 was released in 2006, and even though Wordpress (edit: in WP version < 5.2) STILL supports it, you should not use it.
The question is may be old, but I have found a better way to do it:
function callback($buffer) {
// modify buffer here, and then return the updated code
return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { ob_end_flush(); }
add_action('wp_head', 'buffer_start');
add_action('wp_footer', 'buffer_end');
Explanation
This plugin code registers two actions – buffer_start and buffer_end.
buffer_start is executed at the end of the header section of the html. The parameter, the callback function, is called at the end of the output buffering. This occurs at the footer of the page, when the second registered action, buffer_end, executes.
The callback function is where you add your code to change the value of the output (the $buffer variable). Then you simply return the modified code and the page will be displayed.
Notes
Be sure to use unique function names for buffer_start, buffer_end, and callback, so they do not conflict with other functions you may have in plugins.
AFAIK, there is no hook for this, since the themes uses HTML which won't be processed by WordPress.
You could, however, use output buffering to catch the final HTML:
<?php
// example from php.net
function callback($buffer) {
// replace all the apples with oranges
return (str_replace("apples", "oranges", $buffer));
}
ob_start("callback");
?>
<html><body>
<p>It's like comparing apples to oranges.</p>
</body></html>
<?php ob_end_flush(); ?>
/* output:
<html><body>
<p>It's like comparing oranges to oranges.</p>
</body></html>
*/
#jacer, if you use the following hooks, the header.php also gets included.
function callback($buffer) {
$buffer = str_replace('replacing','width',$buffer);
return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { ob_end_flush(); }
add_action('after_setup_theme', 'buffer_start');
add_action('shutdown', 'buffer_end');
I was using the top solution of this post (by kfriend) for a while. It uses an mu-plugin to buffer the whole output.
But this solution breaks the caching of wp-super-cache and no supercache-files are generated when i upload the mu-plugin.
So: If you are using wp-super-cache, you can use the filter of this plugin like this:
add_filter('wp_cache_ob_callback_filter', function($buffer) {
$buffer = str_replace('foo', 'bar', $buffer);
return $buffer;
});
Modified https://stackoverflow.com/users/419673/kfriend answer.
All code will be on functions.php. You can do whatever you want with the html on the "final_output" filter.
On your theme's 'functions.php'
//we use 'init' action to use ob_start()
add_action( 'init', 'process_post' );
function process_post() {
ob_start();
}
add_action('shutdown', function() {
$final = '';
// We'll need to get the number of ob levels we're in, so that we can iterate over each, collecting
// that buffer's output into the final output.
$levels = ob_get_level();
for ($i = 0; $i < $levels; $i++) {
$final .= ob_get_clean();
}
// Apply any filters to the final output
echo apply_filters('final_output', $final);
}, 0);
add_filter('final_output', function($output) {
//this is where changes should be made
return str_replace('foo', 'bar', $output);
});
You might try looking in the wp-includes/formatting.php file. For example, the wpautop function.
If you are looking for doing something with the entire page, look at the Super Cache plugin. That writes the final web page to a file for caching. Seeing how that plug-in works may give you some ideas.
Indeed there was a discusussion recently on the WP-Hackers mailing list about the topic of full page modification and it seems the consensus was that output buffering with ob_start() etc was the only real solution. There was also some discussion about the upsides and downsides of it: http://groups.google.com/group/wp-hackers/browse_thread/thread/e1a6f4b29169209a#
To summarize: It works and is the best solution when necessary (like in the WP-Supercache plugin) but slows down overall speeds because your content isn't allowed to be sent to the browser as its ready, but instead has to wait for the full document to be rendered (for ob_end() ) before it can be processed by you and sent to the browser.
To simplify previous answers, just use this in functions.php:
ob_start();
add_action('shutdown', function () {
$html = ob_get_clean();
// ... modify $html here
echo $html;
}, 0);
I've been testing the answers here now for a while, and since the cache breaking thing is still an issue, I came up with a slightly different solution. In my tests no page cache broke. This solution has been implemented into my WordPress plugin OMGF (which has 50k+ users right now) and no issues with page cache breaking has been reported.
First, we start an output buffer on template redirect:
add_action('template_redirect', 'maybe_buffer_output', 3);
function maybe_buffer_output()
{
/**
* You can run all sorts of checks here, (e.g. if (is_admin()) if you don't want the buffer to start in certain situations.
*/
ob_start('return_buffer');
}
Then, we apply our own filter to the HTML.
function return_buffer($html)
{
if (!$html) {
return $html;
}
return apply_filters('buffer_output', $html);
}
And then we can hook into the output by adding a filter:
add_filter('buffer_output', 'parse_output');
function parse_output($html)
{
// Do what you want. Just don't forget to return the $html.
return $html;
}
Hope it helps anyone.
I have run into problems with this code, as I end up with what seems to be the original source for the page so that some plugins has no effect on the page. I am trying to solve this now - I haven't found much info regarding best practises for collecting the output from WordPress.
Update and solution:
The code from KFRIEND didnt work for me as this captures unprocessed source from WordPress, not the same output that ends up in the browser in fact. My solution is probably not elegant using a globals variable to buffer up the contents - but at least I know get the same collected HTML as is delivered to the browser. Could be that different setups of plugins creates problems but thanks to code example by Jacer Omri above I ended up with this.
This code is in my case located typically in functions.php in theme folder.
$GLOBALS['oldschool_buffer_variable'] = '';
function sc_callback($data){
$GLOBALS['final_html'] .= $data;
return $data;
}
function sc_buffer_start(){
ob_start('sc_callback');
}
function sc_buffer_end(){
// Nothing makes a difference in my setup here, ob_get_flush() ob_end_clean() or whatever
// function I try - nothing happens they all result in empty string. Strange since the
// different functions supposedly have very different behaviours. Im guessing there are
// buffering all over the place from different plugins and such - which makes it so
// unpredictable. But that's why we can do it old school :D
ob_end_flush();
// Your final HTML is here, Yeeha!
$output = $GLOBALS['oldschool_buffer_variable'];
}
add_action('wp_loaded', 'sc_buffer_start');
add_action('shutdown', 'sc_buffer_end');
If you want to modify the output, you can use template_include:
add_filter( 'template_include', static function ( $template ) {
if ( basename( $template ) === 'foo-template.php' ) {
echo str_replace( 'foo', 'bar', file_get_contents( $template ) );
}
return null;
} );
If instead you want to override the output completely, you can use the action template_redirect.
add_action( 'template_redirect', static function () {
wp_head();
echo 'My output.';
wp_footer();
exit;
} );

How to prevent shortcodes from being executed multiple times

It seems shortcodes added to the front page in Wordpress will execute multiple times, possibly this is expected behavior. For example, adding a shortcode called [myshortcode] to the front page, and defining it like this in functions.php:
function myshortcode()
{
ob_start();
// This is what really needs to be run only once
update_my_database();
return ob_get_clean();
}
add_shortcode('myshortcode','myshortcode');
WordPress seems to execute this multiple times. Can that be prevented? Or is there another way to execute a function one time and only one time on the WP front page without using a shortcode?
I've tried multiple checks including in_the_loop() and is_singular() but none seem to work. Also tried adding remove_shortcode('myshortcode'); within the myshortcode function, thinking that might prevent subsequent executions, but that doesn't work either: Tried adding static variables per https://stackoverflow.com/a/3695685/8552218 but doesn't work.
Here's what it looks like with all these approaches, and this still doesn't work. Any ideas?
function myshortcode()
{
ob_start();
if (!is_singular()) return ob_get_clean;
if (!in_the_loop()) return ob_get_clean;
if (!is_main_query()) return ob_get_clean;
static $runonce=false;
if (!$runonce)
{
$runonce=true;
return;
}
// This is what really needs to be run only once
update_my_database();
remove_shortcode('myshortcode');
return ob_get_clean();
}
add_shortcode('myshortcode','myshortcode');
function update_my_database()
{
error_log("Executing...",1,$myemail);
}

Drupal: force regions to always render?

I'd like to always render all regions. Even if they don't contain any blocks.
I render my regions like this (in page.tpl.php):
<?php print render($page['region_name']); ?>
Here's the code I'm using, but this has no effect.
function theme_name_page_alter(&$page) {
$regions = system_region_list($GLOBALS['theme'], REGIONS_ALL);
foreach ($regions as $region => $name) {
if(empty($page[$region])) {
$page[$region] = array();
}
}
}
You can make sure Drupal renders every region with the following code (in a custom module):
function hook_page_alter(&$page) {
foreach($page as $region => $blocks) {
if(is_array($blocks) && in_array($region, array('region_1', 'region_2', 'region_3'))) {
if(count($blocks)==1) {
$page[$region]['phantom_content']['#markup'] = ' ';
}
}
}
}
You will need to replace region_1, region_2, region_3 with the names of your regions that you want to make sure are always rendered.
To explain the code a little, if the count of the blocks array is 1 then it means it is empty as it will always contain the #sorted attribute.
It's not just that you don't have any blocks assigned to that region, but almost certainly because the render array is empty. Keep in mind what the render function does-- it calls drupal_render on any/all elements & children in the render array, which converts them to an html string for output. If there are no renderable elements, it doesn't return any html.
The correct (programmatic) way of rendering these regions would be to define a render array for each region, setting the #markup element to whatever html you want Drupal to output there. This would have to be done in your custom module.
If you need to do this from the gui only, I don't see any way other than defining a phantom block. In which case you should probably re-consider what it is you're trying to accomplish.
I think the answer lies in the region.tpl.php file and associated template_preprocess_region() function.
The template file checks that there is a valid variable called $content available, which is loaded up from the preprocess function. If that $content array is empty, the condition will fail and no markup will be rendered (which will happen if the region contains no blocks).
Try adding a copy of region.tpl.php to your theme, removing the if ($content): condition, and then flushing Drupal's cache.

Wordpress: Individual Styling/Coding for Custom Post Types on Index

I'm really struggling to Google my way out of this issue. I've set-up custom post-types, and I've managed to include those custom types in the index page. But now I really need a way of styling those entries differently to the regular posts. Does anyone know how I can do this? It's not just styling, but also, replacing/adding/removing certain code, as the custom types operate/look differently to regular posts.
Any help would be HUGELY appreciated!
As for the CSS, inside your loop, when you're defining the element that contains your post, try doing something like this
<article class="<?php echo $post->post_type; ?>" ...>
This will give you a class you can hook onto within your CSS file.
You can also use WordPress's built-in post_class function to achieve a similar result
<article <?php post_class($post->post_type); ?>>
That will add the post type to the rest of the default post classes, so you can still style those elements differently.
As for the different code for different post types, one of the answers above me mentioned using an if statement based on the post type:
if ('my_post_type' == $post->post_type)
{
// Code Here
}
I hope this helps you.
Codex Reference for post_class
you can use this code inside the loop:
if ( 'custom_post_type' == get_post_type() ){
//do whatever
}
or if you need to check some post types you can use a switch like:
switch ( get_post_type() ) {
case 'post':
//do whatever;
break;
case 'custom_post_type_1':
//do whatever;
break;
case 'custom_post_type_2':
//do whatever;
break;
}

Why isn't $vars['node'] available in preprocess_page for some content types?

I am currently using drupal 6 for a site I'm working on. I have a MYTHEME_preprocess_page() function that adds a few variables to the page.tpl.php template from the taxonomy and from a cck field. It was working correctly for a bit, and then the $vars['node'] is empty, but only for 2 content types. The 'node' variable is available to the preprocess_page function in other content types.
I thought it was a problem with using the following code, but when I remove all of this, the 'node' variable is still empty.
function mytheme_preprocess_node(&$vars, $hook) {
$function = 'mytheme_preprocess_node'.'_'. $vars['node']->type;
if (function_exists($function)) {
$function(&$vars);
}
}
Does anyone know of any gotchas or bugs that might be removing the 'node' variable? I can't seem to figure out where I'm going wrong. I'm at a loss.
Here is my complete mytheme_preprocess_page() function.
function mytheme_preprocess_page(&$vars, $hook) {
if ($hook == 'node' || $hook == 'page') {
if (is_object($vars['node'])) {
// grab the header image if it exists to make it avaialble to the content header
$vars['header_image'] = _mytheme_get_header_image($vars);
// get the taxonomy term to put in the content header
if (count($vars['node']->taxonomy) > 0) {
$vars['tax_term'] = "<div class=\"terms\">" . _mytheme_get_first_taxonomy_term($vars['node']->taxonomy) . "</div>";
}
// add the teacher's credentials to the content header
if ($vars['node']->field_credentials[0]['view'] != '') {
$vars['teacher_credentials'] = '<span class="teacher-creds">' . $vars['node']->field_credentials[0]['view'] . '</span>';
}
}
}
}
After going through and disabling modules one-by-one, I determined that the problem is related to the module, node_breadcrumb. A similar issue was filed here: http://drupal.org/node/616100#comment-2199374
In the 3rd comment, you'll see a link to another issue with a resolution
For others that run into this, I had the same issue as a result of using the jQuery UI module. Disabling and re-enabling fixed it, and I could not track down the specific issue, but it appeared to be related to $static variables in some path check functions.
To others that stumble their way into here, I suggest you pull some of the more obvious modules right out of the module folder on your dev setup, see if things change, and then put them back in there until you figure it out.
Another option is to search for instances of _preprocess_page(, $variables['node'] and $vars['node'] to see if some contributed code is unwittingly unsetting a node when it shouldn't be.

Resources