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

featured image can be changed but not saved

Open NuernBert opened this issue 5 years ago • 13 comments

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!?

NuernBert avatar Dec 08 '19 22:12 NuernBert

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.

vidothedesigner avatar Dec 12 '19 17:12 vidothedesigner

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

Lycurgue avatar Dec 12 '19 20:12 Lycurgue

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

vidothedesigner avatar Dec 12 '19 20:12 vidothedesigner

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 avatar Dec 12 '19 21:12 Lycurgue

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.

Lycurgue avatar Dec 13 '19 00:12 Lycurgue

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.

vidothedesigner avatar Dec 16 '19 19:12 vidothedesigner

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.

frifrafry avatar Feb 11 '20 07:02 frifrafry

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

asgerqo avatar Feb 11 '20 18:02 asgerqo

Yes, just add it to functions.php

frifrafry avatar Feb 11 '20 19:02 frifrafry

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

serpentes80 avatar Jun 06 '20 18:06 serpentes80

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

artifex404 avatar Oct 07 '20 12:10 artifex404

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

frifrafry avatar Oct 08 '20 08:10 frifrafry

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 👍

artifex404 avatar Oct 08 '20 08:10 artifex404