• Creator
    Topic
  • #32286
    vacdk
    Participant

    Hi
    I use the plugin happyfiles, and have created some categories like Avatar
    Then I have created some custom fields for users included an image field for an avatar images.

    So my problem is, when uploading the image through the frontend submission, the images goes to the default media library and is not assigned to any category by happyfiles "happyfiles_category" aka Avatar

    I have contacted Thomas from happyfiles and his note was:

    A HappyFiles folder is a custom taxonomy term in your database.
    You could hook into the upload file event and assign any folder (= term_id) you like to it.
    The HappyFiles taxonomy slug for files is: happyfiles_category

    So my question is, can this be done by the metabox custom field, ex may be in the advanced / Custom settings.

    And if yes how ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #32306
    Long Nguyen
    Moderator

    Hi,

    You can create a taxonomy or taxonomy_advanced in a field group (meta box) and assign the meta box to the post type Attachment. Then when editing an image uploaded, you can select and assign the term of the taxonomy happyfiles_category to that image. Screenshot https://share.getcloudapp.com/d5u65Nm8

    Please read more on the documentation
    https://docs.metabox.io/fields/taxonomy/

    #32312
    vacdk
    Participant

    Thanks for the reply

    The Avatar image is already hooked to the user profile, so it's not possible also to add it to the attachment, I know ACF can hook to multiple locations posts, pages, users but I don't see the option in MB.

    As written in the earlier post, the Happyfiles plugin has already created the taxonomy.
    I just want to set a taxonomy argument on the image upload. Hidden, as users shall not be able to select different options.

    I tried with JSON notation, but no luck.

    CUSTOM SETTINGS
    ex. tax_query = [{"happyfiles_category": "avatar", "term_id": "20", "slug": "avatar"}]

    #32325
    Long Nguyen
    Moderator

    Hi,

    The custom settings tax_query of a field is to filter the value and show it on the select field for the user to choose. It will not work in your case.

    I think you can use the Meta Box action hook rwmb_{$field_id}_after_save_field then set the term for the image ID (post type attachment). Refer to these documentations
    https://docs.metabox.io/actions/#rwmb_after_save_field
    https://developer.wordpress.org/reference/functions/wp_set_post_terms/

    #32357
    vacdk
    Participant

    Thanks Long

    But the lack of documentation or examples I can not get it to work.

    In this example, I am just trying to update a text field after saving.
    Text field ID = text_plo7fflsjjp

    add_action( 'rwmb_text_plo7fflsjjp_after_save_field', function ( $null, $field, $new, $old, $object_id ) {
        $settext = 'updated';
        
        update( $field["id"], $settext , $new );
    
    }, 10, 5 );

    but i am getting a Fatal error: Uncaught Error: Call to undefined function update()

    #32364
    vacdk
    Participant

    So, after some digging, I think I got it to work.

    add_action( 'rwmb_galleri_upload_after_save_field', function ( $null, $field, $new, $old, $object_id ) {
     $args = array(
        'post_type' => 'attachment',
        'numberposts' => null,
        'post_status' => null,
        'post_parent' => $object_id
    ); 
    $attachments = get_posts($args);
    if ($attachments) {
        foreach ($attachments as $attachment) {
          wp_set_object_terms( $attachment->ID, 'Galleri', 'happyfiles_category', false );
        }
    }   
    }, 10, 5 );

    is this the right way to do it ? or is there an easier way ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.