humanmade/network-media-library

Featured Image Not working for Subsite

Opened this issue · 17 comments

Running WordPress 5.7

On a subsite, clicking on featured, and uploading an image, then save the post, the featured image goes back to empty.
I also can't select a previously uploaded image.
Not sure what else to say, other than I emulated this on my live and my dev servers.

If someone wants more information, ask away.

Can confirm. I have this problem with blog posts and any custom post types so far.

So apparently, saving a post should add an entry to the table wp_{subsitenumber}_postmeta which states that my post should have a _thumbnail_id of the image I chose, but this doesn't happen.

I've solved this with the following code:

// add_action('rest_insert_{post_type}', 'onInsertItem', 10, 3);
add_action('rest_insert_page', 'onInsertItem', 10, 3);

/**
 * On subsites the featured_media must be set manually
 *
 * @param \WP_Post $post
 * @param \WP_REST_Request $request
 * @param bool $update
 * 
 * @return void
 */
function onInsertItem(\WP_Post $post, \WP_REST_Request $request, bool $update)
{
    $featured_media = $request['featured_media'];
    if ($featured_media) {
        //this is the original from rest controller that does not work
        $result = set_post_thumbnail($post->ID, $featured_media);
        //so we set the _thumbnail_id manually
        $result = update_post_meta($post->ID, '_thumbnail_id', $featured_media);
        //now we unset the featured_image (so the REST controller can't interfere)
        unset($request['featured_media']);
    }
}

I've solved this with the following code:

// add_action('rest_insert_{post_type}', 'onInsertItem', 10, 3);
add_action('rest_insert_page', 'onInsertItem', 10, 3);

/**
 * On subsites the featured_media must be set manually
 *
 * @param \WP_Post $post
 * @param \WP_REST_Request $request
 * @param bool $update
 * 
 * @return void
 */
function onInsertItem(\WP_Post $post, \WP_REST_Request $request, bool $update)
{
    $featured_media = $request['featured_media'];
    if ($featured_media) {
        //this is the original from rest controller that does not work
        $result = set_post_thumbnail($post->ID, $featured_media);
        //so we set the _thumbnail_id manually
        $result = update_post_meta($post->ID, '_thumbnail_id', $featured_media);
        //now we unset the featured_image (so the REST controller can't interfere)
        unset($request['featured_media']);
    }
}

I've write this code to my plugin file. but featured image still won't show up..

Make sure the fill in the correct post-type in add_action('rest_insert_{post_type}', 'onInsertItem', 10, 3);.
If so on which location did you placed this code?

i placed that code at plugin editor network-media-library.php

Try placing this code in your functions.php inside your theme folder.

Featured image still won't show up. i have searching the problem with error_log, and result is featured image can't load because link image didn't load central gallery but localhost gallery.
example my featured image link is:
https://clone.website.com/wp-content/uploads/sites/10/2021/06/IMG_20210607_110441_635-592x444.jpg
and real image i can load is:
https://clone.website.com/wp-content/uploads/2021/06/IMG_20210607_110441_635-592x444.jpg

i try to fix this but its really hard for me

Can you show the code that you currently have and for which post-type do you want to add this?

this is my functions.php file and i have writed your code.
post-type is property

functions.zip

Alright, can you first check if setting a featured image for a page is working correctly?
Secondly; add_action('rest_insert_{property}', 'onInsertItem', 10, 3); should be add_action('rest_insert_property', 'onInsertItem', 10, 3);

@mvdhoek1's code works for me. This saved me a ton of time. Thank you, @mvdhoek1.

For those who are trying to save a featured image of posts, replace add_action('rest_insert_page', 'onInsertItem', 10, 3); with add_action('rest_insert_post', 'onInsertItem', 10, 3);.

This worked for me as well. Thanks @mvdhoek1!

The add_action() trick seems to be work for post and page. Avada template has custom post type called Portfolio (avada_portfolio). It's not working using rest_insert_avada_portfolio.
Any ideas on how to make it work with avada_portfolio?

Another twist to the same problem:
I have created custom type with Pods. Adding hook rest_insert_{my type} also dosn't work.
Any hint on what I can do to make it work? Thanks!

This works for me:

/**
 * Filter REST API responses for post saves which include featured_media
 * field and force the post meta update even if the id doesn't exist on the
 * current site.
 *
 * @param \WP_HTTP_Response|\WP_Error $response
 * @param array                       $handler
 * @param \WP_REST_Request            $request
 *
 * @return \WP_HTTP_Response|\WP_Error
 *
 * @wp-hook rest_request_after_callbacks
 */
function rest_request_after_callbacks( $response, array $handler, \WP_REST_Request $request ) {
	if ( is_media_site() ) {
		return $response;
	}

	$featured_image = (int) $request['featured_media'] ?? null;

	if ( $featured_image ) {
		switch_to_media_site();
		$attachment = get_post( $featured_image );
		restore_current_blog();

		$post_id = (int) $request['id'] ?? null;

		if ( $attachment ) {
			update_post_meta( $post_id, '_thumbnail_id', $featured_image );
		} else {
			delete_post_meta( $post_id, '_thumbnail_id' );
		}

		$data                   = $response->get_data();
		$data['featured_media'] = $featured_image;
		$response->set_data( $data );
	}

	return $response;
}

add_filter( 'rest_request_after_callbacks', __NAMESPACE__ . '\\rest_request_after_callbacks', 10, 3 );

Thanks @piotr-bajer and @mvdhoek1. Your code helped me fix the network media library when updating featured images within subsites with the Gutenberg editor 💯