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 can be changed but not saved #70

Open
NuernBert opened this issue Dec 8, 2019 · 13 comments
Open

featured image can be changed but not saved #70

NuernBert opened this issue Dec 8, 2019 · 13 comments

Comments

@NuernBert
Copy link

Until the last WP update the plugin on my multisite worked very well.
Unfortunately (after the last WP update 5.3 ? ) I have the problem that the featured image at site id 3 (media lib id = 2) can be changed but not saved anymore. Mmh!?

@vidothedesigner
Copy link

i have the same problem. I don't know if the problem started since the last WP update, but i think it has something to do with the paths... all other places ( i tryed ) i uploaded an image worked except the feature image, when i network disable the plugin, the feature image works perfectly, but when i activate it again the feature image, lets me upload an image, then update the post, and after refresh the feature image is empty. I tryed the plugin on multiple WP multisite instaltions and it is it that is causing the bug.

@Lycurgue
Copy link

Hi, I think Gutenberg is the culprit. I uninstalled it and was able to save Feature Image.

@vidothedesigner
Copy link

just tryed that, it doesnt work for me, but after i set the Upload Url Path for the domain, the images start to save for the posts, but now i get a different image when i print it on the front end

@Lycurgue
Copy link

When I said I uninstalled Gutenberg, in fact I used Disabled Gutenberg plugin to deactivate it on a specific page. I just reactivated Gutenberg on this page after inserting a Featur Image and save it. I hope it can help!

@Lycurgue
Copy link

Beware, Disabled Gutenberg plugin seems to break content format done with Gutenberg. The plugin deactivate Gutenberg on installation instead to let the user to choose. Which is not a good idea.

@vidothedesigner
Copy link

oh so i did test this :) and YES disabling gutenberg in the middle of a project is a very bad idea, so i did a fresh install and after i disabled the gutenberg editor, i could upload feature image, but it might've worked all along. Where i found a problem was when a client asked me to fix his divi theme, and i noticed that in only one case, these two ( plugin and theme ) didnt work quite well, and that was only when you want to put blog posts in a slider and try to put the feature image as a background, only then there is no image or sometimes a wrong image is being displayed.

@frifrafry
Copy link

I found a workaround solution.
The problem is the WP_REST_Posts_Controller that tries to set the featured image via "set_post_thumbnail" - in this is failing, as our "networked" attachement-id is no valid post-id (as it belongs to another site).
So what I did: I used the "rest_insert_"-Action, to set the featured image manually, and then unset the featured_image (so the REST controller can't interfere).

add_action('rest_insert_<post-type>', 'on_insert_item', 10,3);

function on_insert_item($post, $request, $update){
    $featured_media = (int) $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']);
        if ( $result ) {
            return true;
        } else {
            return new WP_Error( 'rest_invalid_featured_media', __( 'Invalid featured media ID.' ), array( 'status' => 400 ) );
        }
    } else {
        return delete_post_thumbnail( $post->ID );
    }
}

Make sure to replace with the actual post-type you're using.

@asgerqo
Copy link

asgerqo commented Feb 11, 2020

@frifrafry awesome that you found a solution. Where do I need to change this?
Is it something I add to functions.php?

@frifrafry
Copy link

frifrafry commented Feb 11, 2020 via email

@serpentes80
Copy link

@frifrafry
thx, that tip saved my ass :-)

@artifex404
Copy link

Looks like this is not working as of the most recent WordPress version if the image is uploaded to another subsite. When saving a post the thumbnails dissappears (or reverts to the previous one). Do your solution still works? @frifrafry

@frifrafry
Copy link

@artifex404
I just checked with Wordpress 5.5.1 and network-media-library 1.5.0 and yes - the solution ist still working!
:-)

@artifex404
Copy link

For me it was not working on a plain WP 5.5.1 MU installation. The featured image dissappears when saving up the post if the image was initially uploaded to another subsite.

The code to fix was to use the class Post_Thumbnail_Saver_REST posted in some fork/pull-request, with the following change:

    /**
     * Sets up the necessary action callback if the post is being saved from a REST request.
     */
    public function __construct() {
        add_action( 'rest_api_init', function () {
            add_action( 'pre_post_update', [ $this, 'action_pre_post_update' ], 10, 2 );
        });
    }

@frifrafry But thanks anyway for checking so quickly 👍

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

7 participants