@Dodok я смотрел
Как реализовать мультизагрузку фотографий на wordpress - дождаться полной загрузки - и только потом сохранить запись?
-
Здравствуйте!
В системе Wordpress можно загружать фотографии.
Вопрос вот в чем - у меня есть папка с фотографиями на сервере.
Хотелось бы чтобы можно было следующее:- Начать загрузку фото.
- Выбрать все фото из папки.
- Дождаться, пока все фото загрузятся (не знаю по какому принципу это можно отфильтровать вообще).
- И только после этого сохранить запись.
-
ето делаеться простыми плагинами ... или при длительном действии в консоли запусти скрипт... без проблем грузиться 100 000 постов с 10 - 20 примстегнутыми фотками
вот примерный пример
а вообще решений много .... ето один из примеров --- разовой загрузки..
есть готовые плагины --- импорт експрот --- с хорошим функционалом...<?php
/**- WordPress Cron Implementation for hosts, which do not offer CRON or for which
- the user has not set up a CRON job pointing to this file.
- The HTTP request to this file will not slow down the visitor who happens to
- visit when the cron job is needed to run.
- @package WordPress
*/
ignore_user_abort(true);
if (!empty($_POST) || defined('DOING_AJAX') || defined('DOING_CRON'))
die();/**
- Tell WordPress we are doing the CRON task.
- @var bool
*/
define('DOING_CRON', true);
if (!defined('ABSPATH')) {
/** Set up WordPress environment */
require_once (dirname(file) . '/wp-load.php');
}
require_once (ABSPATH . "wp-admin" . '/includes/image.php');
require_once (ABSPATH . "wp-admin" . '/includes/file.php');
require_once (ABSPATH . "wp-admin" . '/includes/media.php');
// получаем список директорий для загрузки
$dir1 = ABSPATH.'00000';
$ddd[] = ' '.get_option('home').' ';$type = array(
1 => 'jpg',
2 => 'jpeg',
3 => 'png',
4 => 'gif'); //store all the image extension types in array$files1 = scandir($dir1);
$file_dir = array();
$dir = opendir($dir1);
while ($file = readdir($dir)) {
if (is_dir('00000/' . $file) && $file != '.' && $file != '..') {$file_dir[] = $dir1 . '/' . $file; }}
$sql = "SELECT ID FROM wp_posts WHERE post_type='product' ";
$temp = $wpdb->get_results($sql);
$i = 0;
// узнаем и удаляем медиа файлы
foreach ($temp as $tmp) {
wp_set_object_terms($tmp->ID, array('is_show'), 'product_tag');
$fixed_gall = get_post_meta($tmp->ID, 'fixed_gall', true);
if ($fixed_gall != 'YES') {
// if (true) {
$thumbnail_id = get_post_meta($tmp->ID, '_thumbnail_id', true);if ($thumbnail_id) { wp_delete_attachment($thumbnail_id); update_post_meta($tmp->ID, '_thumbnail_id', ''); } $thumbnail_gallery = get_post_meta($tmp->ID, '_product_image_gallery', true); if ($thumbnail_gallery) { $thumbnail_gallery = explode(',', $thumbnail_gallery); // print_r($thumbnail_gallery); foreach ($thumbnail_gallery as $tmp_gallery) { wp_delete_attachment($tmp_gallery); } update_post_meta($tmp->ID, '_product_image_gallery', ''); } // подписываем из какой директории загрузить update_post_meta($tmp->ID, 'file_dir', $file_dir[$i]); } $i++;}
// берем папку// die; ето просто чистим
$i = 0;
foreach ($temp as $tmp) {
$fixed_gall = get_post_meta($tmp->ID, 'fixed_gall', true);
if ($fixed_gall != 'YES') {
// if (true) {
$file_dir = get_post_meta($tmp->ID, 'file_dir', true);$files1 = scandir($file_dir); $url1 = explode('00000/', $file_dir); $media_id = array(); foreach ($files1 as $files1_tmp) { $ext = explode(".", $files1_tmp); if ((in_array($ext[1], $type))) //check image extension not in the array $type { $url = get_option('home').'/00000/' . $url1[1] . '/' . $files1_tmp; // загрузили пачку в массив $media_id[] = upload_to_media_abg($url); } } // установили 1 картинку update_post_meta($tmp->ID, '_thumbnail_id', $media_id[0]); // засунули в строчку $thumbnail_gallery = implode(',', $media_id); update_post_meta($tmp->ID, '_product_image_gallery', $thumbnail_gallery); // delete_post_meta($tmp->ID, 'file_dir'); update_post_meta($tmp->ID, 'fixed_gall', 'YES'); } $i++; echo $i . ' ' . $tmp->ID . "\n";}
//upload_to_media_abg('');
function upload_to_media_abg($url = '')
{$tmp = download_url($url); if (is_wp_error($tmp)) { // download failed, handle error } $post_id = 0; $desc = ""; $file_array = array(); // Set variables for storage // fix file filename for query strings preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $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'] = ''; } // do the validation and storage stuff return media_handle_sideload($file_array, $post_id, $desc);}
die();