Last active
April 27, 2017 17:37
-
-
Save kosinix/5493051 to your computer and use it in GitHub Desktop.
Adds custom field to WordPress 3.5 Media Manager. The field's unique key is 'meta_link'.
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 | |
// In your functions.php | |
/** | |
* Add custom field | |
*/ | |
function mytheme_attachment_fields( $fields, $post ) { | |
$meta = get_post_meta($post->ID, 'meta_link', true); | |
$fields['meta_link'] = array( | |
'label' => 'Video URL', | |
'input' => 'text', | |
'value' => $meta, | |
'show_in_edit' => true, | |
); | |
return $fields; | |
} | |
add_filter( 'attachment_fields_to_edit', 'mytheme_attachment_fields', 10, 2 ); | |
/** | |
* Update custom field on save | |
*/ | |
function mytheme_update_attachment_meta($attachment){ | |
global $post; | |
update_post_meta($post->ID, 'meta_link', $attachment['attachments'][$post->ID]['meta_link']); | |
return $attachment; | |
} | |
add_filter( 'attachment_fields_to_save', 'mytheme_update_attachment_meta', 4); | |
/** | |
* Update custom field via ajax | |
*/ | |
function mytheme_media_xtra_fields() { | |
$post_id = $_POST['id']; | |
$meta = $_POST['attachments'][$post_id ]['meta_link']; | |
update_post_meta($post_id , 'meta_link', $meta); | |
clean_post_cache($post_id); | |
} | |
add_action('wp_ajax_save-attachment-compat', 'mytheme_media_xtra_fields', 0, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment