• I tried mdc comment toolbar on several sites and found all had this error “You don’t have permission to attach files to this post”. when any user other than the post author or administrator added media to the comment.

    My “fix” was to force the media to be unattached by adding “$post->ID = 0;” in the function mdc_comment_toolbar() just after the “global $post”.

    I hope something like that can be added to the plugin.

    https://wordpress.org/plugins/mdc-comment-toolbar/

Viewing 1 replies (of 1 total)
  • Thread Starter gizmomol

    (@gizmomol)

    My “fix” introduced an error when people wanted to edit a post, they always had a post id of 0.

    I changed the function to save the original post id and restore it before exiting this function. Then checks ownership of the post and sets the media as unattached only if the current user does not own the post the comment is on.

    This is the modified function:

    function mdc_comment_toolbar() {
        global $post;
        global $current_user;
    
        // save post ID
        $post_id_orig = $post->ID;
    
        /* a post->ID of '0' means the media is unattached.
        ** if the current user does not own the post, use post->ID = '0'
        ** or if the user can 'edit any post', keep the post->ID
        */
        $current_user = wp_get_current_user();
        if ( !(( current_user_can('edit_others_posts')) || ($current_user->ID == $post->post_author)) ) {
          $post->ID = 0;
        }
    
        ob_start();
    
        wp_editor( '', 'comment', array(
          'textarea_rows' => textarea_rows(),
          'teeny' => is_teeny(),  //hide some icons
          'quicktags' => is_quick_tag(),  //enable html toolbar
          'media_buttons' => is_media_button(),
          'drag_drop_upload' => is_media_button()
          )
        );
    
        $toolbar = ob_get_contents();
    
        ob_end_clean();
    
        // make sure comment media is attached to parent post
        $toolbar = str_replace( 'post_id=0', 'post_id='.get_the_ID(), $toolbar );
    
        // get back post ID
        $post->ID = $post_id_orig;
    
        return $toolbar;
      }
Viewing 1 replies (of 1 total)

The topic ‘Add media permission error’ is closed to new replies.