Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Featured Image Not working for Subsite #84

Open
eangulus opened this issue Mar 18, 2021 · 17 comments
Open

Featured Image Not working for Subsite #84

eangulus opened this issue Mar 18, 2021 · 17 comments

Comments

@eangulus
Copy link

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.

@basemod
Copy link

basemod commented Apr 25, 2021

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

@basemod
Copy link

basemod commented Apr 26, 2021

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.

@mvdhoek1
Copy link

mvdhoek1 commented May 10, 2021

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

@kuzeya9
Copy link

kuzeya9 commented Jun 8, 2021

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

@mvdhoek1
Copy link

mvdhoek1 commented Jun 8, 2021

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?

@kuzeya9
Copy link

kuzeya9 commented Jun 8, 2021

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

@mvdhoek1
Copy link

mvdhoek1 commented Jun 9, 2021

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

@kuzeya9
Copy link

kuzeya9 commented Jun 9, 2021

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

@mvdhoek1
Copy link

mvdhoek1 commented Jun 9, 2021

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

@kuzeya9
Copy link

kuzeya9 commented Jun 9, 2021

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

functions.zip

@mvdhoek1
Copy link

mvdhoek1 commented Jun 9, 2021

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

@dalguji
Copy link

dalguji commented Aug 3, 2021

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

@basemod
Copy link

basemod commented Aug 3, 2021 via email

@amooreTO
Copy link

This worked for me as well. Thanks @mvdhoek1!

@MarekRzewuski
Copy link

MarekRzewuski commented Dec 31, 2021

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!

@piotr-bajer
Copy link

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

@petertenhoor
Copy link

petertenhoor commented May 3, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants