isaac mathias
isaac mathias

Reputation: 1

Exporting a laravel blog content to wordpress

I have a blog built in laravel , but I want to move all the content to an existing blog built in wordpress, I have tried exporting the database as a CSV file to import on my wordpress database but its not in the same table format , any idea on how to import the contents

Upvotes: 0

Views: 1451

Answers (1)

Krishnadas PC
Krishnadas PC

Reputation: 6539

It is less likely to work the direct import since the table structures will be different. So what we can do is to

  1. Connect to the Laravel Database from your WordPress installation.
  2. Select the Laravel db data using wpdb query (https://codex.wordpress.org/Class_Reference/wpdb)
  3. Insert into our WordPress site using wp_insert_post (https://developer.wordpress.org/reference/functions/wp_insert_post/)
  4. If there are pictures attached to the Laravel Blog you must query it as well and upload it and attach to the WordPress as well. (https://codex.wordpress.org/Function_Reference/wp_insert_attachment)

You must take WordPress DB backup before doing this.

Sample code will be something like this.

$mydb = new wpdb('username','password','laravel_database','localhost');
$rows = $mydb->get_results("select title, content from laravle_blogs_table");

foreach ($rows as $obj) :
   // Create post object
$my_post = array(
  'post_title'    => wp_strip_all_tags( $obj->title ),
  'post_content'  => $obj->content,
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => array( 8,39 )
);

// Insert the post into the database
wp_insert_post( $my_post );
endforeach;

You must change the field names as per your database.

This code can be put in any of the active theme files may be header.php or footer.php or other template and just load the page from your browser. If there are a huge number of posts put a limit to the select query and insert step by step.

Upvotes: 1

Related Questions