admin管理员组文章数量:1127173
I am using the following code to add a couple of custom fields to media attachments:
//Add custom fields for attachments
function kgal_add_custom_fields_to_artwork( $form_fields, $post ) {
$form_fields['kgal_artwork_artist'] = array(
'label' => 'Artist',
'input' => 'html',
'html' => get_artists_dropdown( $post->ID ),
'application' => 'image',
'exclusions' => array( 'audio', 'video' ),
);
$form_fields['kgal_artwork_medium'] = array(
'label' => 'Medium',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'kgal_artwork_medium', true ),
'application' => 'image',
'exclusions' => array( 'audio', 'video' ),
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'kgal_add_custom_fields_to_artwork', 10, 2 );
function kgal_save_custom_fields_for_artwork( $post, $attachment ) {
if( isset( $attachment['kgal_artwork_artist'] ) )
update_post_meta( $post['ID'], 'kgal_artwork_artist', $attachment['kgal_artwork_artist'] );
if( isset( $attachment['kgal_artwork_medium'] ) )
update_post_meta( $post['ID'], 'kgal_artwork_medium', $attachment['kgal_artwork_medium'] );
return $post;
}
add_filter( 'attachment_fields_to_save', 'kgal_save_custom_fields_for_artwork', 10, 2 );
// output artists dropdown
function get_artists_dropdown( $id ) {
if( !$id )
return;
$artists = get_posts(
array(
'post_type' => 'artist',
'posts_per_page' => -1,
'post_status' => 'publish',
)
);
if( !$artists )
return;
$output = "<select name='attachments[{$id}][kgal_artwork_artist]' id='attachments[{$id}][kgal_artwork_artist]'>";
foreach( $artists as $artist )
$output .= '<option ' . selected( get_post_meta( $id, "kgal_artwork_artist", true ), $artist->post_id, false ) . ' value="' . $artist->post_id . '">' . $artist->post_title . '</option>';
$output .= '</select>';
return $output;
}
Now when I go to media library to edit an image, the text field i.e. kgal_artwork_medium updates perfectly fine i.e. the saving gif spins and after navigating to another image and getting back to the one I've just edited, I see the correct value there. I've also displayed the field in my attachments page template, and it shows up just fine on the front end.
However, when it comes to the html field i.e. kgal_artwork_artist, things get messy. The dropdown shows up just fine, populated with all the artists by name as expected, and when I select an artist from it, the saving gif also spins just fine, but the actual data doesn't seem to get saved, and upon navigating to another image and getting back, the dropdown always reverts to displaying the last artist in the list. Also, nothing shows up for the artist at the frontend on the attachment's page.
There might be some issue with my selected() function that I'm missing out on here, or perhaps I'm not saving the right value...the dropdown needs to display the artist's name, but save its ID, so that I can utilize the ID to show whatever data I need in my template.
I'm totally lost at this point, but I know I'm really, REALLY close and I've been trying to get this to work for over a couple of months now, to the point when I'm completely frustrated. Any help in this regard will be IMMENSELY appreciated.
I am using the following code to add a couple of custom fields to media attachments:
//Add custom fields for attachments
function kgal_add_custom_fields_to_artwork( $form_fields, $post ) {
$form_fields['kgal_artwork_artist'] = array(
'label' => 'Artist',
'input' => 'html',
'html' => get_artists_dropdown( $post->ID ),
'application' => 'image',
'exclusions' => array( 'audio', 'video' ),
);
$form_fields['kgal_artwork_medium'] = array(
'label' => 'Medium',
'input' => 'text',
'value' => get_post_meta( $post->ID, 'kgal_artwork_medium', true ),
'application' => 'image',
'exclusions' => array( 'audio', 'video' ),
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'kgal_add_custom_fields_to_artwork', 10, 2 );
function kgal_save_custom_fields_for_artwork( $post, $attachment ) {
if( isset( $attachment['kgal_artwork_artist'] ) )
update_post_meta( $post['ID'], 'kgal_artwork_artist', $attachment['kgal_artwork_artist'] );
if( isset( $attachment['kgal_artwork_medium'] ) )
update_post_meta( $post['ID'], 'kgal_artwork_medium', $attachment['kgal_artwork_medium'] );
return $post;
}
add_filter( 'attachment_fields_to_save', 'kgal_save_custom_fields_for_artwork', 10, 2 );
// output artists dropdown
function get_artists_dropdown( $id ) {
if( !$id )
return;
$artists = get_posts(
array(
'post_type' => 'artist',
'posts_per_page' => -1,
'post_status' => 'publish',
)
);
if( !$artists )
return;
$output = "<select name='attachments[{$id}][kgal_artwork_artist]' id='attachments[{$id}][kgal_artwork_artist]'>";
foreach( $artists as $artist )
$output .= '<option ' . selected( get_post_meta( $id, "kgal_artwork_artist", true ), $artist->post_id, false ) . ' value="' . $artist->post_id . '">' . $artist->post_title . '</option>';
$output .= '</select>';
return $output;
}
Now when I go to media library to edit an image, the text field i.e. kgal_artwork_medium updates perfectly fine i.e. the saving gif spins and after navigating to another image and getting back to the one I've just edited, I see the correct value there. I've also displayed the field in my attachments page template, and it shows up just fine on the front end.
However, when it comes to the html field i.e. kgal_artwork_artist, things get messy. The dropdown shows up just fine, populated with all the artists by name as expected, and when I select an artist from it, the saving gif also spins just fine, but the actual data doesn't seem to get saved, and upon navigating to another image and getting back, the dropdown always reverts to displaying the last artist in the list. Also, nothing shows up for the artist at the frontend on the attachment's page.
There might be some issue with my selected() function that I'm missing out on here, or perhaps I'm not saving the right value...the dropdown needs to display the artist's name, but save its ID, so that I can utilize the ID to show whatever data I need in my template.
I'm totally lost at this point, but I know I'm really, REALLY close and I've been trying to get this to work for over a couple of months now, to the point when I'm completely frustrated. Any help in this regard will be IMMENSELY appreciated.
Share Improve this question asked Oct 14, 2015 at 22:18 Haroon Q. RajaHaroon Q. Raja 114 bronze badges1 Answer
Reset to default 0I think you need to change the value of id in select field
$output = "<select name='attachments[{$id}][kgal_artwork_artist]' id='attachments-'.$id.'-kgal_artwork_artist'>";
本文标签: Custom dropdown field added to media gallery how to get it to save properly
版权声明:本文标题:Custom dropdown field added to media gallery; how to get it to save properly? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736695365a1948153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论