network-media-library icon indicating copy to clipboard operation
network-media-library copied to clipboard

Featured Image Not working for Subsite

Open eangulus opened this issue 3 years ago • 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.

eangulus avatar Mar 18 '21 09:03 eangulus

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

basemod avatar Apr 25 '21 16:04 basemod

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.

basemod avatar Apr 26 '21 12:04 basemod

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']);
    }
}

mvdhoek1 avatar May 10 '21 08:05 mvdhoek1

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..

kuzeya9 avatar Jun 08 '21 09:06 kuzeya9

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?

mvdhoek1 avatar Jun 08 '21 11:06 mvdhoek1

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

kuzeya9 avatar Jun 08 '21 15:06 kuzeya9

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

mvdhoek1 avatar Jun 09 '21 08:06 mvdhoek1

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

kuzeya9 avatar Jun 09 '21 09:06 kuzeya9

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

mvdhoek1 avatar Jun 09 '21 10:06 mvdhoek1

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

functions.zip

kuzeya9 avatar Jun 09 '21 10:06 kuzeya9

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 avatar Jun 09 '21 16:06 mvdhoek1

@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);.

dalguji avatar Aug 03 '21 11:08 dalguji

Guten Tag,

vielen Dank für Ihre Nachricht. Dies ist eine automatisch generierte Antwort. Ich bin bis inkl. 22.08.2021 im Urlaub und ab 23.08.2021 wieder in München zu erreichen.

Mit freundlichen Grüßen Michael Krefft

--

BASEMOD Consulting Michael Krefft

Infanteriestr. 19 Haus 6 80797 München +49 89 32211207 https://www.basemod.de

basemod avatar Aug 03 '21 11:08 basemod

This worked for me as well. Thanks @mvdhoek1!

amooreTO avatar Dec 16 '21 14:12 amooreTO

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!

MarekRzewuski avatar Dec 31 '21 01:12 MarekRzewuski

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 );

piotr-bajer avatar Apr 28 '22 10:04 piotr-bajer

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

petertenhoor avatar May 03 '23 14:05 petertenhoor