• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP wp_insert_attachment函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了PHP中wp_insert_attachment函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_insert_attachment函数的具体用法?PHP wp_insert_attachment怎么用?PHP wp_insert_attachment使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了wp_insert_attachment函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的PHP代码示例。

示例1: handle_upload

 /**
  * Upload
  * Ajax callback function
  *
  * @return string Error or (XML-)response
  */
 static function handle_upload()
 {
     check_admin_referer('rwmb-upload-images_' . $_REQUEST['field_id']);
     $post_id = 0;
     if (is_numeric($_REQUEST['post_id'])) {
         $post_id = (int) $_REQUEST['post_id'];
     }
     // You can use WP's wp_handle_upload() function:
     $file = $_FILES['async-upload'];
     $file_attr = wp_handle_upload($file, array('test_form' => true, 'action' => 'plupload_image_upload'));
     $attachment = array('guid' => $file_attr['url'], 'post_mime_type' => $file_attr['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($file['name'])), 'post_content' => '', 'post_status' => 'inherit');
     // Adds file as attachment to WordPress
     $id = wp_insert_attachment($attachment, $file_attr['file'], $post_id);
     if (!is_wp_error($id)) {
         wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file_attr['file']));
         // Save file ID in meta field
         if (isset($_REQUEST['field_id'])) {
             add_post_meta($post_id, $_REQUEST['field_id'], $id, false);
         }
         $response = new WP_Ajax_Response();
         $response->add(array('what' => 'rwmb_image_response', 'data' => self::img_html($id)));
         $response->send();
     }
     exit;
 }
开发者ID:scotlanddig,项目名称:bootstrap_basic,代码行数:31,代码来源:plupload-image.php


示例2: upload_file

 /**
  * @param strin $file_url
  *
  * @return int
  */
 private function upload_file($file_url)
 {
     if (!filter_var($file_url, FILTER_VALIDATE_URL)) {
         return false;
     }
     $contents = @file_get_contents($file_url);
     if ($contents === false) {
         return false;
     }
     $upload = wp_upload_bits(basename($file_url), null, $contents);
     if (isset($upload['error']) && $upload['error']) {
         return false;
     }
     $type = '';
     if (!empty($upload['type'])) {
         $type = $upload['type'];
     } else {
         $mime = wp_check_filetype($upload['file']);
         if ($mime) {
             $type = $mime['type'];
         }
     }
     $attachment = array('post_title' => basename($upload['file']), 'post_content' => '', 'post_type' => 'attachment', 'post_mime_type' => $type, 'guid' => $upload['url']);
     $id = wp_insert_attachment($attachment, $upload['file']);
     wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file']));
     return $id;
 }
开发者ID:acutedeveloper,项目名称:havering-intranet-development,代码行数:32,代码来源:Featured_Image_Uploader.php


示例3: custom_media_sideload_image

/**
 * media_sideload_image, but returns ID
 * @param  string  $image_url [description]
 * @param  boolean $post_id   [description]
 * @return [type]             [description]
 */
function custom_media_sideload_image($image_url = '', $post_id = false)
{
    require_once ABSPATH . 'wp-admin/includes/file.php';
    $tmp = download_url($image_url);
    // Set variables for storage
    // fix file filename for query strings
    preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $image_url, $matches);
    $file_array['name'] = basename($matches[0]);
    $file_array['tmp_name'] = $tmp;
    // If error storing temporarily, unlink
    if (is_wp_error($tmp)) {
        @unlink($file_array['tmp_name']);
        $file_array['tmp_name'] = '';
    }
    $time = current_time('mysql');
    $file = wp_handle_sideload($file_array, array('test_form' => false), $time);
    if (isset($file['error'])) {
        return new WP_Error('upload_error', $file['error']);
    }
    $url = $file['url'];
    $type = $file['type'];
    $file = $file['file'];
    $title = preg_replace('/\\.[^.]+$/', '', basename($file));
    $parent = (int) absint($post_id) > 0 ? absint($post_id) : 0;
    $attachment = array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $parent, 'post_title' => $title, 'post_content' => '');
    $id = wp_insert_attachment($attachment, $file, $parent);
    if (!is_wp_error($id)) {
        require_once ABSPATH . 'wp-admin/includes/image.php';
        $data = wp_generate_attachment_metadata($id, $file);
        wp_update_attachment_metadata($id, $data);
    }
    return $id;
}
开发者ID:developmentDM2,项目名称:Whohaha,代码行数:39,代码来源:snippets.php


示例4: do_save

 public function do_save($return_val, $params, $metas, $module)
 {
     if (empty($params['attachment_url'])) {
         return false;
     }
     $bits = file_get_contents($params['attachment_url']);
     $filename = $this->faker->uuid() . '.jpg';
     $upload = wp_upload_bits($filename, null, $bits);
     $params['guid'] = $upload['url'];
     $params['post_mime_type'] = 'image/jpeg';
     // Insert the attachment.
     $attach_id = wp_insert_attachment($params, $upload['file'], 0);
     if (!is_numeric($attach_id)) {
         return false;
     }
     if (!function_exists('wp_generate_attachment_metadata')) {
         // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
         require_once ABSPATH . 'wp-admin/includes/image.php';
     }
     // Generate the metadata for the attachment, and update the database record.
     $metas['_wp_attachment_metadata'] = wp_generate_attachment_metadata($attach_id, $upload['file']);
     foreach ($metas as $key => $value) {
         update_post_meta($attach_id, $key, $value);
     }
     return $attach_id;
 }
开发者ID:juanfra,项目名称:fakerpress,代码行数:26,代码来源:attachment.php


示例5: dswoddil_site_icon_upload

/**
 * Upload theme default site icon
 *
 * @return void
 */
function dswoddil_site_icon_upload()
{
    $image_name = 'site-icon';
    if (!function_exists('wp_update_attachment_metadata')) {
        require_once ABSPATH . 'wp-admin/includes/image.php';
    }
    if (!dswoddil_get_attachment_by_post_name('dswoddil-' . $image_name)) {
        $site_icon = get_template_directory() . '/img/' . $image_name . '.png';
        // create $file array with the indexes show below
        $file['name'] = $site_icon;
        $file['type'] = 'image/png';
        // get image size
        $file['size'] = filesize($file['name']);
        $file['tmp_name'] = $image_name . '.png';
        $file['error'] = 1;
        $file_content = file_get_contents($site_icon);
        $upload_image = wp_upload_bits($file['tmp_name'], null, $file_content);
        // Check the type of tile. We'll use this as the 'post_mime_type'.
        $filetype = wp_check_filetype(basename($upload_image['file']), null);
        $attachment = array('guid' => $upload_image['file'], 'post_mime_type' => $filetype['type'], 'post_title' => 'dswoddil-' . $image_name, 'post_content' => '', 'post_status' => 'inherit');
        //insert wordpress attachment of uploaded image to get attachmen ID
        $attachment_id = wp_insert_attachment($attachment, $upload_image['file']);
        //generate attachment thumbnail
        wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $upload_image['file']));
        add_post_meta($attachment_id, '_wp_attachment_context', $image_name);
    }
}
开发者ID:ertik,项目名称:dsw-oddil,代码行数:32,代码来源:site-icon.php


示例6: import

 public function import($attachment)
 {
     $saved_image = $this->_return_saved_image($attachment);
     if ($saved_image) {
         return $saved_image;
     }
     // Extract the file name and extension from the url
     $filename = basename($attachment['url']);
     if (function_exists('file_get_contents')) {
         $options = ['http' => ['user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux i686 on x86_64; rv:49.0) Gecko/20100101 Firefox/49.0']];
         $context = stream_context_create($options);
         $file_content = file_get_contents($attachment['url'], false, $context);
     } else {
         $file_content = wp_remote_retrieve_body(wp_safe_remote_get($attachment['url']));
     }
     if (empty($file_content)) {
         return false;
     }
     $upload = wp_upload_bits($filename, null, $file_content);
     $post = ['post_title' => $filename, 'guid' => $upload['url']];
     $info = wp_check_filetype($upload['file']);
     if ($info) {
         $post['post_mime_type'] = $info['type'];
     } else {
         // For now just return the origin attachment
         return $attachment;
         //return new \WP_Error( 'attachment_processing_error', __( 'Invalid file type', 'elementor' ) );
     }
     $post_id = wp_insert_attachment($post, $upload['file']);
     wp_update_attachment_metadata($post_id, wp_generate_attachment_metadata($post_id, $upload['file']));
     update_post_meta($post_id, '_elementor_source_image_hash', $this->_get_hash_image($attachment['url']));
     $new_attachment = ['id' => $post_id, 'url' => $upload['url']];
     $this->_replace_image_ids[$attachment['id']] = $new_attachment;
     return $new_attachment;
 }
开发者ID:pojome,项目名称:elementor,代码行数:35,代码来源:class-import-images.php


示例7: add_image

 static function add_image($image_url)
 {
     if (empty($image_url)) {
         return FALSE;
     }
     // Add Featured Image to Post
     $upload_dir = wp_upload_dir();
     // Set upload folder
     $image_data = file_get_contents($image_url);
     // Get image data
     $filename = basename($image_url);
     // Create image file name
     // Check folder permission and define file location
     if (wp_mkdir_p($upload_dir['path'])) {
         $file = $upload_dir['path'] . '/' . $filename;
     } else {
         $file = $upload_dir['basedir'] . '/' . $filename;
     }
     // Create the image  file on the server
     file_put_contents($file, $image_data);
     // Check image file type
     $wp_filetype = wp_check_filetype($filename, NULL);
     // Set attachment data
     $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($filename), 'post_content' => '', 'post_status' => 'inherit');
     // Create the attachment
     $attach_id = wp_insert_attachment($attachment, $file);
     // Include image.php
     require_once ABSPATH . 'wp-admin/includes/image.php';
     // Define attachment metadata
     $attach_data = wp_generate_attachment_metadata($attach_id, $file);
     // Assign metadata to attachment
     wp_update_attachment_metadata($attach_id, $attach_data);
     return $attach_id;
 }
开发者ID:gpsidhuu,项目名称:alphaReputation,代码行数:34,代码来源:xsUTL.php


示例8: process_attachment

function process_attachment($post, $url)
{
    // if the URL is absolute, but does not contain address, then upload it assuming base_site_url
    //if ( preg_match( '|^/[\w\W]+$|', $url ) )
    //	$url = rtrim( $this->base_url, '/' ) . $url;
    global $url_remap;
    $upload = fetch_remote_file($url, $post);
    if (is_wp_error($upload)) {
        return $upload;
    }
    if ($info = wp_check_filetype($upload['file'])) {
        $post['post_mime_type'] = $info['type'];
    } else {
        return new WP_Error('attachment_processing_error', __('Invalid file type', 'wordpress-importer'));
    }
    $post['guid'] = $upload['url'];
    // as per wp-admin/includes/upload.php
    $post_id = wp_insert_attachment($post, $upload['file']);
    wp_update_attachment_metadata($post_id, wp_generate_attachment_metadata($post_id, $upload['file']));
    // remap resized image URLs, works by stripping the extension and remapping the URL stub.
    if (preg_match('!^image/!', $info['type'])) {
        $parts = pathinfo($url);
        $name = basename($parts['basename'], ".{$parts['extension']}");
        // PATHINFO_FILENAME in PHP 5.2
        $parts_new = pathinfo($upload['url']);
        $name_new = basename($parts_new['basename'], ".{$parts_new['extension']}");
        $url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
    }
    return $post_id;
}
开发者ID:centroculturalsp,项目名称:cultural,代码行数:30,代码来源:importimages.php


示例9: wpua_avatar_upload

 public function wpua_avatar_upload($file)
 {
     $filetype = wp_check_filetype($file->name);
     $media_upload = array();
     $media_upload['file'] = array('name' => $file->name, 'type' => $filetype['type'], 'tmp_name' => $file->path, 'error' => 0, 'size' => filesize($file->path));
     $media_file = wp_handle_upload($media_upload['file'], array('test_form' => false, 'test_upload' => false, 'action' => 'custom_action'));
     if ($media_file['file']) {
         $url = $media_file['url'];
         $filepath = $media_file['file'];
         if ($image_meta = @wp_read_image_metadata($filepath)) {
             if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) {
                 $title = $image_meta['title'];
             }
         }
         $attachment = array('guid' => $url, 'post_mime_type' => $filetype['type'], 'post_title' => $title);
         $attachment_id = wp_insert_attachment($attachment, $filepath);
         if (!is_wp_error($attachment_id)) {
             $this->delete_attachment_by_user($this->user_id);
             wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $filepath));
             update_post_meta($attachment_id, '_wp_attachment_wp_user_avatar', $this->user_id);
             $arr = wp_get_attachment_image_src($attachment_id, 'full');
             $this->avatar_url = $arr[0];
             $this->avatar_filename = basename($filepath);
             $this->resource = $attachment_id;
             $saved = $this->save();
             if (!$saved) {
                 $this->delete_attachment($attachment_id);
                 return $saved;
             }
             return $saved;
         }
     } else {
         return WP_Error('file_upload_problem', __("Media avatar could't uploading please check you have right permission for uploads folder.", 'wp-user-avatar-pro'));
     }
 }
开发者ID:andyUA,项目名称:kabmin-new,代码行数:35,代码来源:class-wpua-media-storage.php


示例10: atp_plupload_action

 function atp_plupload_action()
 {
     // check ajax noonce
     $imgid = $_POST["imgid"];
     check_ajax_referer($imgid . 'pluploadan');
     $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
     // handle file upload
     $filename = $_FILES[$imgid . 'async-upload']['name'];
     $status = wp_handle_upload($_FILES[$imgid . 'async-upload'], array('test_form' => true, 'action' => 'plupload_action'));
     if (!isset($status['file'])) {
         continue;
     }
     $file_name = $status['file'];
     $name_parts = pathinfo($file_name);
     $name = trim(substr($filename, 0, -(1 + strlen($name_parts['extension']))));
     $attachment = array('post_mime_type' => $status['type'], 'guid' => $status['url'], 'post_parent' => $post_id, 'post_title' => $name, 'post_content' => '');
     $id = wp_insert_attachment($attachment, $file_name, $post_id);
     if (!is_wp_error($id)) {
         wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file_name));
         $new[] = $id;
     }
     // send the uploaded file url in response
     $upload_path = wp_get_attachment_url($id, true);
     if (preg_match_all('/[^\\?]+\\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $upload_path, $matches)) {
         $image_attributes = wp_get_attachment_image_src($id, 'thumbnail');
         // returns an array
         $uplaod_url = $image_attributes[0];
     } else {
         $uplaod_url = $upload_path;
     }
     $imagetest = array('url' => $status['url'], 'link' => get_edit_post_link($id), 'audioid' => $id, 'name' => $name, 'img' => $uplaod_url);
     echo json_encode($imagetest);
     //print_r ($imagetest);
     exit;
 }
开发者ID:pryspry,项目名称:MusicPlay,代码行数:35,代码来源:meta-generator.php


示例11: create_png

function create_png()
{
    $attachment = array('guid' => 'http://example.org/wp-content/uploads/2014/12/logo.png', 'post_mime_type' => 'image/png', 'post_title' => 'PNG', 'post_status' => 'inherit');
    $img = wp_insert_attachment($attachment, 'logo.png');
    wp_update_attachment_metadata($img, array('width' => 2448, 'height' => 3264, 'file' => '2014/12/logo.png', 'sizes' => array('thumbnail' => array('file' => 'logo-480x640.png', 'width' => 480, 'height' => 640), 'medium' => array('file' => 'logo-600x800.png', 'width' => 600, 'height' => 800), 'large' => array('file' => 'logo-1024x1365.png', 'width' => 1024, 'height' => 1365))));
    return $img;
}
开发者ID:jgadbois,项目名称:responsify-wp,代码行数:7,代码来源:create_attachment.php


示例12: cptImages_savefile

function cptImages_savefile($file, $name, $slug = false, $parent_post_id = 0, $content = '', $attachData = array())
{
    if (empty($file)) {
        return false;
    }
    if (!function_exists('wp_handle_upload')) {
        require_once ABSPATH . 'wp-admin/includes/file.php';
    }
    $upload_overrides = array('test_form' => false);
    $movefile = wp_handle_upload($file, $upload_overrides);
    $name = 'wp-cpt-images image for ' . $name;
    if ($movefile && !isset($movefile['error'])) {
        $wp_filetype = $movefile['type'];
        $filename = $movefile['file'];
        $wp_upload_dir = wp_upload_dir();
        $attachment = array('guid' => $wp_upload_dir['url'] . '/' . basename($filename), 'post_mime_type' => $wp_filetype, 'post_title' => $name, 'post_content' => $content, 'post_status' => 'inherit');
        $attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
        require_once ABSPATH . 'wp-admin/includes/image.php';
        add_post_meta($attach_id, 'wp-cpt-image-attachment', $slug);
        foreach ($attachData as $key => $val) {
            add_post_meta($attach_id, $key, $val);
        }
        update_cpt_connection_link($slug, $attach_id);
    }
    return $attach_id;
}
开发者ID:social-ink,项目名称:wp-cpt-images,代码行数:26,代码来源:core.php


示例13: do_save

 public function do_save($return_val, $data, $module)
 {
     if (empty($data['attachment_url'])) {
         return false;
     }
     $bits = file_get_contents($data['attachment_url']);
     $filename = $this->faker->uuid() . '.jpg';
     $upload = wp_upload_bits($filename, null, $bits);
     $data['guid'] = $upload['url'];
     $data['post_mime_type'] = 'image/jpeg';
     // Insert the attachment.
     $attach_id = wp_insert_attachment($data, $upload['file'], 0);
     if (!is_numeric($attach_id)) {
         return false;
     }
     if (!function_exists('wp_generate_attachment_metadata')) {
         // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
         require_once ABSPATH . 'wp-admin/includes/image.php';
     }
     // Generate the metadata for the attachment, and update the database record.
     update_post_meta($attach_id, '_wp_attachment_metadata', wp_generate_attachment_metadata($attach_id, $upload['file']));
     // Flag the Object as FakerPress
     update_post_meta($attach_id, self::$flag, 1);
     return $attach_id;
 }
开发者ID:KiyaDigital,项目名称:tvarchive-canonical_wordpress,代码行数:25,代码来源:attachment.php


示例14: nev_admin_upload_file

function nev_admin_upload_file()
{
    if (!function_exists('wp_handle_upload')) {
        require_once ABSPATH . 'wp_admin/includes/file.php';
    }
    if (empty($_FILES['file_path'])) {
        return;
    }
    $uploadedFile = $_FILES['file_path'];
    $uploaded_overrides = array('test_form' => false);
    $moveFile = wp_handle_upload($uploadedFile, $uploaded_overrides);
    if (!empty($moveFile['error'])) {
        echo $moveFile['error'];
        return;
    }
    if ($moveFile) {
        $wp_filetype = $moveFile['type'];
        $filename = $moveFile['file'];
        $wp_upload_dir = wp_upload_dir();
        $attachment = array('guid' => $wp_upload_dir['url'] . '/' . basename($filename), 'post_mime_type' => $wp_filetype, 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit');
        $attach_id = wp_insert_attachment($attachment, $filename);
        $file_path = get_attached_file($attach_id);
        update_option("nev-file_path", $file_path);
        echo "File uploaded to " . $file_path;
    } else {
        echo "Failed!";
    }
}
开发者ID:pmarki,项目名称:next_events,代码行数:28,代码来源:nev-admin.php


示例15: insert_attachment

 /**
  * Helper function: insert an attachment to test properties of.
  *
  * @param int $parent_post_id
  * @param str path to image to use
  * @param array $post_fields Fields, in the format to be sent to `wp_insert_post()`
  * @return int Post ID of inserted attachment
  */
 private function insert_attachment($parent_post_id = 0, $image = null, $post_fields = array())
 {
     $filename = rand_str() . '.jpg';
     $contents = rand_str();
     if ($image) {
         // @codingStandardsIgnoreStart
         $filename = basename($image);
         $contents = file_get_contents($image);
         // @codingStandardsIgnoreEnd
     }
     $upload = wp_upload_bits($filename, null, $contents);
     $this->assertTrue(empty($upload['error']));
     $type = '';
     if (!empty($upload['type'])) {
         $type = $upload['type'];
     } else {
         $mime = wp_check_filetype($upload['file']);
         if ($mime) {
             $type = $mime['type'];
         }
     }
     $attachment = wp_parse_args($post_fields, array('post_title' => basename($upload['file']), 'post_content' => 'Test Attachment', 'post_type' => 'attachment', 'post_parent' => $parent_post_id, 'post_mime_type' => $type, 'guid' => $upload['url']));
     // Save the data
     $id = wp_insert_attachment($attachment, $upload['file'], $parent_post_id);
     wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file']));
     return $id;
 }
开发者ID:davisshaver,项目名称:shortcake-bakery,代码行数:35,代码来源:test-image-comparison.php


示例16: _process_downloaded_image

 /**
  * Process downloaded image
  *
  * @param string   $tmp_file
  * @param int|bool $media_author
  * @param string   $media_date
  *
  * @return bool
  */
 private function _process_downloaded_image($tmp_file, $media_author, $media_date)
 {
     if ('image/jpeg' !== ($mime = mime_content_type($tmp_file))) {
         WP_CLI::warning('Invalid image type.');
         return false;
     }
     $info = pathinfo($tmp_file);
     $name = isset($info['filename']) ? $info['filename'] : 'unsplash';
     $file_array = array('name' => $name . '.jpeg', 'type' => $mime, 'tmp_name' => $tmp_file, 'error' => 0, 'size' => filesize($tmp_file));
     if ('random' === $media_date) {
         $timestamp = current_time('timestamp') - mt_rand(0, 315576000);
         // In last 10 years
         $media_date = gmdate('Y-m-d H:i:s', $timestamp);
     }
     $file = wp_handle_sideload($file_array, array('test_form' => false), $media_date);
     if (isset($file['error'])) {
         WP_CLI::warning('Error uploading file.');
         return false;
     }
     $attachment = array('post_mime_type' => $file['type'], 'guid' => $file['url'], 'post_title' => $name, 'post_author' => $media_author, 'post_date' => $media_date);
     // Save the attachment metadata
     $id = wp_insert_attachment($attachment, $file['file']);
     if (is_wp_error($id)) {
         WP_CLI::warning('Error creating attachment.');
         return false;
     }
     wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file['file']));
 }
开发者ID:A5hleyRich,项目名称:wp-cli-unsplash-command,代码行数:37,代码来源:class-wp-cli-unsplash-command.php


示例17: _make_attachment

	function _make_attachment($upload, $parent_post_id=-1) {

		$type = '';
		if ( !empty($upload['type']) ) {
			$type = $upload['type'];
		} else {
			$mime = wp_check_filetype( $upload['file'] );
			if ($mime)
				$type = $mime['type'];
		}

		$attachment = array(
			'post_title' => basename( $upload['file'] ),
			'post_content' => '',
			'post_type' => 'attachment',
			'post_parent' => $parent_post_id,
			'post_mime_type' => $type,
			'guid' => $upload[ 'url' ],
		);

		// Save the data
		$id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );

		return $this->ids[] = $id;

	}
开发者ID:staylor,项目名称:develop.svn.wordpress.org,代码行数:27,代码来源:attachments.php


示例18: wpmp_add_product

function wpmp_add_product()
{
    if (wp_verify_nonce($_POST['__product_wpmp'], 'wpmp-product') && $_POST['task'] == '') {
        if ($_POST['post_type'] == "wpmarketplace") {
            global $current_user, $wpdb;
            get_currentuserinfo();
            $my_post = array('post_title' => $_POST['product']['post_title'], 'post_content' => $_POST['product']['post_content'], 'post_excerpt' => $_POST['product']['post_excerpt'], 'post_status' => "draft", 'post_author' => $current_user->ID, 'post_type' => "wpmarketplace");
            //echo $_POST['id'];
            if ($_POST['id']) {
                //update post
                $my_post['ID'] = $_REQUEST['id'];
                wp_update_post($my_post);
                $postid = $_REQUEST['id'];
            } else {
                //insert post
                $postid = wp_insert_post($my_post);
            }
            update_post_meta($postid, "wpmp_list_opts", $_POST['wpmp_list']);
            foreach ($_POST['wpmp_list'] as $k => $v) {
                update_post_meta($postid, $k, $v);
            }
            //echo $_POST['wpmp_list']['fimage'];
            if ($_POST['wpmp_list']['fimage']) {
                $wp_filetype = wp_check_filetype(basename($_POST['wpmp_list']['fimage']), null);
                $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($_POST['wpmp_list']['fimage'])), 'post_content' => '', 'guid' => $_POST['wpmp_list']['fimage'], 'post_status' => 'inherit');
                $attach_id = wp_insert_attachment($attachment, $_POST['wpmp_list']['fimage'], $postid);
                set_post_thumbnail($postid, $attach_id);
            }
        }
        //echo $_SERVER['HTTP_REFERER'];
        header("Location: " . $_SERVER['HTTP_REFERER']);
        die;
    }
}
开发者ID:tvolmari,项目名称:hammydowns,代码行数:34,代码来源:functions_old.php


示例19: fileupload_trigger_check

 function fileupload_trigger_check()
 {
     if (intval(get_query_var('postform_fileupload')) == 1) {
         if (!(get_option('bbp_5o1_toolbar_allow_image_uploads') && (is_user_logged_in() || get_option('bbp_5o1_toolbar_allow_anonymous_image_uploads')))) {
             echo htmlspecialchars(json_encode(array("error" => __("You are not permitted to upload images.", 'bbp_5o1_toolbar'))), ENT_NOQUOTES);
             exit;
         }
         require_once dirname(__FILE__) . '/includes/fileuploader.php';
         // list of valid extensions, ex. array("jpeg", "xml", "bmp")
         $allowedExtensions = array('jpg', 'jpeg', 'png', 'gif');
         // Because using Extensions only is very bad.
         $allowedMimes = array(IMAGETYPE_JPEG, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);
         // max file size in bytes
         $sizeLimit = bbp_5o1_images_panel::return_bytes(min(array(ini_get('post_max_size'), ini_get('upload_max_filesize'))));
         $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
         $directory = wp_upload_dir();
         $result = $uploader->handleUpload(trailingslashit($directory['path']));
         $mime = exif_imagetype($result['file']);
         if (!$mime || !in_array($mime, $allowedMimes)) {
             $deleted = unlink($result['file']);
             echo htmlspecialchars(json_encode(array("error" => __("Disallowed file type.", 'bbp_5o1_toolbar'))), ENT_NOQUOTES);
             exit;
         }
         // Construct the attachment array
         $attachment = array('post_mime_type' => $mime ? image_type_to_mime_type($mime) : '', 'guid' => trailingslashit($directory['url']) . $result['filename'], 'post_parent' => 0, 'post_title' => $result['name'], 'post_content' => 'Image uploaded for a forum topic or reply.');
         // Save the data
         $id = wp_insert_attachment($attachment, $result['file'], 0);
         $result['id'] = $id;
         $result['attachment'] = $attachment;
         $result = array("success" => true, "file" => $attachment['guid']);
         echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
         exit;
     }
 }
开发者ID:sayf75,项目名称:bbPress-Post-Toolbar,代码行数:34,代码来源:toolbar-images-panel.php


示例20: file_import

 function file_import()
 {
     if (wp_verify_nonce($_POST['nonce'], "molie_admin_file")) {
         $post = get_post($_POST['course_post']);
         $file_system = get_post_meta($post->ID, "courseFileSystem", true);
         $file_contents = file_get_contents($_POST['url']);
         file_put_contents($file_system[$_POST['folder']]['actual_path'] . "/" . $_POST['filename'], $file_contents);
         $filetype = wp_check_filetype(basename($file_system[$_POST['folder']]['actual_path'] . "/" . $_POST['filename']), null);
         echo $file_system[$_POST['folder']]['actual_path'] . "/" . $_POST['filename'] . "<br />";
         $attachment = array('guid' => $file_system[$_POST['folder']]['actual_path'] . "/" . $_POST['filename'], 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($_POST['filename'])), 'post_content' => '', 'post_status' => 'inherit');
         $attach_id = wp_insert_attachment($attachment, $file_system[$_POST['folder']]['actual_path'] . "/" . $_POST['filename']);
         $data = wp_generate_attachment_metadata($attach_id, $file_system[$_POST['folder']]['actual_path'] . "/" . $_POST['filename']);
         update_post_meta($attach_id, "_wp_attachment_metadata", $data, true);
         update_post_meta($post->ID, "molie_file_" . $_POST['item'], $attach_id);
         update_post_meta($attach_id, "CanvasCourse", get_post_meta($post->ID, "courseID", true));
         update_post_meta($attach_id, "CanvasFileVerifier", $_POST['verifier']);
         update_post_meta($attach_id, "CanvasCourseIDFileID", get_post_meta($post->ID, "courseID", true) . "," . $_POST['item']);
         $file_url = get_post_meta($post->ID, "courseURL", true) . "/courses/" . get_post_meta($post->ID, "courseID", true) . "/files/" . $_POST['item'] . "/download?verifier=" . $_POST['verifier'];
         update_post_meta($attach_id, "CanvasFileURL", $file_url);
         echo __("File Downloaded");
     } else {
         echo "Nonce failed";
     }
     wp_die();
 }
开发者ID:pgogy,项目名称:WordPress-MOLIE,代码行数:25,代码来源:molie-file-ajax.php



注:本文中的wp_insert_attachment函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP wp_insert_category函数代码示例发布时间:2022-05-23
下一篇:
PHP wp_initialize_the_theme_message函数代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap