• Resolved gpspake

    (@gpspake)


    I’m having some trouble with this.
    I want users to be able to click on ‘Upload Media’ and insert images in to the post.

    To get the ‘Add Media’ button to appear on the add-post and edit-post pages, I granted subscribers the ‘upload_files’ capability with the User Role Editor Plugin and I enabled ‘Rich Text(Full)’ in the WPUF settings. When a subscriber logs in and clicks on the “Add Media’ button, the WordPress media upload interface appears. If the subscriber then clicks ‘Select Files,’ browses to media on his or her computer, and clicks ‘Open,’ they receive an error message where the picture thumbnail would be.

    It took me a minute to figure out what was happening but I noticed that the button at the bottom of the add media interface, instead of saying ‘Insert into Post,’ says ‘Insert into Page.’
    I went back to User Role Editor and granted subscribers three more capabilities: edit_pages, edit_others_pages, and edit_published pages. Once these capabilities have been granted, subscribers can upload media and insert it directly in to the post.

    This tells us that, for some reason, the when the add media button is clicked on the WPUF add-post or edit-post pages, the WordPress media uploader is associating the media with the page itself rather than the post. When the Insert into Page button is clicked, thie image is inserted in to the WPUF post description; the user can save the post and they have effectively inserted a picture in to the post.

    There are at least a couple of major issues here.
    Obviously, granting subscribers these capabilities is not an option as it gives them the ability to edit actual pages if they know the page id. Also, because the images are associated with the page itself, they appear below the WPUF form, on the add post and edit post pages as ‘WPUF-Attachments’ and are visible to all users.

    This is proving more difficult to figure out than I expected.
    The solution is to make wp_editor’s Add Media Button recognize that the media is being added to the post description and not the page. However, I’m having a hard time figuring out where it’s being handled. Where is the ‘page id’ being passed to the media uploader, and how can we change it to be the ‘WPUF post ID?’

    Any ideas would be greatly appreciated.

    https://www.ads-software.com/extend/plugins/wp-user-frontend/

Viewing 15 replies - 16 through 30 (of 30 total)
  • First the good news.

    The following code works.

    /**
     * Fix insert media editor button
     *
     * Fixes bug with WordPress 3.5.1
     *
     * @since version 1.1-fork-2RRR-4.3
     */
    function wpuf_insert_media_fix() {
        add_filter( 'media_view_settings', 'wpuf_insert_media_fix_filter', 10, 2 );
    } 
    
    /**
     * Fix insert media editor button filter
     *
     * Fixes bug with WordPress 3.5.1
     *
     * @since version 1.1-fork-2RRR-4.3
     */
    function wpuf_insert_media_fix_filter( $settings, $post ) {
        unset( $settings['post']['id'] );
        unset( $settings['post']['nonce'] );
    	return $settings;
    }

    Insert it into wpuf-functions.php and call it at the beginning of wpuf-add-post:post_form() and wpuf-edit-post.php:edit_form() as follows

    //Fix WordPress 3.5.1 Insert Media Bug
    		wpuf_insert_media_fix();

    Now the not so bad news. I was mistaken about WordPress 3.4.2 updating the attachments to link to the new or edited post (post_parent in the database). The new fix doesn’t change this behavior on either WordPress 3.4.2 or 3.5.1. As a result the attachments will display in the Media Library as unattached which is a bit dangerous if somebody decides to remove the unattached files from the Media Library. Therefore we should fix this bug too.

    To Jonga1306

    Don’t know if the above fix will solve your Featured Image problem. I recall there was another problem there I fixed with my development version. It should be mentioned somewhere in the forums

    Had a look for a solution to the unattached files in the Media Library bug mentioned above.

    Looked firstly at filtering the content on post save. Images do include the attachment id but other attachments did not hence this is not a solution.

    Looking now at the solution mentioned earlier using either the edited post’s id or for new posts using auto-drafts.

    Thread Starter gpspake

    (@gpspake)

    Thanks prof,

    Works great. There’s still some interface I need to do; I don’t want users to be able to see other peoples pictures in the library and I don’t want to let them delete images from posts that have been published (This would probably involve ‘the attachments to link to the new or edited post’.

    This should make it a lot easier though. I’ll let you know if I make any progress.

    Can confirm auto-drafts seems to be the best solution.

    Modify my earlier code given above as follows

    /**
     * Fix insert media bug
     *
     * @since version 1.1-fork-2RRR-4.3
     */
    function wpuf_insert_media_fix( $post_id ) {
    	global $wpuf_post_id;
    	global $post_ID; 
    
    	/* WordPress 3.4.2 fix */
    	$post_ID = $post_id; 
    
    	/* WordPress 3.5.1 fix */
    	$wpuf_post_id = $post_id;
        add_filter( 'media_view_settings', 'wpuf_insert_media_fix_filter', 10, 2 );
    } 
    
    /**
     * Fix insert media editor button filter
     *
     * Fixes bug with WordPress 3.5.1
     *
     * @since version 1.1-fork-2RRR-4.3
     */
    function wpuf_insert_media_fix_filter( $settings, $post ) {
    	global $wpuf_post_id;
    
        $settings['post']['id'] = $wpuf_post_id;
        $settings['post']['nonce'] = wp_create_nonce( 'update-post_' . $wpuf_post_id );
    
    	return $settings;
    }

    Insert it into wpuf-functions.php.

    For wpuf-edit-post.php add the following the beginning of edit_form( $curpost ) as follows:

    //Fix Insert Media Bug
    		wpuf_insert_media_fix( $curpost->ID );

    For wpuf-add-post.php there are a number of things to do.

    First add the following line to the beginning of the file.

    require_once(ABSPATH . '/wp-admin/includes/post.php');

    Then change the beginning of function post_form( $post_type ) as follows:

    function post_form( $post_type ) {
    	global $userdata;
    	$curpost = get_default_post_to_edit( $post_type, true );
    
    	//Fix Insert Media Bug
    	wpuf_insert_media_fix( $curpost->ID );

    Later on in the same function add the following line after the line starting with ‘<input class=”wpuf_submit” type=”submit” name=”wpuf_new_post_submit”‘ ….

    <input type="hidden" name="post_id" value="<?php echo $curpost->ID; ?>">

    Change the beginning of submit_post as follows

    function submit_post() {
            global $userdata;
    	$post_id = trim( $_POST['post_id'] );

    Finally in submit_post() add the following line to the $my_post array:

    'ID' => $post_id,

    Presto!!!

    Hi gpspake,

    Your wish list is a lot to wish for

    I don’t want users to be able to see other peoples pictures in the library and I don’t want to let them delete images from posts that have been published (This would probably involve ‘the attachments to link to the new or edited post’.

    That probably requires a Media Library replacement or some clever use of wp media’s actions and filters.

    There are some Media Library type plugins that already exist that may do what you need. Worth a search.

    Thread Starter gpspake

    (@gpspake)

    Well, I haven’t looked in to it too much but subscribers are already restricted from deleting images that they didn’t upload so I’m guessing there’s got to be some way to filter out those pics.
    My logic is, if I can restrict users from deleting pics uploaded by other users from the library, I should be able to prevent them from seeing those pics in the library.

    The issues you bring up with the Media Library are definitely worth exploring as I have had concerns along the same lines.

    I actually consider the fact that it allows you to delete files that are actually in use to be a bug.

    Ok some more fixes related to the above.

    Fix 1
    ——

    Featured Images uploaded via Frontend are not linked to the post and are listed as unattached in the Media Library.

    The following fix needs to be applied to wpuf-edit-post.php:submit_post().

    Change

    //set post thumbnail if has any
                    if ( $attach_id ) {
                        set_post_thumbnail( $post_id, $attach_id );
                    }

    to

    //set post thumbnail to featured image attach id
    		if ( $attach_id ) {
    			$attachment = get_post( $attach_id );
    
    			// If this attachment is unattached, attach it.
    			if ( $attachment->post_parent == 0 )
    				wp_update_post( array( 'ID' => $attach_id, 'post_parent' => $post_id ) );
    
    			set_post_thumbnail( $post_id, $attach_id );
    		}

    Fix2
    —–

    Given the above fixes and normal use of the backend editor the attachment section will list thumbnails and attachments in the content of the posts.

    The following fix limits that just to attachments that are not the featured image or in the content.

    Note if the attachments are deleted from the content or removed as the thumbnail they may then appear in the attachment section as their parent still remains the original post.

    Change wpuf-functions.php:wpfu_get_attachments to this.

    /**
     * Get the attachments of a post
     *
     * @param int $post_id
     * @return array attachment list
     */
    function wpfu_get_attachments( $post_id ) {
        $att_list = array();
    
        $args = array(
            'post_type' => 'attachment',
            'numberposts' => -1,
            'post_status' => null,
            'post_parent' => $post_id,
            'order' => 'ASC',
            'orderby' => 'menu_order'
        );
    
        $post = get_post( $post_id );
        $content = $post->post_content;
        $attachments = get_posts( $args );
    
        foreach ($attachments as $attachment) {
            $id = $attachment->ID;
            $url = wp_get_attachment_url( $attachment->ID );
    
            //exclude thumbnails
            if ( $id == get_post_thumbnail_id( $post_id ) )
                continue;
    
            //exclude attachments in content
            if ( strpos( $content, $url ) !== false )
                continue;
    
            $att_list[] = array(
                'id' => $id,
                'title' => $attachment->post_title,
                'url' => $url,
                'mime' => $attachment->post_mime_type
            );
        }
    
        return $att_list;
    }

    For those of you that find all these changes to much to bear I will be releasing a new development version in a day or two with all these changes

    Prof – re fix one and two above, I can’t find any reference to set_post_thumbnail in wpuf-edit-post.php

    All other code changes above that implemented but confused as to why I don’t have that function reference?

    Hi jonga1306.

    Forgot to mention that all the changes above were for the official FrontEnd version but will work for both (except for this one).

    For Fix 1 for the development version change featured_image.php:attach_file_to_post() as follows

    /**
    	* Attach a featured image to a post
    	*
    	* @since 1.1-fork-2RRR-3.0
    	*/
        static function attach_file_to_post( $post_id ) {
    		//get featured image attach id
    		$attach_id = isset( $_POST['wpuf_featured_img'] ) ? intval( $_POST['wpuf_featured_img'] ) : 0;
    
    		//set post thumbnail to featured image attach id
    		if ( $attach_id ) {
    			$attachment = get_post( $attach_id );
    
    			// If this attachment is unattached, attach it.
    			if ( $attachment->post_parent == 0 )
    				wp_update_post( array( 'ID' => $attach_id, 'post_parent' => $post_id ) );
    
    			set_post_thumbnail( $post_id, $attach_id );
    		}
        }

    Fix2 is the same for both official and development versions

    Thread Starter gpspake

    (@gpspake)

    Cool man! I’m zoned in to another project today but I can’t wait to check this out. Thanks.

    Just landed the new development version 4.3 which includes these fixes

    Hi there,
    I’m using official plugin version (1.1) and I’ve been applying the above mentioned fixes. I have created a patch to easily apply these changes to the plugin, you can download it here:

    https://db.tt/o48MDTpZ

    In Linux, copy the downloaded file to your website root folder, e.g.
    /var/www/mywebsite

    Change directory:
    cd /var/www/mywebsite

    And run:
    patch -p1 < wp-user-frontend_fix-media-insert-bug.patch

    If all goes well you should see:
    patching file wp-content/plugins/wp-user-frontend/wpuf-add-post.php
    patching file wp-content/plugins/wp-user-frontend/wpuf-edit-post.php
    patching file wp-content/plugins/wp-user-frontend/wpuf-functions.php

    Hope it helps

Viewing 15 replies - 16 through 30 (of 30 total)
  • The topic ‘Inserting Media Directly into Post with The Add Media Button’ is closed to new replies.