最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How can I import an excel column into wordpress database?

programmeradmin1浏览0评论

I have an excel column with around 1000 values, i need to import all these values into my wordpress database so i can create a small form with a text input and a submit button to let the users to add an input value and check if it exists in the imported values from the database.

What is the best way to do this, do i have to create a new table for this column or should i add it to an existing table?

I have an excel column with around 1000 values, i need to import all these values into my wordpress database so i can create a small form with a text input and a submit button to let the users to add an input value and check if it exists in the imported values from the database.

What is the best way to do this, do i have to create a new table for this column or should i add it to an existing table?

Share Improve this question asked Nov 28, 2017 at 7:09 TarekTarek 333 silver badges8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

There is some way to import excel to WordPress database. but I have an easy solution to import this file.

  1. Go to convertcsv and convert your excel file to JSON.
  2. Create the below files and folders:

Folder: wp-content/plugins/import/

File: wp-content/plugins/import/import.json

File: wp-content/plugins/import/import.php

  1. Then save JSON file to import.json
  2. Put below code in import.php

.

/*
* Plugin Name: Import
* Plugin URI: https://veronalabs
* Description: Import excel data to WordPress
* Author: Mostafa Soufi
* Version: 1.0
* Author URI: https://mostafa-soufi.ir
*/

if ( isset( $_GET['do'] ) and $_GET['do'] == 'import' ) {
    $json = file_get_contents( dirname( __FILE__ ) . '/import.json' );

    if ( $json ) {
        $data = json_decode( $json, true );

        // Print your data
        echo '<pre>' . print_r( $data, 1 ) . '</pre>';

        // Each data
        foreach ( $data as $item ) {
            // Create post object
            $args = array(
                'post_title'   => $item['title'], // your column
                'post_content' => $item['content'], // your column
                'post_status'  => 'publish',
                'post_type'    => 'post',
                'post_author'  => 1
            );

            // Insert the post into the database
            //$post_id = wp_insert_post( $args );
        }
    }

    exit;
}
  1. Go to Plugins and enable Import plugin. then run this request in your WordPress:

    http://yoursite/wp-admin/?do=import

Don't forget, you should get valid data from your json. You can see this data through print_r before each data.

发布评论

评论列表(0)

  1. 暂无评论