Posts aus CSV Datei erstellen

Post Type = portfolio
ACF Field Post Type = link_url

PHP
<?php
/**
 * Custom functions and definitions
 */

// EINZIGER Hook für Admin-Importe
add_action('admin_init', function () {
    if (!current_user_can('administrator')) return;
    if (isset($_GET['import_linktree'])) {
        import_linktree_articles();
        wp_die('✅ Linktree-Import abgeschlossen. <br><br><a href="' . admin_url() . '">Zurück zum Dashboard</a>');
    }
    if (isset($_GET['import_csv'])) {
        import_portfolio_from_csv();
        wp_die('✅ CSV-Import abgeschlossen. <br><br><a href="' . admin_url() . '">Zurück zum Dashboard</a>');
    }
});

// CSV-Importer: Portfolio-Einträge mit Bild & ACF
function import_portfolio_from_csv() {
    $csv_path = get_stylesheet_directory() . '/wp_ultimate_import.csv';
    
    // Erforderliche WordPress-Dateien laden
    require_once ABSPATH . 'wp-admin/includes/image.php';
    require_once ABSPATH . 'wp-admin/includes/file.php';
    require_once ABSPATH . 'wp-admin/includes/media.php';
    
    // CSV-Datei mit UTF-8 Encoding lesen
    $csv_content = file_get_contents($csv_path);
    if (substr($csv_content, 0, 3) == "\xEF\xBB\xBF") {
        $csv_content = substr($csv_content, 3);
    }
    
    $temp_file = tmpfile();
    fwrite($temp_file, $csv_content);
    rewind($temp_file);
    
    // Header überspringen
    fgetcsv($temp_file, 0, ',');
    
    $imported_count = 0;
    
    echo '<div style="font-family: Arial, sans-serif; max-width: 800px; margin: 20px;">';
    echo '<h2>CSV-Import wird durchgeführt...</h2>';
    
    while (($row = fgetcsv($temp_file, 0, ',')) !== false) {
        $title = trim($row[0]);
        $link_url = trim($row[1]);
        $image_url = trim($row[2]);
        
        // Prüfen ob Post bereits existiert
        $existing_post = get_page_by_title($title, OBJECT, 'portfolio');
        if ($existing_post) {
            echo '<p style="color: orange;">⚠️ Post existiert bereits: ' . esc_html($title) . '</p>';
            continue;
        }
        
        // Post erstellen
        $post_id = wp_insert_post([
            'post_title'  => $title,
            'post_type'   => 'portfolio',
            'post_status' => 'publish',
            'post_content' => '',
        ]);
        
        echo '<p style="color: green;">✅ Post erstellt: ' . esc_html($title) . ' (ID: ' . $post_id . ')</p>';
        
        // Link-URL als Custom Field setzen
        if (!empty($link_url)) {
            update_post_meta($post_id, 'link_url', $link_url);
            echo '<p style="color: blue; margin-left: 20px;">🔗 Link gesetzt: ' . esc_html($link_url) . '</p>';
        }
        
        // Bild importieren
        if (!empty($image_url)) {
            echo '<p style="margin-left: 20px;">📸 Lade Bild: ' . esc_html($image_url) . '</p>';
            $attachment_id = media_sideload_image($image_url, $post_id, $title, 'id');
            if (!is_wp_error($attachment_id)) {
                set_post_thumbnail($post_id, $attachment_id);
                echo '<p style="color: green; margin-left: 20px;">✅ Bild erfolgreich importiert.</p>';
            } else {
                echo '<p style="color: red; margin-left: 20px;">❌ Bildimport fehlgeschlagen: ' . $attachment_id->get_error_message() . '</p>';
            }
        }
        
        $imported_count++;
        echo '<hr style="margin: 10px 0;">';
    }
    
    fclose($temp_file);
    
    echo '<h3>Import abgeschlossen!</h3>';
    echo '<p><strong>Erfolgreich importiert:</strong> ' . $imported_count . ' Posts</p>';
    echo '</div>';
}

// Platzhalterfunktion für alten Linktree-Import (falls noch nötig)
function import_linktree_articles() {
    echo '❌ Linktree-Import ist nicht definiert.';
}