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

Save the featured image via REST API if the media post ID doesn't exist in the non-media site #50

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

stuajnht
Copy link

@stuajnht stuajnht commented Mar 4, 2019

Description

When using the Gutenberg editor to add a featured image, if the value of the post ID of the image on the media site doesn't exist in the non-media site that the featured image is being applied to, the featured image is not saved correctly.

This commit hooks into the rest_insert_ action and saves the post ID of the featured image to the post meta table of the non-media site.

Related Issue

#49

…st in the non-media site

When using the Gutenberg editor to add a featured image, if the value of the post ID of the image on the media site doesn't exist in the non-media site that the featured image is being applied to, the featured image is not saved correctly.

This commit hooks into the `rest_insert_` action and saves the post ID of the featured image to the post meta table of the non-media site.
@palmiak
Copy link
Contributor

palmiak commented Jul 11, 2019

@stuajnht I have a small problem with this. It works great when there are no metaboxes (Yoast SEO for example). With metaboxes it looks like first the thumbnail_id is saved but it's overwritten by the the metabox save.

For now I have added a small workaround:

if ( array_key_exists( 'featured_media', $request_json ) ) {
	update_post_meta( $post->ID, '_thumbnail_id', $request_json[ 'featured_media' ] ); //so the preview works correctly
	update_post_meta( $post->ID, '_image_to_save', $request_json[ 'featured_media' ] );
}

and I add:

add_action( 'load-post.php', 'post_listing_page' );
function post_listing_page() {
	if( isset( $_GET['post'] ) && intval($_GET['post']) ) {
		$img = get_post_meta( $_GET['post'], '_image_to_save', true );

		if( $img != '' ) {
			update_post_meta( $_GET['post'], '_thumbnail_id', $img );
		}
	}
}

So I replace the img with the temporary one. Yet I know this is just a workaround. Do you have a better idea how to fix this.

8conor8 added a commit to themedicalrepublic/network-media-library that referenced this pull request Sep 5, 2022
@davidegreenwald
Copy link

👋 Hey all, any movement on this? We are using the code in this PR with Gutenberg but would love to see a merge / release.

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

Successfully merging this pull request may close these issues.

3 participants