Remove DFI from REST API?
-
How to ensure that the DFI does not appear in the REST API? I only want it to appear on the website.
Thanks.
-
Hi Chris,
Currently I’m on a vacation. So I cannot provide a in dept answer now. It’s probably possible with custom code. There are several code snippets in the FAQ.
The hard part is that the wp-admin also uses the rest API when editing a post.
Please let me know if you need me to create the custom snippet. I could create that somewhere next week.Hi Jan,
A snippet to disable DFI for REST API would be great, if you don’t mind. My attempts with AI have failed. π
Thank you.
A few extra settings would go a long way if you are considering extra features, such as checkboxes to..
Disable DFI for posts
Disable DFI for pages
Disable DFI for {registered custom post type 1}, etc
Disable DFI for REST APIGreat plugin all the same!
Hi Chris,
I’ll get back with the snippet on Wednesday.
A option per post-type/category ect had been noted before.
I’ve been thinking about it, might happen next year.Hi Chris,
Below is the code that disables DFI in the rest API
In your
wp-content/plugins/folder create a file named:dfi-exclude-rest.php
With the following code:<?php
/**
* Plugin Name: Default Featured Image - Random post image
* Plugin URI: https://wordpress.org/support/topic/multiple-featured-images-6/
* Version: 1.0
*/
add_filter( 'dfi_thumbnail_id', 'dfi_random_image', 10, 2 );
function dfi_random_image( $dfi_id, $post_id ) {
if ( get_post_type( $post_id ) !== 'post' ) {
return $dfi_id; // This is not a post, we only check posts.
}
$random_image_ids = array(
123, # Add your image id's in this array.
456,
);
return $random_image_ids[ array_rand( $random_image_ids ) ];
}After that, go to the WP-Admin > Plugins and activate the new plugin.
That’s it.Let me know how it goes!
Hi Jan,
Are you sure you pasted the correct function? That appears to replace the dfi with a random image and I see no reference to the REST API.
Thanks,Chris
Sorry this is the correct code:
<?php
/**
* Plugin Name: Default Featured Image - Exclude rest API
* Plugin URI: https://wordpress.org/support/topic/remove-dfi-from-rest-api/
* Version: 1.0
*/
add_filter( 'dfi_thumbnail_id', 'dfi_disable_in_rest', 10, 2 );
function dfi_disable_in_rest( $dfi_id, $post_id ) {
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return 0; // Empty image ID for rest requests.
}
return $dfi_id; // the original featured image id.
}That worked a treat. Thank you very much!
You must be logged in to reply to this topic.