• Resolved rbarron

    (@rbarron)


    Hi David,

    Trying to create a filter that adds the attribute category automatically for users other than admin uploading media. Admit this is WAY beyond my skill level. Here’s what I have so far, are you able to help with the reference to the attribute category?

    Thank you ….. Rick

    function cms_add_artwork_img_att( $atts, $attachment ) {
    	$capability = 'create_users';
    	current_user_can( $capability );
            $att-value = 'artwork';
        if ( !current_user_can( $capability ) ) {
            $atts['XXXX'] = $artwork;
            }
        }
        return $atts;
    }
    add_filter( 'wp_get_attachment_image_attributes', 'cms_add_artwork_img_att', 10, 2 );

    https://ww.wp.xz.cn/plugins/media-library-assistant/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author David Lingren

    (@dglingren)

    Thanks for your question. Here is my best guess at a filter that will accomplish your goal:

    function cms_add_artwork_img_att( $data, $post_id ) {
        // Determine if user is a network (super) admin.
        // Will also check if user is admin if network mode is disabled.
        if ( is_super_admin() ) {
            return $data;
        }
    
        // see https://codex.ww.wp.xz.cn/Function_Reference/wp_set_post_terms
        // for details on adding hierarhical terms like categories.
        $term = get_term_by( 'slug', 'artwork', 'category' );
        $term_id = array( intval( $term->term_id ) );
        $taxonomy_obj = get_taxonomy( 'category' );
        if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
            wp_set_post_terms( $post_id, $term_id, 'category', true );
        }
    
        return $data;
    }
    add_filter( 'wp_update_attachment_metadata', 'cms_add_artwork_img_att', 10, 2 );

    I haven’t actually tested this code but it is adapted from code I have used in MLA for similar purposes. You should read through the Codex documentation for the functions I’ve used to make sure they do what you want.

    I am marking this topic resolved, but please update it if you have problems or further questions regarding the above suggestions. Thanks for your interest in MLA.

    Thread Starter rbarron

    (@rbarron)

    Thank you David you are AWESOME!!

    I’ll give it a test-drive.

    ….. Rick

    Thread Starter rbarron

    (@rbarron)

    David,

    The check box for the att. category “artwork” is not checked when uploading a media file through Buddypress. I confirmed both the WP and the Buddypress Roles have the edit_post capability.

    Appreciate your help. Think this is a Buddypress issue?

    ….. Rick

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your update and the BuddyPress report. I did some testing and it looks like BuddyPress requires rtMedia to support media file uploads; are you using rtMedia?

    Can you give me the steps you are following to upload media files in BuddyPress?

    Any additional details you can provide would be most helpful.

    Thread Starter rbarron

    (@rbarron)

    David,

    Thank you for the reply. We’re using GD bbPress Attachments to upload images to forum posts.

    Here’s the forum where members can upload images: http://www.colormystress.com/forums/forum/my-artwork/

    You’ll need a login to test it, here’s a video of my doing so – http://screencast.com/t/15ttA1eVeO

    Thank you for your willingness to help …… Rick

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your update with the additional information. I modified my test system to use bbPress and GD bbPress Attachments. I added the cms_add_artwork_img_att code above and ran some tests. The code is working for me when attachments are added with the GD bbPress Attachments “front end” uploader.

    If you have access to your site’s error log you can add some debugging statements, e.g.:

    function cms_add_artwork_img_att( $data, $post_id ) {
    error_log( __LINE__ . ' DEBUG: cms_add_artwork_img_att $data = ' . var_export( $data, true ), 0 );
    error_log( __LINE__ . ' DEBUG: cms_add_artwork_img_att $post_id = ' . var_export( $post_id, true ), 0 );
        // Determine if user is a network (super) admin.
        // Will also check if user is admin if network mode is disabled.
        if ( is_super_admin() ) {
            return $data;
        }
    
        // see https://codex.ww.wp.xz.cn/Function_Reference/wp_set_post_terms
        // for details on adding hierarhical terms like categories.
        $term = get_term_by( 'slug', 'artwork', 'category' );
    error_log( __LINE__ . ' DEBUG: cms_add_artwork_img_att $term = ' . var_export( $term, true ), 0 );
        $term_id = array( intval( $term->term_id ) );
        $taxonomy_obj = get_taxonomy( 'category' );
    error_log( __LINE__ . ' DEBUG: cms_add_artwork_img_att $taxonomy_obj = ' . var_export( $taxonomy_obj, true ), 0 );
    error_log( __LINE__ . ' DEBUG: cms_add_artwork_img_att current_user_can = ' . var_export( current_user_can( $taxonomy_obj->cap->assign_terms ), true ), 0 );
        if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
            $result = wp_set_post_terms( $post_id, $term_id, 'category', true );
    error_log( __LINE__ . ' DEBUG: cms_add_artwork_img_att $result = ' . var_export( $result, true ), 0 );
        }
    
        return $data;
    }

    Here are some things to look for:

    • Make sure your filter is being called and writes the log entries
    • Make sure the taxonomy is supported for object_type attachment
    • Make sure the term exists for slug artwork
    • Make sure the user has cap 'assign_terms' => 'edit_posts'
    • Make sure the wp_set_post_terms result is an array of the affected terms, not false or a WP_Error object

    Let me know if the logging works for you and gives you any insight. If you’re still having problems, post the log messages to give me a better idea of what is happening. If you don’t have access to the error log, go to the “MLA Debug Tab” section of the Settings/Media Library Assistant Documentation tab to set up a convenient way to use the log.

    Thread Starter rbarron

    (@rbarron)

    Thanx again David, SO helpful.

    Unfortunately I’m unable to see any messages in the error log. I’ve asked a technical resource on our team for assistance. Will update you when I have a resolution.

    ….. Rick

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Att. Category Database Value’ is closed to new replies.