Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

dhirlinger on "get attachment post id to display attachment custom field data"

$
0
0

Firstly, I know that this question or those similar to it have already been asked. And yet I have been banging my head against the wall on this for days. Thus, I am posting my first question.
I have created a custom field for attachment images, for a custom post type, using the extremely helpful tutorial on the process by Andy Blackwell (It is here) employing the attachment_fields_to_edit and attachment_fields_to_save hooks just as he does. I can see that the data makes it to the data base and that it shares the same post id there as the image it is paired with. However, I can not get the data, which for now is simple text, to display with the image. In my plugin I have used the img_caption_shortcode hook to add another figcaption tag and I have tested it with text rather than a variable to make sure that aspect is working - and it is. Additionally, when I type the post id directly into get_post_meta the text also displays. So, I know that my problem is passing the correct id. Never the less I have not succeeded! Your help is greatly appreciated.

Below is my code. $dpi is where I want to store the needed data.

function my_image_attachment_fields_to_edit($form_fields, $post) {
    // $form_fields is a special array of fields to include in the attachment form
    // $post is the attachment record in the database
    //     $post->post_type == 'attachment'
    // (attachments are treated as posts in Wordpress)

    // add our custom field to the $form_fields array
    // input type="text" name/id="attachments[$attachment->ID][custom1]"

if( substr($post->post_mime_type, 0, 5) == 'image' ){

	$field_value = get_post_meta($post->ID, "_custom1", true);
	$form_fields["custom1"] = array(
        "label" => __("300dpi Image Name (include extension)"),
        "input" => "text", // this is default if "input" is omitted
        "value" => $field_value ? $field_value : ''
    );
    // if you will be adding error messages for your field,
    // then in order to not overwrite them, as they are pre-attached
    // to this array, you would need to set the field up like this:
    //$form_fields["custom1"]["label"] = __("Custom Text Field");
    //$form_fields["custom1"]["input"] = "text";
    //$form_fields["custom1"]["value"] = get_post_meta($post->ID, "_custom1", true);

    return $form_fields;
	}
}
// attach our function to the correct hook
add_filter("attachment_fields_to_edit", "my_image_attachment_fields_to_edit", 10, 2);

/**
 * @param array $post
 * @param array $attachment
 * @return array
 */
function my_image_attachment_fields_to_save($post, $attachment) {
    // $attachment part of the form $_POST ($_POST[attachments][postID])
    // $post attachments wp post array - will be saved after returned
    //     $post['post_type'] == 'attachment'
	;
//if( substr($post->post_mime_type, 0, 5) == 'image' ){

	if( isset($attachment['custom1']) ){
        // update_post_meta(postID, meta_key, meta_value);
        update_post_meta($post['ID'], '_custom1', $attachment['custom1']);
    }
    return $post;
//}
}

add_filter( 'attachment_fields_to_save', 'my_image_attachment_fields_to_save', 10, 2 );

add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );

function my_img_caption_shortcode( $empty, $attr, $content ){
$atts = shortcode_atts( array(
		'id'	  => '',
		'align'	  => 'alignnone',
		'width'	  => '',
		'caption' => '',
		'class'   => '',
	), $attr, 'caption' );

	$atts['width'] = (int) $atts['width'];
	if ( $atts['width'] < 1 || empty( $atts['caption'] ) )
		return $content;

	if ( ! empty( $atts['id'] ) )
		$atts['id'] = 'id="' . esc_attr( $atts['id'] ) . '" ';

	$class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] );

	//$dpi code was here

		if ( current_theme_supports( 'html5', 'caption' ) ) {
		return '<figure ' . $atts['id'] . 'style="width: ' . (int) $atts['width'] . 'px;" class="' . esc_attr( $class ) . '">'
		. do_shortcode( $content ) . '<figcaption class="wp-caption-text">' . $atts['caption'] . '</figcaption>' .  '<figcaption class="dpi-link">' . $dpi . '</figcaption></figure>';
	}
}
?>

Viewing all articles
Browse latest Browse all 8245

Trending Articles