Search
Close this search box.

wordpress上传特色图片,更新特色图片,删除特色图片,删除媒体库图片,编辑么题库图片属性

function add_votes_opload_img()
{
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    require_once(ABSPATH . 'wp-admin/includes/file.php');
    require_once(ABSPATH . 'wp-admin/includes/media.php');
    $id = $_POST['activity_id'];
   //获取该用户活动图片start

    $userid = get_current_user_id();
    $datas = get_post($id);
    $title = $datas->post_title;
    $content = $datas->post_content;
    $arg = array(
        'post_type' => 'joinactivity',
        'post_author' => $userid,
        'post_status' => array('publish'),
        'meta_query' => array(
            array(
                'key' => '_activity',
                'value' => $id,
            )
        )
    );
    $arrList = new WP_Query($arg);
    $p= $arrList->posts;
    // print_r("<pre>");
    // print_r($p);
    if(count($p) == 1){
        $url_pre = get_post_thumbnail_id($p[0]->ID);
        wp_delete_attachment( $url_pre, true );
        delete_post_thumbnail($p[0]->ID);
        $attachment_id = media_handle_upload('file', $p[0]->ID);
        set_post_thumbnail($p[0]->ID, $attachment_id);
        update_post_meta($p[0]->ID, '_activity', $id);
        $my_image_meta = array(
            'ID'        => $attachment_id,          // Specify the image (ID) to be updated
            'post_title'    => $title,      // Set image Title to sanitized title
            'post_excerpt'  => $title,      // Set image Caption (Excerpt) to sanitized title
            'post_content'  => $content,        // Set image Description (Content) to sanitized title
        );
        wp_update_post( $my_image_meta );
        $url = wp_get_attachment_url($attachment_id); 
        $permalink = get_permalink($p[0]->ID);
        wp_send_json(array("share_link" => $permalink, "download_link" => $url));
    }else if(count($p) == 0){
        $my_post = array(
            'post_status' => 'publish',
            'post_author' => get_current_user_id(),
            'post_type' => 'joinactivity',
            'post_title' => $title,
            'post_content' => $content
        );
 

发表评论

Optimized by WPJAM Basic