humanmade / network-media-library

Network Media Library plugin for WordPress Multisite
MIT License
288 stars 55 forks source link

featured image can be changed but not saved #70

Open NuernBert opened 4 years ago

NuernBert commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

vidothedesigner commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 "restinsert"-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 commented 4 years ago

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

frifrafry commented 4 years ago

Yes, just add it to functions.php

serpentes80 commented 4 years ago

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

artifex404 commented 4 years ago

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 commented 4 years ago

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

artifex404 commented 4 years ago

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 👍