Manşet ekleme scripti

Genel Forum
Cevapla
muratca61
Site Admin
Mesajlar: 35899
Kayıt: Cmt Ara 21, 2024 7:56 am

Manşet ekleme scripti

Mesaj gönderen muratca61 »

bu script kategori ekliyor etiket ekliyor ama yayımlanma tarihi sorunlu

Kod:Tümünü seç

<?php
/*
Plugin Name: Toplu Resimli Yazı Yükleyici
Description: Çoklu resim yükleme, otomatik yazı oluşturma ve tarih çıkarma
Version: 1.4
Author: Sizin Adınız
*/

// Admin menüsüne ekleme
function image_bulk_uploader_menu() {
    add_menu_page(
        'Toplu Resim Yükle',
        'Resimli Yazı Ekle',
        'manage_options',
        'image-bulk-uploader',
        'uploader_admin_page',
        'dashicons-images-alt2',
        6
    );
}
add_action('admin_menu', 'image_bulk_uploader_menu');

// Admin arayüzü
function uploader_admin_page() {
    ?>
    <div class="wrap">
        <h1>Toplu Resim Yükleme</h1>
        <form method="post" enctype="multipart/form-data">
            <?php wp_nonce_field('image_upload_action', 'image_upload_nonce'); ?>
            <input type="file" name="image_files[]" multiple accept="image/*">
            <p class="description">• Dosya adı formatı: YYYYAAgg_ornek.jpg (Örn: 20000517_tercuman.jpg)<br>
               • Otomatik "Tarih" ve "Manşetler" kategorilerine eklenecek</p>
            <?php submit_button('Yükle ve Yazıları Oluştur'); ?>
        </form>
        <?php
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            handle_image_upload();
        }
        ?>
    </div>
    <?php
}

// Kategori kontrolü
function check_required_categories() {
    $categories = ['tarih', 'manşetler'];
    foreach ($categories as $category) {
        if (!term_exists($category, 'category')) {
            wp_insert_term(
                ucfirst($category),
                'category',
                ['slug' => sanitize_title($category)]
            );
        }
    }
}

// Dosya işleme fonksiyonu
function handle_image_upload() {
    check_required_categories();
    
    if (!current_user_can('manage_options')) {
        wp_die('Yetkiniz yok!');
    }
    
    if (!wp_verify_nonce($_POST['image_upload_nonce'], 'image_upload_action')) {
        wp_die('Güvenlik hatası!');
    }

    if (empty($_FILES['image_files'])) {
        echo '<div class="error"><p>Lütfen dosya seçin!</p></div>';
        return;
    }

    $files = $_FILES['image_files'];
    $uploaded = 0;

    foreach ($files['name'] as $key => $value) {
        if ($files['name'][$key]) {
            if (process_single_file($files, $key)) $uploaded++;
        }
    }

    echo "<div class='updated'><p>Başarıyla tamamlandı: $uploaded yazı oluşturuldu</p></div>";
}

// Tarih çıkarımı (dosya adı formatı: 19610107_Tercüman-gazetesi.jpg)
function extract_date_from_filename($filename) {
    // 19610107 formatındaki tarihi çıkarma
    preg_match('/\b(\d{8})\b/', $filename, $matches);
    
    if (!empty($matches[1])) {
        $date_str = $matches[1];
        try {
            // 19610107 formatında tarihi dönüştürme
            $date = DateTime::createFromFormat('Ymd', $date_str);
            if ($date && checkdate(
                (int)$date->format('m'),
                (int)$date->format('d'),
                (int)$date->format('Y')
            )) {
                return $date->format('Y-m-d 12:00:00'); // Yayınlama tarihi olarak kullanacağız
            }
        } catch (Exception $e) {}
    }
    return current_time('mysql');
}

// Yazı oluşturma ve başlığa tarih ekleme
function create_image_post($title, $date, $attachment_id) {
    // Dosya adı formatından tarihi temizle ve başlığa tarih ekle
    $clean_title = preg_replace('/\b\d{8}\b/', '', $title); // 19610107 kısmını başlıktan çıkar
    $clean_title = sanitize_text_field(str_replace(['_', '-'], ' ', $clean_title));
    
    // Başlığa tarihi ekle (örneğin: 07.01.1961 - Tercüman gazetesinin yazısı)
    $formatted_date = date('d.m.Y', strtotime($date)); // Yayınlanma tarihi formatı: d.m.Y
    $final_title = $formatted_date . ' - ' . $clean_title;

    $new_post = [
        'post_title' => $final_title,  // Başlığa tarihi ekledik
        'post_content' => '',
        'post_status' => 'publish',
        'post_type' => 'post',
        'post_date' => $date,           // Yayınlama tarihi olarak çıkardığımız tarihi kullandık
        'post_date_gmt' => get_gmt_from_date($date),
        'meta_input' => [
            '_thumbnail_id' => $attachment_id
        ]
    ];
    
    $post_id = wp_insert_post($new_post, true);
    
    if (!is_wp_error($post_id)) {
        // Kategorileri ekle
        $categories = [];
        foreach (['tarih', 'manşetler'] as $category) {
            $term = get_term_by('slug', $category, 'category');
            if ($term) $categories[] = $term->term_id;
        }
        wp_set_post_categories($post_id, $categories);

        // Etiketleri ekle
        wp_set_post_terms($post_id, ['tarih', 'manşet'], 'post_tag', true);
        
        return true;
    }
    return false;
}

// Dosya işleme
function process_single_file($files, $key) {
    $file = [
        'name' => sanitize_file_name($files['name'][$key]),
        'type' => $files['type'][$key],
        'tmp_name' => $files['tmp_name'][$key],
        'error' => $files['error'][$key],
        'size' => $files['size'][$key]
    ];

    $filename = pathinfo($file['name'], PATHINFO_FILENAME);
    $date = extract_date_from_filename($filename);

    $upload = wp_upload_bits($file['name'], null, file_get_contents($file['tmp_name']));
    if ($upload['error']) return false;

    $attachment_id = create_attachment($upload['file'], $date);
    if (!$attachment_id) return false;

    return create_image_post($filename, $date, $attachment_id);
}

// Medya dosyası oluşturma
function create_attachment($file, $date) {
    $attachment = [
        'post_mime_type' => mime_content_type($file),
        'post_title' => pathinfo($file, PATHINFO_FILENAME),
        'post_content' => '',
        'post_status' => 'inherit',
        'post_date' => $date,
        'post_date_gmt' => get_gmt_from_date($date)
    ];

    $attach_id = wp_insert_attachment($attachment, $file);
    if (!is_wp_error($attach_id)) {
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attach_data = wp_generate_attachment_metadata($attach_id, $file);
        wp_update_attachment_metadata($attach_id, $attach_data);
        return $attach_id;
    }
    return false;
}
muratca61
Site Admin
Mesajlar: 35899
Kayıt: Cmt Ara 21, 2024 7:56 am

Re: Manşet ekleme scripti

Mesaj gönderen muratca61 »

bu script cgpt tarafından birleştirildi. tarihe göre yayımlanma tamam ama yayımlanma tarihinin başlığa atanması ve etiketleme eksik

Kod:Tümünü seç

<?php
/*
Plugin Name: Toplu Resimli Yazı Yükleyici
Description: Çoklu resim yükleme, otomatik yazı oluşturma ve tarih çıkarma
Version: 1.5
Author: Sizin Adınız
*/

// Admin menüsüne ekleme
function image_bulk_uploader_menu() {
    add_menu_page(
        'Toplu Resim Yükle',
        'Resimli Yazı Ekle',
        'manage_options',
        'image-bulk-uploader',
        'uploader_admin_page',
        'dashicons-images-alt2',
        6
    );
}
add_action('admin_menu', 'image_bulk_uploader_menu');

// Admin arayüzü
function uploader_admin_page() {
    ?>
    <div class="wrap">
        <h1>Toplu Resim Yükleme</h1>
        <form method="post" enctype="multipart/form-data">
            <?php wp_nonce_field('image_upload_action', 'image_upload_nonce'); ?>
            <input type="file" name="image_files[]" multiple accept="image/*">
            <p class="description">• Dosya adı formatı: YYYYAAgg_ornek.jpg (Örn: 19820325_tercuman.jpg)<br>
               • İlk 8 karakter tarih olmalı (19820325 → 25.03.1982)</p>
            <?php submit_button('Yükle ve Yazıları Oluştur'); ?>
        </form>
        <?php
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            handle_image_upload();
        }
        ?>
    </div>
    <?php
}

// Gerekli kategorileri kontrol et ve oluştur
function check_required_categories() {
    $categories = ['tarih', 'manşetler'];
    foreach ($categories as $category) {
        if (!term_exists($category, 'category')) {
            wp_insert_term(
                ucfirst($category),
                'category',
                ['slug' => sanitize_title($category)]
            );
        }
    }
}

// Dosya işleme fonksiyonu
function handle_image_upload() {
    check_required_categories();
    
    if (!current_user_can('manage_options')) {
        wp_die('Yetkiniz yok!');
    }
    
    if (!wp_verify_nonce($_POST['image_upload_nonce'], 'image_upload_action')) {
        wp_die('Güvenlik hatası!');
    }

    if (empty($_FILES['image_files'])) {
        echo '<div class="error"><p>Lütfen dosya seçin!</p></div>';
        return;
    }

    $files = $_FILES['image_files'];
    $uploaded = 0;

    foreach ($files['name'] as $key => $value) {
        if ($files['name'][$key] && $files['error'][$key] === UPLOAD_ERR_OK) {
            if (process_single_file($files, $key)) $uploaded++;
        }
    }

    echo "<div class='updated'><p>Başarıyla tamamlandı: $uploaded yazı oluşturuldu</p></div>";
}

// Tek dosya işleme
function process_single_file($files, $key) {
    $file = [
        'name' => sanitize_file_name($files['name'][$key]),
        'type' => $files['type'][$key],
        'tmp_name' => $files['tmp_name'][$key],
        'error' => $files['error'][$key],
        'size' => $files['size'][$key]
    ];

    $filename = pathinfo($file['name'], PATHINFO_FILENAME);
    $date = extract_date_from_filename($filename);

    $upload = wp_upload_bits($file['name'], null, file_get_contents($file['tmp_name']));
    if ($upload['error']) return false;

    $attachment_id = create_attachment($upload['file'], $date);
    if (!$attachment_id) return false;

    return create_image_post($filename, $date, $attachment_id);
}

// Tarih çıkarımı
function extract_date_from_filename($filename) {
    $date_str = substr($filename, 0, 8);

    if (preg_match('/^(\d{4})(\d{2})(\d{2})$/', $date_str, $matches)) {
        $year = $matches[1];
        $month = $matches[2];
        $day = $matches[3];
        
        if (checkdate($month, $day, $year)) {
            try {
                $date = new DateTime("$year-$month-$day");
                return $date->format('Y-m-d 12:00:00');
            } catch (Exception $e) {}
        }
    }
    return current_time('mysql');
}

// Medya dosyası oluşturma
function create_attachment($file, $date) {
    $attachment = [
        'post_mime_type' => mime_content_type($file),
        'post_title' => pathinfo($file, PATHINFO_FILENAME),
        'post_content' => '',
        'post_status' => 'inherit',
        'post_date' => $date,
        'post_date_gmt' => get_gmt_from_date($date)
    ];

    $attach_id = wp_insert_attachment($attachment, $file);
    if (!is_wp_error($attach_id)) {
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attach_data = wp_generate_attachment_metadata($attach_id, $file);
        wp_update_attachment_metadata($attach_id, $attach_data);
        return $attach_id;
    }
    return false;
}

// Yazı oluşturma ve kategorilere ekleme
function create_image_post($title, $date, $attachment_id) {
    $clean_title = sanitize_text_field(str_replace(['_', '-'], ' ', substr($title, 8)));
    
    $new_post = [
        'post_title' => $clean_title,
        'post_content' => '',
        'post_status' => 'publish',
        'post_type' => 'post',
        'post_date' => $date,
        'post_date_gmt' => get_gmt_from_date($date),
        'meta_input' => [
            '_thumbnail_id' => $attachment_id
        ]
    ];
    
    $post_id = wp_insert_post($new_post, true);
    
    if (!is_wp_error($post_id)) {
        $categories = [];
        foreach (['tarih', 'manşetler'] as $category) {
            $term = get_term_by('slug', $category, 'category');
            if ($term) $categories[] = $term->term_id;
        }
        wp_set_post_categories($post_id, $categories);
        return true;
    }
    return false;
}
?>
muratca61
Site Admin
Mesajlar: 35899
Kayıt: Cmt Ara 21, 2024 7:56 am

Re: Manşet ekleme scripti

Mesaj gönderen muratca61 »

gayet güzel bir üstteki sorunlar giderilmiş. son olarak şimdi görseli yazının içine de ekleyeceğiz ve yükleme işlemini gösteren loading bar ekleteceğiz.
image-uploader.php

Kod:Tümünü seç

<?php
/*
Plugin Name: Toplu Resimli Yazı Yükleyici
Description: Çoklu resim yükleme, otomatik yazı oluşturma ve tarih çıkarma
Version: 1.5
Author: Sizin Adınız
*/

// Admin menüsüne ekleme
function image_bulk_uploader_menu() {
    add_menu_page(
        'Toplu Resim Yükle',
        'Resimli Yazı Ekle',
        'manage_options',
        'image-bulk-uploader',
        'uploader_admin_page',
        'dashicons-images-alt2',
        6
    );
}
add_action('admin_menu', 'image_bulk_uploader_menu');

// Admin arayüzü
function uploader_admin_page() {
    ?>
    <div class="wrap">
        <h1>Toplu Resim Yükleme</h1>
        <form method="post" enctype="multipart/form-data">
            <?php wp_nonce_field('image_upload_action', 'image_upload_nonce'); ?>
            <input type="file" name="image_files[]" multiple accept="image/*">
            <p class="description">• Dosya adı formatı: YYYYAAgg_ornek.jpg (Örn: 19820325_tercuman.jpg)<br>
               • İlk 8 karakter tarih olmalı (19820325 → 25.03.1982)</p>
            <?php submit_button('Yükle ve Yazıları Oluştur'); ?>
        </form>
        <?php
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            handle_image_upload();
        }
        ?>
    </div>
    <?php
}

// Gerekli kategorileri kontrol et ve oluştur
function check_required_categories() {
    $categories = ['tarih', 'manşetler'];
    foreach ($categories as $category) {
        if (!term_exists($category, 'category')) {
            wp_insert_term(
                ucfirst($category),
                'category',
                ['slug' => sanitize_title($category)]
            );
        }
    }
}

// Dosya işleme fonksiyonu
function handle_image_upload() {
    check_required_categories();
    
    if (!current_user_can('manage_options')) {
        wp_die('Yetkiniz yok!');
    }
    
    if (!wp_verify_nonce($_POST['image_upload_nonce'], 'image_upload_action')) {
        wp_die('Güvenlik hatası!');
    }

    if (empty($_FILES['image_files'])) {
        echo '<div class="error"><p>Lütfen dosya seçin!</p></div>';
        return;
    }

    $files = $_FILES['image_files'];
    $uploaded = 0;

    foreach ($files['name'] as $key => $value) {
        if ($files['name'][$key] && $files['error'][$key] === UPLOAD_ERR_OK) {
            if (process_single_file($files, $key)) $uploaded++;
        }
    }

    echo "<div class='updated'><p>Başarıyla tamamlandı: $uploaded yazı oluşturuldu</p></div>";
}

// Tek dosya işleme
function process_single_file($files, $key) {
    $file = [
        'name' => sanitize_file_name($files['name'][$key]),
        'type' => $files['type'][$key],
        'tmp_name' => $files['tmp_name'][$key],
        'error' => $files['error'][$key],
        'size' => $files['size'][$key]
    ];

    $filename = pathinfo($file['name'], PATHINFO_FILENAME);
    $date = extract_date_from_filename($filename);

    $upload = wp_upload_bits($file['name'], null, file_get_contents($file['tmp_name']));
    if ($upload['error']) return false;

    $attachment_id = create_attachment($upload['file'], $date);
    if (!$attachment_id) return false;

    return create_image_post($filename, $date, $attachment_id);
}

// Tarih çıkarımı
function extract_date_from_filename($filename) {
    $date_str = substr($filename, 0, 8);

    if (preg_match('/^(\d{4})(\d{2})(\d{2})$/', $date_str, $matches)) {
        $year = $matches[1];
        $month = $matches[2];
        $day = $matches[3];
        
        if (checkdate($month, $day, $year)) {
            try {
                $date = new DateTime("$year-$month-$day");
                return $date->format('Y-m-d 12:00:00');
            } catch (Exception $e) {}
        }
    }
    return current_time('mysql');
}

// Medya dosyası oluşturma
function create_attachment($file, $date) {
    $attachment = [
        'post_mime_type' => mime_content_type($file),
        'post_title' => pathinfo($file, PATHINFO_FILENAME),
        'post_content' => '',
        'post_status' => 'inherit',
        'post_date' => $date,
        'post_date_gmt' => get_gmt_from_date($date)
    ];

    $attach_id = wp_insert_attachment($attachment, $file);
    if (!is_wp_error($attach_id)) {
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attach_data = wp_generate_attachment_metadata($attach_id, $file);
        wp_update_attachment_metadata($attach_id, $attach_data);
        return $attach_id;
    }
    return false;
}

// Yazı oluşturma ve kategorilere ekleme
function create_image_post($title, $date, $attachment_id) {
    // Başlığa yayımlama tarihini ekle
    $formatted_date = date('d.m.Y', strtotime($date));  // "01.01.1991" formatında tarih
    $clean_title = sanitize_text_field(str_replace(['_', '-'], ' ', substr($title, 8)));
    $full_title = $formatted_date . ' ' . $clean_title;  // Tarih + Başlık

    $new_post = [
        'post_title' => $full_title,
        'post_content' => '',
        'post_status' => 'publish',
        'post_type' => 'post',
        'post_date' => $date,
        'post_date_gmt' => get_gmt_from_date($date),
        'meta_input' => [
            '_thumbnail_id' => $attachment_id
        ]
    ];
    
    $post_id = wp_insert_post($new_post, true);
    
    if (!is_wp_error($post_id)) {
        // Kategoriler
        $categories = [];
        foreach (['tarih', 'manşetler'] as $category) {
            $term = get_term_by('slug', $category, 'category');
            if ($term) $categories[] = $term->term_id;
        }
        wp_set_post_categories($post_id, $categories);

        // Etiketler ekle
        wp_set_post_tags($post_id, 'tarih, manşet');
        
        return true;
    }
    return false;
}
?>
muratca61
Site Admin
Mesajlar: 35899
Kayıt: Cmt Ara 21, 2024 7:56 am

Re: Manşet ekleme scripti

Mesaj gönderen muratca61 »

son çalışan kod içerikleri
image_bulk_uploader.php

Kod:Tümünü seç

<?php
/*
Plugin Name: Toplu Resimli Yazı Yükleyici
Description: Çoklu resim yükleme, otomatik yazı oluşturma ve tarih çıkarma
Version: 1.6
Author: Sizin Adınız
*/

// Admin menüsüne ekleme
function image_bulk_uploader_menu() {
    add_menu_page(
        'Toplu Resim Yükle',
        'Resimli Yazı Ekle',
        'manage_options',
        'image-bulk-uploader',
        'uploader_admin_page',
        'dashicons-images-alt2',
        6
    );
}
add_action('admin_menu', 'image_bulk_uploader_menu');

// Admin arayüzü
function uploader_admin_page() {
    wp_enqueue_script('bulk-uploader-js', plugins_url('js/uploader.js', __FILE__), ['jquery'], null, true);
    wp_enqueue_style('bulk-uploader-css', plugins_url('css/style.css', __FILE__));
    wp_localize_script('bulk-uploader-js', 'uploader_vars', [
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('image_upload_action')
    ]);
    
    ?>
    <div class="wrap">
        <h1>Toplu Resim Yükleme</h1>
        <div id="upload-progress">
            <div class="progress-bar">
                <div class="progress-fill"></div>
            </div>
            <p class="status-text">Yükleniyor: <span class="current-file">0</span>/<span class="total-files">0</span></p>
        </div>
        <form id="bulk-upload-form" method="post" enctype="multipart/form-data">
            <input type="file" name="image_files[]" multiple accept="image/*" id="image-files">
            <p class="description">• Dosya adı formatı: YYYYAAgg_ornek.jpg (Örn: 19820325_tercuman.jpg)</p>
            <?php submit_button('Yükle ve Yazıları Oluştur'); ?>
        </form>
        <div id="upload-results"></div>
    </div>
    <?php
}

// Kategori kontrolü
function check_required_categories() {
    $categories = ['tarih', 'manşetler'];
    foreach ($categories as $category) {
        if (!term_exists($category, 'category')) {
            wp_insert_term(
                ucfirst($category),
                'category',
                ['slug' => sanitize_title($category)]
            );
        }
    }
}

// AJAX handler
add_action('wp_ajax_handle_bulk_upload', 'handle_image_upload_ajax');
function handle_image_upload_ajax() {
    check_required_categories();
    
    if (!current_user_can('manage_options') || 
        !wp_verify_nonce($_POST['nonce'], 'image_upload_action')) {
        wp_send_json_error('Yetki hatası!');
    }

    $files = $_FILES['image_files'];
    $total = count($files['name']);
    $results = [];
    
    for ($i = 0; $i < $total; $i++) {
        if ($files['name'][$i] && $files['error'][$i] === UPLOAD_ERR_OK) {
            $result = process_single_file($files, $i);
            $results[] = [
                'file' => sanitize_file_name($files['name'][$i]),
                'status' => $result ? 'success' : 'error',
                'message' => $result ? 'Başarılı' : 'Hata oluştu'
            ];
        }
    }
    
    wp_send_json_success([
        'total' => $total,
        'results' => $results
    ]);
}

// Tek dosya işleme
function process_single_file($files, $key) {
    $file = [
        'name' => sanitize_file_name($files['name'][$key]),
        'type' => $files['type'][$key],
        'tmp_name' => $files['tmp_name'][$key],
        'error' => $files['error'][$key],
        'size' => $files['size'][$key]
    ];

    $filename = pathinfo($file['name'], PATHINFO_FILENAME);
    $date = extract_date_from_filename($filename);

    $upload = wp_upload_bits($file['name'], null, file_get_contents($file['tmp_name']));
    if ($upload['error']) return false;

    $attachment_id = create_attachment($upload['file'], $date);
    return $attachment_id ? create_image_post($filename, $date, $attachment_id) : false;
}

// Tarih çıkarımı
function extract_date_from_filename($filename) {
    $date_str = substr($filename, 0, 8);
    if (preg_match('/^(\d{4})(\d{2})(\d{2})$/', $date_str, $matches)) {
        $year = $matches[1];
        $month = $matches[2];
        $day = $matches[3];
        if (checkdate($month, $day, $year)) {
            try {
                return new DateTime("$year-$month-$day 12:00:00");
            } catch (Exception $e) {}
        }
    }
    return new DateTime(current_time('mysql'));
}

// Medya oluşturma
function create_attachment($file, $date) {
    $attachment = [
        'post_mime_type' => mime_content_type($file),
        'post_title' => pathinfo($file, PATHINFO_FILENAME),
        'post_content' => '',
        'post_status' => 'inherit',
        'post_date' => $date->format('Y-m-d H:i:s'),
        'post_date_gmt' => get_gmt_from_date($date->format('Y-m-d H:i:s'))
    ];

    $attach_id = wp_insert_attachment($attachment, $file);
    if (!is_wp_error($attach_id)) {
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        $attach_data = wp_generate_attachment_metadata($attach_id, $file);
        wp_update_attachment_metadata($attach_id, $attach_data);
        return $attach_id;
    }
    return false;
}

// Yazı oluşturma
function create_image_post($title, $date, $attachment_id) {
    $image_url = wp_get_attachment_url($attachment_id);
    $formatted_date = $date->format('d.m.Y');
    $clean_title = sanitize_text_field(str_replace(['_', '-'], ' ', substr($title, 8)));

    $new_post = [
        'post_title' => "$formatted_date $clean_title",
        'post_content' => "<img src='$image_url' alt='$clean_title' class='wp-post-image'>",
        'post_status' => 'publish',
        'post_type' => 'post',
        'post_date' => $date->format('Y-m-d H:i:s'),
        'post_date_gmt' => get_gmt_from_date($date->format('Y-m-d H:i:s')),
        'meta_input' => [
            '_thumbnail_id' => $attachment_id
        ]
    ];

    $post_id = wp_insert_post($new_post);
    if (!is_wp_error($post_id)) {
        $categories = array_map(function($cat) {
            return get_term_by('slug', $cat, 'category')->term_id;
        }, ['tarih', 'manşetler']);
        
        wp_set_post_categories($post_id, $categories);
        return true;
    }
    return false;
}
?>
style.css

Kod:Tümünü seç

#upload-progress {
    margin: 20px 0;
    display: none;
}

.progress-bar {
    background: #f1f1f1;
    border-radius: 13px;
    height: 20px;
    overflow: hidden;
}

.progress-fill {
    background: #0073aa;
    height: 100%;
    width: 0%;
    transition: width 0.4s ease;
}

.status-text {
    color: #666;
    margin: 5px 0;
}

#upload-results ul {
    list-style-type: none;
    padding-left: 0;
}

#upload-results li {
    padding: 5px 0;
    border-bottom: 1px solid #ddd;
}
uploader.js

Kod:Tümünü seç

jQuery(document).ready(function($) {
    const form = $('#bulk-upload-form');
    const progressBar = $('.progress-fill');
    const currentFile = $('.current-file');
    const totalFiles = $('.total-files');
    const resultsDiv = $('#upload-results');

    form.on('submit', function(e) {
        e.preventDefault();
        const formData = new FormData(this);
        const files = $('#image-files')[0].files;
        
        $('#upload-progress').show();
        totalFiles.text(files.length);
        formData.append('action', 'handle_bulk_upload');
        formData.append('nonce', uploader_vars.nonce);

        $.ajax({
            url: uploader_vars.ajax_url,
            method: 'POST',
            data: formData,
            contentType: false,
            processData: false,
            xhr: function() {
                const xhr = new XMLHttpRequest();
                xhr.upload.addEventListener('progress', function(e) {
                    const percent = (e.loaded / e.total) * 100;
                    progressBar.css('width', percent + '%');
                });
                return xhr;
            },
            success: function(response) {
                let resultHTML = '<div class="notice notice-success"><ul>';
                response.data.results.forEach(item => {
                    resultHTML += `<li>${item.file}: ${item.status === 'success' ? '✅' : '❌'}</li>`;
                });
                resultHTML += '</ul></div>';
                resultsDiv.html(resultHTML);
            },
            error: function() {
                resultsDiv.html('<div class="notice notice-error">Hata oluştu!</div>');
            }
        });
    });
});
Cevapla