I’m working on my first WP plugin, and am stuck.
I created a custom field (field 1) on the post page below the content editor. It saves correctly. 🙂
I created a custom field (field 2) inside the Media Library popup when adding media. It saves correctly. 🙂
What I’m wanting to do, is use the value from field 1 as the default value for field 2.
I’m suspecting that the problem lies within the attachment_fields_to_edit callback function.
I think that $post is now referring to the actual “file attachment post” rather than the post itself, so when I’m referencing my saved values:
$post_meta = get_post_meta( $post->ID );
it is actually pulling all of the meta associated with that attachment, and not with the current post. Is it possible to pull the meta from the actual post?
This code is from the Codex:
function my_add_attachment_location_field( $form_fields, $post ) {
$field_value = get_post_meta( $post->ID, 'location', true );
$form_fields['location'] = array(
'value' => $field_value ? $field_value : '',
'label' => __( 'Location' ),
'helps' => __( 'Set a location for this attachment' )
);
return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'my_add_attachment_location_field', 10, 2 );
function my_save_attachment_location( $attachment_id ) {
if ( isset( $_REQUEST['attachments'][$attachment_id]['location'] ) ) {
$location = $_REQUEST['attachments'][$attachment_id]['location'];
update_post_meta( $attachment_id, 'location', $location );
}
}
add_action( 'edit_attachment', 'my_save_attachment_location' );
How would I get_post_meta for the current post that we are inserting the attachment into? This would need to happen in the my_add_attachment_location_field callback function in the codex code above.
Thanks!
One way I can think of:
Then you can do:
You can try the following:
and
to update the
location
post meta value of the parent post, from the media popup.You might also want to check this out, if you edit the attachment directly from the media library pages:
I’m not sure what kind of workflow you have in mind, but I think there’s a problem with your idea, as I understand it:
So I wonder if you could use a hidden post meta value instead, for example
_location
?Hope this helps.
ok try a different way……you wont be easily able to get the post id whilst in the navigator. Im not sure what the location is but if you want to save images as post meta, i use this…………
steps:
1.create a new js file + visit http://jsfiddle.net/dheffernan/BB37U/2/ and copy the js there into the js file. call it miu_script.js or if you want to change it you need to make some mods to the code below. Save it in your plugin folder (change path below if you want to move it into subfolder.
enter in the code below into your functions – it will save the image location as a serialized url and in a field called “_images” again change if you want by modifying the code.
There may be errors below, i had this in Oop format so let me know if there are issues. . If there are, note php errors, if no php errors but it dosent work, check the console in chrome or firefox. Ill be able to debug.
in your functions php
just a simple solution
working fine with my plugin
$_REQUEST[‘post_id’] is actual wp post ID
and with get_post_meta you can get your metabox location ..
🙂
I dont know if i am just guessing it. you can use this function