0

I have a form that visitors use to upload files and fill out information about them and a function that adds those files to the media library and set various fields (including some custom taxonomies) on the media library items based on fields that the visitor filled out in the form.

The problem is, I have an email address field. When a visitor fills out the email address field with "[email protected]", what is displayed on the Attachment details screen (as in siteurl.com/wp-admin/upload.php?item=xx ) for that email field is "nameemail-com".

screenshot of attachment details screen, showing "nameemail-com" as the value for the email field

I want the Attachment details screen to show the actual entered value (a valid email address) as in "[email protected]" instead. How do I achieve this?

Here's the function (in functions.php) where I add the uploaded files to the media library and set the fields based on form fields.

function form_to_media_library($entry){


// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();

// build array of uploads
$all_files_string = trim ($entry[ '1' ], '[]');
$all_files = explode(",", $all_files_string);

// start loop to process each uploaded file

foreach ($all_files as $this_file) {

    // set filename
    $upload_path = GFFormsModel::get_upload_path( $entry[ 'form_id' ] );
  $upload_url = GFFormsModel::get_upload_url( $entry[ 'form_id' ] );
  $filename_verbose = str_replace( $upload_url, $upload_path, $this_file );
    $filename_backslashes = trim( $filename_verbose, ' " ');
    $filename = stripslashes( $filename_backslashes );

    // check the type of file. We'll use this as the 'post_mime_type'
    $filetype = wp_check_filetype( basename( $filename ), null );



    // Prepare an array of post data for the attachment.
    $attachment = array(
        'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
        'post_mime_type' => $filetype['type'],
        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
            //get caption from upload form field
        'post_excerpt'   => rgar( $entry, '8'),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );

    // create a file in the upload folder
    $upload = wp_upload_bits( basename ( $filename ), null,  file_get_contents( $filename ));

    // Insert the attachment.
    $attach_id = wp_insert_attachment( $attachment, $upload['file'] );

    // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );

    // Generate alternate sizes for the attachment, and update the database record.
    $attach_data = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
    wp_update_attachment_metadata( $attach_id, $attach_data );

    // set custom field values
        wp_set_object_terms( $attach_id, rgar( $entry, '2'), 'submitter_name' );
        wp_set_object_terms( $attach_id, rgar( $entry, '3'), 'submitter_email' );
        wp_set_object_terms( $attach_id, rgar( $entry, '4'), 'vhs_year' );
        wp_set_object_terms( $attach_id, rgar( $entry, '6'), 'ensemble' );
        wp_set_object_terms( $attach_id, rgar( $entry, '11'), 'creator_name' );
        wp_set_object_terms( $attach_id, rgar( $entry, '7'), 'copyright' );

// end loop
}

}

// targets the specific form by form ID of 1
add_action( 'gform_after_submission_1', 'form_to_media_library', 10, 2 );

Here's how the taxonomy for the email field on attachments is registered (also in functions.php).

function register_taxonomy_submitter_email(){
$labels = array(
        'name'              => _x( 'Submitter Email', 'taxonomy general name' ),
        'singular_name'     => _x( 'Submitter Email', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Submitter Emails' ),
        'all_items'         => __( 'All Submitter Emails' ),
        'parent_item'       => __( 'Parent Submitter Email' ),
        'parent_item_colon' => __( 'Parent Submitter Email:' ),
        'edit_item'         => __( 'Edit Submitter Email' ),
        'update_item'       => __( 'Update Submitter Email' ),
        'add_new_item'      => __( 'Add New Submitter Email' ),
        'new_item_name'     => __( 'New Submitter Email Name' ),
        'menu_name'         => __( 'Submitter Email' ),
);
$args   = array(
        'hierarchical'      => false, // make it hierarchical (like categories)
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'create_posts'      => false,
        'rewrite'           => [ 'slug' => 'submitter_email' ],
);
register_taxonomy( 'submitter_email', 'attachment', $args );
}

Here's my entire functions.php file.

WordPress 5.9.3

Relevant plugins:

  1. Advanced Custom Fields (some other fields are defined using this plugin, but the email field is not)
  2. Gravity Forms
  3. Media Library Assistant

1 Answer 1

1

When a visitor fills out the email address field with "[email protected]", what is displayed on the Attachment details screen (as in siteurl.com/wp-admin/upload.php?item=xx ) for that email field is "nameemail-com".

Yes, and that is the default behavior, i.e. WordPress displays the term slug instead of name (or the actual email address in your case).

But there's a filter hook you can use, namely attachment_fields_to_edit, if you want the term field to display the name instead of slug.

Working example:

add_filter( 'attachment_fields_to_edit', 'my_attachment_fields_to_edit' );
function my_attachment_fields_to_edit( $form_fields ) {
    $taxonomy = 'submitter_email';

    // Do nothing if the Submitter Email field is not in the fields list.
    if ( ! isset( $form_fields[ $taxonomy ] ) ) {
        return $form_fields;
    }

    // Get the term by its slug.
    $field = (array) $form_fields[ $taxonomy ];
    $term  = empty( $field['taxonomy'] ) ? null :
        get_term_by( 'slug', $field['value'], $taxonomy );

    // Use the term name.
    if ( $term instanceof WP_Term ) {
        $form_fields[ $taxonomy ]['value'] = $term->name;
    }

    return $form_fields;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.