Tuesday, January 31, 2023
HomeWordPress Developmentfeatures - Creating posts with php-script + csv

features – Creating posts with php-script + csv


I am making an attempt to create CPTs with a Script + CSV. Every row schould create a publish, whereas every column ought to fill the title/ACF-Fields.

$csv_file_path="/take a look at.csv";

operate create_wp_cpt_from_csv() {
    world $csv_file_path;
    if (($deal with = fopen($csv_file_path, "r")) !== FALSE) {
        whereas (($information = fgetcsv($deal with, 1000, $delimiter)) !== FALSE) {
            $publish = array(
                'post_title' => $information[0],
                'post_content' => '',
                'post_status' => 'publish',
                'post_author' => 1,
                'post_type' => 'sorten'
            );
            $post_id = wp_insert_post($publish);
            update_field('acf_1', $information[1], $post_id);
            update_field('acf_2', $information[2], $post_id);
        }
        fclose($deal with);
    }
}

add_action('init', 'create_link_to_import_csv');

operate create_link_to_import_csv() {
    add_menu_page( 'Import CSV', 'Import CSV', 'manage_options', 'import_csv', 'import_csv_function');
}

operate import_csv_function(){
    create_wp_cpt_from_csv();
}

screenshot

I applied this code in my features.php. CSV is in the identical listing because the features.php.

Sadly, every time i run the script by way of clicking on the “Import CSV” within the admin-backend, it simply opens the next hyperlink, however it would not create posts:

/wp-admin/admin.php?web page=import_csv

Theme: Impreza

Are sure permissions lacking right here? Can this work in any respect by way of the features.php? Have i in-built a basic error within the operate?

Thanks guys!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments