dongwu8653 2016-12-01 11:24
浏览 46
已采纳

为特色图片添加用户元

I'm developing a site in which a user is able to capture his/her pic(via .getusermedia) which updated as the post thumbnail.

The problem is that the post thumbnail gets updated for all users - I want the post thumbnail to be updated only for that particular user

function Generate_Featured_Image( $filename, $parent_post_id  ){
    require('/wp-load.php');

    $filetype = wp_check_filetype( basename( $filename ), null );

    $wp_upload_dir = wp_upload_dir();

    $attachment = array(
        'guid'           => $wp_upload_dir['url'] . '/' . basename(  $filename ), 
        'post_mime_type' => $filetype['type'],
        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename(     $filename ) ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );

    $attach_id = wp_insert_attachment( $attachment, $filename,  $parent_post_id );

    require_once( ABSPATH . 'wp-admin/includes/image.php' );

    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    wp_update_attachment_metadata( $attach_id, $attach_data );

    //add_user_meta( $current_user_id, $parent_post_id, $attach_id);
    set_post_thumbnail( $parent_post_id, $attach_id );
}

script.php

Generate_Featured_Image( $addroot.$current_user_id.$extimage, 88  );
// addroot=path ext-extension(.jpg) (this is name of file saved)

i tried to user add_user_meta to accomplish the task but couldn't even get a start

UPDATE

<?php 
// $filename is succesfully saved as currentuserid+.jpg.
 $addroot = '/wp-content/uploads/2016/09/';


  $current_user_id = get_current_user_id();


$extimage = '.jpg';


$filename = $addroot.$current_user_id.$extimage;

 $filetype = wp_check_filetype( basename( $filename ), null );

 // Get the path to the upload directory.
 $wp_upload_dir = wp_upload_dir();

 // Prepare an array of post data for the attachment.
 $attachment = array(
'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
'post_mime_type' => $filetype['type'],
'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content'   => '',
'post_status'    => 'inherit'
  );

// Insert the attachment.
 $attach_id = wp_insert_attachment( $attachment, $filename, 0 );

 require_once( ABSPATH . 'wp-admin/includes/image.php' );

  // Generate the metadata for the attachment, and update the database record.
  $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
      wp_update_attachment_metadata( $attach_id, $attach_data );
     update_user_meta( get_the_ID(), $filename , $_POST[ $filename ] );
  ?>

how to proceed further by calling the image to by $attach_id from usermeta and displayinf it to user as thumbnail- unable to figure this step

  • 写回答

1条回答 默认 最新

  • douzhi3586 2016-12-01 12:07
    关注

    You can't really set a different featured image for post based on a user. There is just one post, therefore only one featured image for it.
    If you want to display a different image, based on which user is viewing the post, save that use wp_insert_attachment( $attachment, $filename, 0) to insert attachment without binding it to the post. Then save that $attach_id to the usermeta table. Then, when user is viewing that post, simply get $attach_id with get_user_meta and display that image (you can use wp_get_attachment_url for example) instead of featured image of the post.

    UPDATE
    First, saving user meta should look like this

    update_user_meta($current_user_id, '_avatar_id', $attach_id);
    

    Second, in the beginning, you should check if user is logged in with is_user_logged_in function.

    Third, you should check for user had avatar before and remove it (I mean, why store their old one after they have a new one, right?) like so:

    $old_attach=get_user_meta($current_user_id, '_avatar_id', true);
    if(is_numeric($old_attach))
    {
        wp_delete_attachment($old_attach, true);
    }
    

    Your final code to save avatar should look like:

    if (is_user_logged_in())
    {
        $addroot = '/wp-content/uploads/2016/09/';
        $current_user_id = get_current_user_id();
        $extimage = '.jpg';
        $filename = $addroot . $current_user_id . $extimage;
        $filetype = wp_check_filetype(basename($filename), null);
        $wp_upload_dir = wp_upload_dir();
        $attachment = array(
            'guid' => $wp_upload_dir['url'] . '/' . basename($filename),
            'post_mime_type' => $filetype['type'],
            'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
            'post_content' => '',
            'post_status' => 'inherit'
        );
        $attach_id = wp_insert_attachment($attachment, $filename, 0);
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
        wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id, $filename));
        $old_attach = get_user_meta($current_user_id, '_avatar_id', true);
        if (is_numeric($old_attach))
        {
            wp_delete_attachment($old_attach, true);
        }
        update_user_meta($current_user_id, '_avatar_id', $attach_id);
    }
    else
    {
        //show error here or something   
    }
    

    Now, to access it, you can write a simple function, like this:

    function get_user_avatar()
    {
        if (is_user_logged_in())
        {
            $avatar_id = get_user_meta(get_current_user_id(), '_avatar_id', true);
            if (is_numeric($avatar_id))
            {
                return'<img src="' . wp_get_attachment_url($avatar_id) . '"  alt="User avatar"/>';
            }
            else
            {
                return '<img src="url_to_your_default_avatar" alt="User avatar"/>';
            }
        }
        return false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP