<?php
/*
Links2WP - links2wp.php
Import a text-file list of links into your WordPress Links Manager.
Author: Kaf Oseo (http://szub.net)
Run in directory with wp-blog-header.php and 'links.txt'. Format for
links is tab-separated list (set $sep variable to change separator),
one link entry per line, as follows:
CAT_ID LINK_URL LINK_NAME LINK_DESCRIPTION
CAT_ID is the numeric ID of a link category the link is to be added
to. The rest should be obvious.
Example list:
1 http://wordpress.org WordPress
1 http://photomatt.net Photo Matt Matt Mullenweg
2 http://www.google.com Google
1 http://guff.szub.net guff Kaf Oseo's blog
*/
$links_file = "links.txt"; // Filename for your links text file.
$sep = "\t"; // Separator character. Default is tab (\t).
$linkowner = 1; // User ID to assign links to. 1 = admin.
// You should not need to edit anything from here on.
if(!file_exists('wp-blog-header.php')) {
echo "<p>This script must reside in the same directory as wp-blog-header.php.</p>";
die;
}
require('wp-blog-header.php');
if(!file_exists($links_file)) {
echo "<p>The file '$links_file' does not exist. Make sure you've placed it with this script in the same directory.</p>";
} else {
$count = 0;
if (!$sep)
$sep = "\t";
if (!$linkowner)
$linkowner = 1;
$id = fopen($links_file, "r");
while($data = fgetcsv($id, filesize($links_file), "$sep"))
$table[] = $data;
fclose($id);
foreach($table as $row) {
$linkcat = trim(addslashes($row[0]));
$linkurl = trim(addslashes($row[1]));
$linkname = trim(addslashes($row[2]));
$linkdesc = trim(addslashes($row[3]));
$check = $wpdb->query("SELECT link_id FROM $wpdb->links WHERE link_url = '$linkurl'");
if($check) {
echo "<strong style=\"color:blue;\">NOTICE:</strong> $row[2] ($row[1]) is already in the database.<br />\n";
} else {
$datetime = date('Y-m-d H:i:s');
$sql = "INSERT INTO $wpdb->links VALUES (NULL, '$linkurl', '$linkname', '', '', $linkcat, '$linkdesc', 'Y', $linkowner, 0, '$datetime', '', '', '')";
$result = mysql_query($sql) or print "<strong style=\"color:red;\">WARNING:</strong> '$row[2]' ($row[1]) could not be added to the database.<br />\n";
if($result) {
echo "$row[2] ($row[1]) has been added to the database.<br />\n";
$count++;
}
}
}
echo "<p>$count links added to database.</p>";
}
?>