dsgixl5195 2019-03-27 10:37
浏览 97

如何在WordPress的前端输出附件post meta

I am creating a small plugin to add a 'data-pin-description' attribute to my images. I intend for the client to be able to add that meta data description to any image attachment from admin and then have that meta value be output to the tag on the front end, when the attachment is added to post content. I want the plugin to grab that meta data at the appropriate time and include it with the img tag data- attribute and value, before it's already output. End result should be:

My admin code seems to be accepting and saving the client-entered value just find, however I am having trouble outputting that data. One problem is I have been unable to identify which hook would retrieve and add that data to the tag at the correct time

I've tried querying the attachment posts but I don't know what action or filter to hook into

Here is how I'm adding the meta data in admin:

<?php

//exit if file is called directly
if (! defined( 'ABSPATH' ) ) {

    exit;

}

/**
 * Adding a custom field to Attachment Edit Fields
 * @param  array $form_fields 
 * @param  WP_POST $post        
 * @return array              
 */


        //add attachment fields
        function ad_add_pinterest_fields( $form_fields, $post ) {

            $field_value = get_post_meta( $post->ID, 'pin-description', true );

            $form_fields['pin-description'] = array(
                'value' => $field_value ? esc_textarea($field_value) : '',
                'label' => __( 'Pin Description' ),
                'helps' => __( 'Add a short description for Pinterest SEO' ),
                'input' => 'textarea'
            );

            return $form_fields;
          }


            add_filter( 'attachment_fields_to_edit', 'ad_add_pinterest_fields', null, 2 );

        //save attachment fields
        function ad_save_pinterest_fields( $attachment_id ) {

            if ( isset( $_REQUEST['attachments'][$attachment_id]['pin-description'] ) ) {
                $pinDescription = sanitize_text_field( $_REQUEST['attachments'][$attachment_id]['pin-description'] );
                update_post_meta( $attachment_id, 'pin-description', $pinDescription );
            }

        }

            add_action( 'edit_attachment', 'ad_save_pinterest_fields' );

?>

I expect to retrieve the post_meta of each attachment image for a given post, but have only received errors

  • 写回答

1条回答 默认 最新

  • dream0776 2019-04-24 11:10
    关注

    to solve this I ended up hooking into image_send_to_editor to modify the image html prior to it's being sent to the editor. this way, with the image html in the editor and correctly displaying my attribute and value, when I click 'update post' the html is saved to the database with those pieces of data. when the post is viewed on the frontend, the output from the database correctly reflects as well.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值