在WordPress 3.5的附件窗口中保存自定义域

时间:2013-01-28 作者:Faisal Khurshid

我正在使用下面的代码在WP 3.5附件窗口中添加一个自定义文本字段(来自此问题#Expanding new Media Uploader in WordPress 3.5) ...

add_filter( \'attachment_fields_to_edit\', \'xf_attachment_fields\', 10, 2 );
function xf_attachment_fields( $fields, $post ) {
    $meta = get_post_meta($post->ID, \'meta_link\', true);
    $fields[\'meta_link\'] = array(
        \'label\' => \'More Media Management\',
        \'input\' => \'text\',
        \'value\' => $meta,
        // \'html\' => \'<div class="meta_link"><input type="text" /></div>\',
        \'show_in_edit\' => true,
    );
    return $fields;
}

add_filter( \'attachment_fields_to_save\', \'xa_update_attachment_meta\', 4);
function xa_update_attachment_meta($attachment){
    global $post;
    update_post_meta($post->ID, \'meta_link\', $attachment[\'attachments\'][$post->ID][\'meta_link\']);
    return $attachment;
}

add_action(\'wp_ajax_save-attachment-compat\', \'xa_media_xtra_fields\', 0, 1);
function xa_media_xtra_fields() {
    $post_id = $_POST[\'id\'];
    $meta = $_POST[\'attachments\'][$post_id ][\'meta_link\'];
    update_post_meta($post_id , \'meta_link\', $meta);
    clean_post_cache($post_id);
}
这段代码在那里添加了字段,我还可以保存并随后检索我的任何附件的值。但问题是this code does NOT saves value of any attachment by using AJAX.

也就是说,如果我在这个自定义字段中为任何附件图像定义了任何值,那么选择另一个图像显然会删除我为该图像定义的值。但是,如果我更新帖子并再次检查该字段,保存的值就在那里。

1 个回复
SO网友:Björn Nordqvist

我希望能够将作者信息添加到附件中,并合并了以下代码:http://www.billerickson.net/wordpress-add-custom-fields-media-gallery/ 和你提到的那个。我通过AJAX在模式窗口中实现了完全的工作。修改后的代码如下:

/**
 * Add Author Name and URL fields to media uploader
 *
 * @param $form_fields array, fields to include in attachment form
 * @param $post object, attachment record in database
 * @return $form_fields, modified form fields
 */
function admin_attachment_field_media_author_credit( $form_fields, $post ) {

    $form_fields[\'media-author-name\'] = array(
        \'label\' => \'Author Name\',
        \'input\' => \'text\',
        \'value\' => get_post_meta( $post->ID, \'media_author_name\', true )
        //\'helps\' => \'If provided, author credit will be displayed\'
    );

    $form_fields[\'media-author-url\'] = array(
        \'label\' => __(\'Author URL\',b()),
        \'input\' => \'text\',
        \'value\' => get_post_meta( $post->ID, \'media_author_url\', true ) 
        //\'helps\' => \'If provided, the author credit will be linked\'
    );

    return $form_fields;

} add_filter( \'attachment_fields_to_edit\', \'admin_attachment_field_media_author_credit\', 10, 2 );

/**
 * Save values of Author Name and URL in media uploader
 *
 * @param $post array, the post data for database
 * @param $attachment array, attachment fields from $_POST form
 * @return $post array, modified post data
 */

function admin_attachment_field_media_author_credit_save( $post, $attachment ) {

    if( isset( $attachment[\'media-author-name\'] ) )
        update_post_meta( $post[\'ID\'], \'media_author_name\', $attachment[\'media-author-name\'] );

    if( isset( $attachment[\'media-author-url\'] ) )
        update_post_meta( $post[\'ID\'], \'media_author_url\', $attachment[\'media-author-url\'] );

    return $post;

} add_filter( \'attachment_fields_to_save\', \'admin_attachment_field_media_author_credit_save\', 10, 2 );

/**
 * Save values of Author Name and URL in media uploader modal via AJAX
 */

function admin_attachment_field_media_author_credit_ajax_save() {

    $post_id = $_POST[\'id\'];

    if( isset( $_POST[\'attachments\'][$post_id][\'media-author-name\'] ) )
        update_post_meta( $post_id, \'media_author_name\', $_POST[\'attachments\'][$post_id][\'media-author-name\'] );

    if( isset( $_POST[\'attachments\'][$post_id][\'media-author-url\'] ) )
        update_post_meta( $post_id, \'media_author_url\', $_POST[\'attachments\'][$post_id][\'media-author-url\'] );

    clean_post_cache($post_id);

} add_action(\'wp_ajax_save-attachment-compat\', \'admin_attachment_field_media_author_credit_ajax_save\', 0, 1); 
希望这是有帮助的!

P、 下一个挑战是将其附加到文档中上载的图像中。D、 S。

结束
在WordPress 3.5的附件窗口中保存自定义域 - 小码农CODE - 行之有效找到问题解决它

在WordPress 3.5的附件窗口中保存自定义域

时间:2013-01-28 作者:Faisal Khurshid

我正在使用下面的代码在WP 3.5附件窗口中添加一个自定义文本字段(来自此问题#Expanding new Media Uploader in WordPress 3.5) ...

add_filter( \'attachment_fields_to_edit\', \'xf_attachment_fields\', 10, 2 );
function xf_attachment_fields( $fields, $post ) {
    $meta = get_post_meta($post->ID, \'meta_link\', true);
    $fields[\'meta_link\'] = array(
        \'label\' => \'More Media Management\',
        \'input\' => \'text\',
        \'value\' => $meta,
        // \'html\' => \'<div class="meta_link"><input type="text" /></div>\',
        \'show_in_edit\' => true,
    );
    return $fields;
}

add_filter( \'attachment_fields_to_save\', \'xa_update_attachment_meta\', 4);
function xa_update_attachment_meta($attachment){
    global $post;
    update_post_meta($post->ID, \'meta_link\', $attachment[\'attachments\'][$post->ID][\'meta_link\']);
    return $attachment;
}

add_action(\'wp_ajax_save-attachment-compat\', \'xa_media_xtra_fields\', 0, 1);
function xa_media_xtra_fields() {
    $post_id = $_POST[\'id\'];
    $meta = $_POST[\'attachments\'][$post_id ][\'meta_link\'];
    update_post_meta($post_id , \'meta_link\', $meta);
    clean_post_cache($post_id);
}
这段代码在那里添加了字段,我还可以保存并随后检索我的任何附件的值。但问题是this code does NOT saves value of any attachment by using AJAX.

也就是说,如果我在这个自定义字段中为任何附件图像定义了任何值,那么选择另一个图像显然会删除我为该图像定义的值。但是,如果我更新帖子并再次检查该字段,保存的值就在那里。

1 个回复
SO网友:Björn Nordqvist

我希望能够将作者信息添加到附件中,并合并了以下代码:http://www.billerickson.net/wordpress-add-custom-fields-media-gallery/ 和你提到的那个。我通过AJAX在模式窗口中实现了完全的工作。修改后的代码如下:

/**
 * Add Author Name and URL fields to media uploader
 *
 * @param $form_fields array, fields to include in attachment form
 * @param $post object, attachment record in database
 * @return $form_fields, modified form fields
 */
function admin_attachment_field_media_author_credit( $form_fields, $post ) {

    $form_fields[\'media-author-name\'] = array(
        \'label\' => \'Author Name\',
        \'input\' => \'text\',
        \'value\' => get_post_meta( $post->ID, \'media_author_name\', true )
        //\'helps\' => \'If provided, author credit will be displayed\'
    );

    $form_fields[\'media-author-url\'] = array(
        \'label\' => __(\'Author URL\',b()),
        \'input\' => \'text\',
        \'value\' => get_post_meta( $post->ID, \'media_author_url\', true ) 
        //\'helps\' => \'If provided, the author credit will be linked\'
    );

    return $form_fields;

} add_filter( \'attachment_fields_to_edit\', \'admin_attachment_field_media_author_credit\', 10, 2 );

/**
 * Save values of Author Name and URL in media uploader
 *
 * @param $post array, the post data for database
 * @param $attachment array, attachment fields from $_POST form
 * @return $post array, modified post data
 */

function admin_attachment_field_media_author_credit_save( $post, $attachment ) {

    if( isset( $attachment[\'media-author-name\'] ) )
        update_post_meta( $post[\'ID\'], \'media_author_name\', $attachment[\'media-author-name\'] );

    if( isset( $attachment[\'media-author-url\'] ) )
        update_post_meta( $post[\'ID\'], \'media_author_url\', $attachment[\'media-author-url\'] );

    return $post;

} add_filter( \'attachment_fields_to_save\', \'admin_attachment_field_media_author_credit_save\', 10, 2 );

/**
 * Save values of Author Name and URL in media uploader modal via AJAX
 */

function admin_attachment_field_media_author_credit_ajax_save() {

    $post_id = $_POST[\'id\'];

    if( isset( $_POST[\'attachments\'][$post_id][\'media-author-name\'] ) )
        update_post_meta( $post_id, \'media_author_name\', $_POST[\'attachments\'][$post_id][\'media-author-name\'] );

    if( isset( $_POST[\'attachments\'][$post_id][\'media-author-url\'] ) )
        update_post_meta( $post_id, \'media_author_url\', $_POST[\'attachments\'][$post_id][\'media-author-url\'] );

    clean_post_cache($post_id);

} add_action(\'wp_ajax_save-attachment-compat\', \'admin_attachment_field_media_author_credit_ajax_save\', 0, 1); 
希望这是有帮助的!

P、 下一个挑战是将其附加到文档中上载的图像中。D、 S。

相关推荐

将文件上载到Uploads文件夹时向自定义用户角色发送电子邮件通知

我已经编写了一个自定义php,允许我的模板页面中的用户从前端上传新创建的子文件夹中的文件user_login 作为其名称uploads 文件夹因此,将上载根文件夹,并在用户的子文件夹中存储所有文件。这是我的密码。<form enctype="multipart/form-data" action="" method="POST" class="centered"> <p><