Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress Get Post Meta from Callback Function

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!

like image 397
Aaron Avatar asked Apr 03 '14 23:04

Aaron


2 Answers

One way I can think of:

$actual_post_id = $post->post_parent;

Then you can do:

get_post_meta($actual_post_id)

like image 123
Edenbauer Avatar answered Oct 19 '22 04:10

Edenbauer


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 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:

/**
 * 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 what kind of workflow you have in mind, but I think there's a problem with your idea, as I understand it:

When you update the location field in the media popup, it looks like you want to update the location post meta value for the parent post. But since the post edit screen doesn't update when you insert the image into the post editor, your location value will be overwritten with the old value when you update the post.

So I wonder if you could use a hidden post meta value instead, for example _location?

Hope this helps.

like image 45
birgire Avatar answered Oct 19 '22 04:10

birgire