WordPress Get Post Meta from Callback Function

I am working on my first WP plugin and am stuck.

I created my own field (field 1) on the page after the content editor. This is saved correctly. :)

I created my own field (field 2) inside the media library popup when adding media. This is saved correctly. :)

What I want to do is use the value from field 1 as the default value for field 2.

I suspect the problem is the attachment_fields_to_edit callback function.

I think that $ post now refers to the actual "post attachment file" and not the post, so when I refer to my saved values:

$post_meta = get_post_meta( $post->ID ); 

it actually pulls the entire meta related to this attachment, not the current message. Can I get the meta out of the actual post?

This code is from 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 do I get get_post_meta for the current message in which we embed the attachment? This should happen in the my_add_attachment_location_field callback function in the codex code above.

Thanks!

+7
post php wordpress meta
source share
5 answers

One way that I can think of:

$ actual_post_id = $ post-> post_parent;

Then you can do:

get_post_meta ($ actual_post_id)

+1
source share

You can try the following:

 /** * Display custom 'location' attachment field */ function so_22850878_attachment_fields_to_edit( $form_fields, $post ) { $field_value = get_post_meta( $post->post_parent, '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', 'so_22850878_attachment_fields_to_edit', 10, 2 ); 

and

 /** * Edit attachment fields */ function so_22850878_edit_attachment( $attachment_id ) { $p = get_post( $attachment_id ); if ( isset( $_REQUEST['attachments'][$attachment_id]['location'] ) ) { $location = $_REQUEST['attachments'][$attachment_id]['location']; // Save the value of the 'location' attachment field // to the 'location' post meta of the post parent if it exists: if( ! is_null( $p ) && 0 < $p->post_parent ) update_post_meta( $p->post_parent, 'location', sanitize_text_field( $location ) ); } } add_action( 'edit_attachment', 'so_22850878_edit_attachment' ); 

to update the meta value of the location column of the parent message from the popup.

You can also check this if you edit the attachment directly from the pages of the media library:

 /** * Save attachment fields */ function so_22850878_attachment_fields_to_save( $post, $attachment ) { $p = get_post( $post['ID'] ); // Save the value of the 'location' attachment field // to the 'location' post meta of the post parent if it exists: if( isset( $attachment['location'] ) && ! is_null( $p ) && 0 < $p->post_parent ) update_post_meta( $p->post_parent, 'location', sanitize_text_field( $attachment['location'] ) ); return $post; } add_action( 'attachment_fields_to_save', 'so_22850878_attachment_fields_to_save', 10, 2 ); 

I'm not sure which workflow you have in mind, but I think there is a problem with your idea, as I understand it:

When you update the location field in the popup menu, it looks like you want to update the meta value of the location post for the parent message. But since the message editing screen does not refresh when you insert images into the message editor, your location value will be overwritten with the old value when updating the message.

So, I wonder if it is possible to use the meta value of a hidden message instead, such as _location ?

Hope this helps.

+1
source share

ok try differently ... you cannot easily get the message id during navigation. I'm not sure what the location is, but if you want to save images as metadata, I use this ............

actions:

1.Create a new js + file, visit http://jsfiddle.net/dheffernan/BB37U/2/ and copy js there to the js file. name it miu_script.js, or if you want to change it, you need to make some mods for the code below. Save it in your plugin folder (change the path below if you want to move it to a subfolder.

  • enter the code below into your functions - it will save the image location as a serialized URL and in the field with the name "_images" will change again if you want by changing the code.

  • There may be errors, I had this in Oop format, so let me know if there are problems. If there is, pay attention to php errors, if there are no php errors, but it works as an assistant professor, check the console in chrome or firefox. I can not debug.

in your php functions

 function add_image_meta_box() { add_meta_box( 'multi_image_upload_meta_box' , __('Upload Multiple Images', 'miu_textdomain') , 'render_meta_box_content' , $post_type , 'advanced' , 'high' ); } add_action( 'add_meta_boxes', 'add_image_meta_box' ); function render_meta_box_content($post) { wp_nonce_field('miu_inner_custom_box', 'miu_inner_custom_box_nonce'); $value = get_post_meta($post->ID, '_images', true); // <-- change field if wanted, there is 1 more below that will need the same name $metabox_content = '<div id="miu_images"></div><input type="button" onClick="addRow()" value="Add Image" class="button" />'; echo $metabox_content; $images = unserialize($value); //<--- when using the images use this!! $script = "<script> itemsCount= 0;"; if (!empty($images)) { foreach ($images as $image) { $script.="addRow('{$image}');"; } } $script .="</script>"; echo $script; } function save_image($post_id){ if (!isset($_POST['miu_inner_custom_box_nonce'])) return $post_id; $nonce = $_POST['miu_inner_custom_box_nonce']; if (!wp_verify_nonce($nonce, 'miu_inner_custom_box')) return $post_id; if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; if ('page' == $_POST['post_type']) { if (!current_user_can('edit_page', $post_id)) return $post_id; } else { if (!current_user_can('edit_post', $post_id)) return $post_id; } $posted_images = $_POST['miu_images']; $miu_images = array(); foreach ($posted_images as $image_url) { if(!empty ($image_url)) $miu_images[] = esc_url_raw($image_url); } update_post_meta($post_id, '_images', serialize($miu_images));<--if you changed this above.......make sure they match } add_action( 'save_post', 'save_image' ); function enqueue_scripts($hook){ if ('post.php' != $hook && 'post-edit.php' != $hook && 'post-new.php' != $hook) return; wp_enqueue_script('miu_script', plugin_dir_url(__FILE__) . 'miu_script.js', array('jquery')); //<--this is the path!!! change if wanted (prob a good idea to add to js folder) } add_action('admin_enqueue_scripts', 'enqueue_scripts'); 
0
source share

a simple solution

 function my_add_attachment_location_field( $form_fields, $post ) { $field_value = get_post_meta( $post->ID, 'location', true ); $default = get_post_meta( $_REQUEST['post_id'] , 'location', true ) ? get_post_meta( $_REQUEST['post_id'] , 'location', true ): "Location Not Yet Set"; $form_fields['location'] = array( 'value' => $field_value ? $field_value : $default, '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 ); 

works fine with my plugin

$ _ REQUEST ['post_id'] is the actual identifier of the wp message and with get_post_meta you can get your metabox location. :)

0
source share

I don’t know if I’m just guessing it. you can use this function

  wp_reset_query(); global $post; $post->ID // retrive the global $post id; 
0
source share

All Articles