Skip to content

Instantly share code, notes, and snippets.

@sonipb
Forked from igorbenic/att.html
Created July 11, 2017 06:01
Show Gist options
  • Save sonipb/a2312d6243ee8ce610a7ddf6277dc72a to your computer and use it in GitHub Desktop.
Save sonipb/a2312d6243ee8ce610a7ddf6277dc72a to your computer and use it in GitHub Desktop.
Extending the WordPress Media Uploader: Custom Fields | http://www.ibenic.com/extending-wordpress-media-uploader-custom-fields
<input type="text" name="attachments[123][field_name]" />
<?php
/**
* Adding a custom field to Attachment Edit Fields
* @param array $form_fields
* @param WP_POST $post attachment post object
* @return array
*/
function ibenic_add_media_custom_field( $form_fields, $post ) {
// Adding the form fields
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'ibenic_add_media_custom_field', null, 2 );
<?php
/**
* Adding a custom field to Attachment Edit Fields
* @param array $form_fields
* @param WP_POST $post
* @return array
*/
function ibenic_add_media_custom_field( $form_fields, $post ) {
$field_value = get_post_meta( $post->ID, 'info', true );
$form_fields['info'] = array(
'value' => $field_value ? $field_value : '',
'label' => __( 'Info' ),
'helps' => __( 'Enter some additional info' ),
'input' => 'textarea'
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'ibenic_add_media_custom_field', null, 2 );
<?php
/**
* Saving the attachment data
* @param integer $attachment_id
* @return void
*/
function ibenic_save_attachment( $attachment_id ) {
if ( isset( $_REQUEST['attachments'][ $attachment_id ]['info'] ) ) {
$info = $_REQUEST['attachments'][ $attachment_id ]['info'];
update_post_meta( $attachment_id, 'info', $info );
}
}
add_action( 'edit_attachment', 'ibenic_save_attachment' );
<?php
/**
* Saving the attachment data
* @param integer $attachment_id
* @return void
*/
function ibenic_save_attachment( $attachment_id ) {
// Saving the data
}
add_action( 'edit_attachment', 'ibenic_save_attachment' );
/**
* @param array $post
* @param array $attachment
* @return array
*/
function ibenic_fields_to_save( $post, $attachment ) {
// Check the field in $attachment and save if needed
return $post;
}
add_filter( 'attachment_fields_to_save', 'ibenic_fields_to_save', 20, 2 );
<?php
$_POST['attachments'][att_id]['tags] = array(
1 => 'adminstrator',
2 => 'another_user',
...
);
<?php
/**
* Adding a custom field to Attachment Edit Fields
* @param array $form_fields
* @param WP_POST $post
* @return array
*/
function ibenic_add_media_custom_field( $form_fields, $post ) {
$field_value = get_post_meta( $post->ID, 'info', true );
$form_fields['info'] = array(
'value' => $field_value ? $field_value : '',
'label' => __( 'Info' ),
'helps' => __( 'Enter some additional info' ),
'input' => 'textarea'
);
// Getting the tags
$tags = get_post_meta( $post->ID, 'tags', true );
// Getting all the users
$users = get_users();
$tag_html = '';
foreach ( $users as $user ) {
// Adding the checkbox HTML with the 'checked="checked"' attribute if the user ID is a tag key
$tag_html .= '<input ' . checked( array_key_exists( $user->ID, $tags ), true, false ) . ' type="checkbox" name="attachments[' . $post->ID . '][tag][' . $user->ID . ']"> ' . $user->display_name . '<br/>';
}
// Adding the tag field
$form_fields['tag'] = array(
'label' => __( 'Tags:' ),
'input' => 'html',
'html' => $tag_html
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'ibenic_add_media_custom_field', null, 2 );
<?php
/**
* Saving the attachment data
* @param integer $attachment_id
* @return void
*/
function ibenic_save_attachment( $attachment_id ) {
if ( isset( $_REQUEST['attachments'][ $attachment_id ]['info'] ) ) {
$info = $_REQUEST['attachments'][ $attachment_id ]['info'];
update_post_meta( $attachment_id, 'info', $info );
}
if ( isset( $_REQUEST['attachments'][ $attachment_id ]['tag'] ) ) {
$tags = $_REQUEST['attachments'][ $attachment_id ]['tag'];
update_post_meta( $attachment_id, 'tags', $tags );
}
}
add_action( 'edit_attachment', 'ibenic_save_attachment' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment