Please disable ad blocker to see the page.

How To Import Articles Into WordPress Via Excel

However, a tool has been given in WordPress to import articles, but it is useful if you import them from particular sites.

Here I would like to write a php script to import articles into WordPress via excel. So you can easy import your articles without any difficulty.

At first, create a new php file.

Add a form for uploading.

<form action="" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

Then upload excel to a folder using below code.

$targetdir = "uploads/";
$targetfile = $targetdir . basename($_FILES["fileToUpload"]["name"]);
$filetoread=move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $targetfile);

Now you will need two files oleread.inc and reader.php to read excel. Download them from here.

Include reader.php on the top of the php file.

require_once('reader.php');

After this, pick the name of the uploaded file.

$filename= "uploads/".$_FILES["fileToUpload"]["name"];

To open excel file add the following code:

$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read($filename);

Now read each cell one by one with the help of "for" loop.

for($x=0; $x <= count($data->sheets[0]["cells"]); $x++)
{
$title=$data->sheets[0]["cells"][$x][1];
$content = $data->sheets[0]["cells"][$x][2];
$status = $data->sheets[0]["cells"][$x][3];

Then we need to insert each article into WordPress database for that collect all the data in a variable in the form of array.

$thepost = array( 'post_title' => $post_title, 'post_content' => $post_content );

To run insert we have to establish a database connection, to do this include wp-config.php on the top of the php file and use below function to insert articles:

wp_insert_post( $thepost );

}

That's it.
Previous
Next Post »
3 Comment
avatar

Thanks for sharing your view to our knowledge’s, its helps me plenty keep sharing…
PHP Training in Chennai

Balas
avatar

The best thing is that your blog really informative thanks for your great information!
erp providers in chennai

Balas
avatar

I heve read your blog it's very interesting and informative. Keep sharing.
erp in chennai | erp software solutions in chennai

Balas