WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Media Uploader on Comment Box needs the Right to edit_others_posts (5 posts)

  1. Frank
    Member
    Posted 1 year ago #

    I have a scenario where only the author role is allowed to comment on other posts. So, in a custom plugin, I've integrated the full WP editor with the media upload button for comment boxes like this:

    <?php
    function twentytwelve_wp_editor(){
        ob_start();
        wp_editor( '', 'comment' );
        return ob_get_clean();
    }
    function twentytwelve_comment_form_defaults($args) {
        $args['comment_field'] = twentytwelve_wp_editor();
         return $args;
    }
    add_filter('comment_form_defaults', 'twentytwelve_comment_form_defaults');
    ?>

    And why not this way? Authors comment a lot and have the same functionality as when they create or edit their posts and they are familiar with the WP editor and media uploader.

    But this is not working using the file upload button of the comment box: an error occured instantly after a file upload, saying in a litte red box: "An error occurred in the upload. Please try again later." Please note, it does not complain about user rights.
    If I otherwise use the media uploader via "+Add" => "file" from the toolbar - it is working! Because I wanted the real workflow and let authors upload and insert images from the comment box, so I've tried around. Then I found that, if I activate the edit_others_posts capability for the author role, it magically works! If this cap is not set, it does not. Hmm, this is strange on the other hand and I do really not want to activate this cap for authors, of course...
    So the whole thing works like a charm for admins and editors, while authors have to get the right to change other posts.

    Is anyone able to help me and knows how to fix this?
    Thanks in advance!

  2. Frank
    Member
    Posted 1 year ago #

    What I figured out so far: the upload will work if the actual post is written by the same authenticated user who is about to comment :-)

    This is because the media uploader assigns the uploaded file to the actual post - what makes sense in a post creation or update context. But using the editor with file uploads in the comment context it seems the check for the edit_others_posts capability makes absolutely no sense, because it's the normal, expected case that you're NOT the only one commenting on your own posts.

    What hackish thing is to do? To simulate somehow the commenting user is the post author? Looks not good. - To skip the assignment to a post at all as we do using the media library? Much better?! But how can I inject this into the comment editor instance?

  3. Frank
    Member
    Posted 1 year ago #

    This is the solution for me. Setting the $post->ID to zero means that attachments have no parent. See the comments.

    function twentytwelve_wp_editor(){
        global $post;
        ob_start();
        // For later restore via Javascript:
        print '<input type="hidden" id="real_post_id" name="real_post_id" value="' . $post->ID . '">';
        $post->ID = 0; // Since this will be the parent of attached media!
        wp_editor( '', 'comment' );
        return ob_get_clean();
    }
    function twentytwelve_comment_form_defaults($args) {
        if ( current_user_can( 'publish_posts' ) ) {
            $args['comment_field'] = twentytwelve_wp_editor();
        }
        return $args;
    }
    add_filter( 'comment_form_defaults', 'twentytwelve_comment_form_defaults' );
    // More allowed tags for authors:
    function authors_allowed_tags() {
        if ( ! current_user_can( 'publish_posts' ) ) return;
        global $allowedtags;
        $allowedtags['ul'] = array();
        $allowedtags['ol'] = array();
        $allowedtags['li'] = array();
        $allowedtags['img'] = array(
            'width' => true,
            'height' => true,
            'src' => true,
            'alt' => true
        );
        $allowedtags['span'] = array(
            'style' => true
        );
        $allowedtags['p'] = array(
            'style' => true
        );
    }
    add_action( 'init', 'authors_allowed_tags' );
    function restore_post_ID() {
      // Restore the real post ID so the comment can be attached to it: ?>
      <script type="text/javascript">jQuery('#comment_post_ID').val(jQuery('#real_post_id').val());</script> <?php
    }
    add_action( 'wp_footer', 'restore_post_ID' );
  4. Marko-M
    Member
    Posted 1 year ago #

    Hi,
    this looks like a bug in the new media editor. Your solution is more of a workaround, so WP guys should fix it in the core.

    http://codex.wordpress.org/Reporting_Bugs

  5. Frank
    Member
    Posted 1 year ago #

    I'm not sure. The media uploader seems at this point not really to be made for moving it around easily. The use with comments is probably not a common case.

Topic Closed

This topic has been closed to new replies.

About this Topic