Last active
July 11, 2017 06:01
-
-
Save igorbenic/25de6609eb2ac2557ff48771d6118a4f 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="text" name="attachments[123][field_name]" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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