Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sjardim
Last active May 27, 2016 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjardim/bef084a55d0fb87a3afca6544b4a613b to your computer and use it in GitHub Desktop.
Save sjardim/bef084a55d0fb87a3afca6544b4a613b to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Import Books</title>
</head>
<body>
<table border='1' width='100%'>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
</tr>
</thead>
<tbody>
<?php
// get access to WordPress wpautop() function
//include("./wordpress/wp-includes/formatting.php");
$wpdb = new PDO("mysql:dbname=ricardo-wp4_db;host=localhost", "root", "root",
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
$books = wire('pages')->get('/books/');
$sql = "
SELECT * FROM wp_pods_books
";
$query = $wpdb->prepare($sql);
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
$book = $books->child("wpid=$row[id]"); // do we already have this book?
if(!$book->id) {
// create a new post
$book = new Page();
$book->template = 'book';
$book->parent = $books;
echo "Creating new book...\n";
}
$book->of(false);
$book->wpid = $row['id'];
$book->name = wire('sanitizer')->pageName($row['name'], true);
$en = $languages->get("default");
$book->title->setLanguageValue($en, $row['name']);
$book->body->setLanguageValue($en, $row['body']);
$book->introduction->setLanguageValue($en, $row['intro']);
//add more fields if needed
$pt = $languages->get("pt");
$book->title->setLanguageValue($pt, $row['name_pt']);
$book->body->setLanguageValue($pt, $markdown->convert($row['body_pt']));
$book->introduction->setLanguageValue($pt, $markdown->convert($row['intro']));
//We need to activate portuguese page so it will appear on the front-end
$book->set("status$pt", 1);
// give detailed report about this post
echo "<tr>" .
"<td>$row[id]</td>" .
"<td>$book->title</td>" .
"</tr>";
$book->save();
}
?>
</tbody>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment