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;
}