wordpress用php脚本导入文章

在网上找了半天发现没有用php脚本导入文章的代码。于是自己写了一个。。只需要require 这个php文件

然后通过fabu(‘你好我是标题‘,‘你好我是内容‘,‘你好我是分类‘,‘你好我是tag1,你好我是tag2‘);

直接发布!

奉上源码~

PS:记得修改handle方法的数据库账户密码和数据库名称

<?php
//fabu(‘你好我是标题‘,‘你好我是内容‘,‘你好我是分类‘,‘你好我是tag1,你好我是tag2‘);
function fabu($title,$content,$category,$tag){
	$title = my_real_escape_string($title);
	$content = my_real_escape_string($content);
	$category = $category;
	$tag = $tag;
	$time = date(‘Y-m-d H:i:s‘);
	$sql = "INSERT INTO `wp_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES (NULL, 1, ‘$time‘, ‘$time‘, ‘$content‘, ‘$title‘, ‘‘, ‘publish‘, ‘open‘, ‘open‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘$time‘, ‘$time‘, ‘‘, ‘0‘, ‘‘, ‘0‘, ‘post‘, ‘‘, ‘0‘);";
	$res = handle($sql);
	if($res == true){
		$res = handle(‘select max(id) from wp_posts;‘);
		$id = intval( $res[0][0] );
	}else{
		return false;
	}
	$termIdArr = array();
	$categoryId = getTerm_id($category,0);
	$tagArr = explode(‘,‘, $tag);
	if(!empty($tagArr)){
		foreach ($tagArr as $value) {
			$termIdArr[] = getTerm_id($value,1);
		}
	}
}

function handle($sql,$database = ‘database‘)
{
    $db = mysql_connect(‘127.0.0.1:3306‘,‘root‘, ‘root‘);
    mysql_select_db($database);
    mysql_query("SET NAMES ‘utf8‘",$db); 
    if(!$db)
    {
        echo "Error! Can‘t connect RS database!";
        return false;
    }
    $res = mysql_query($sql ,$db );
    if($res === false)
    {
        echo "Query Failed!$sql\n";
        return false;
    }
    mysql_close($db);
    if($res === true){
        return true;
    }
    $arrRes = array();
    while($row = mysql_fetch_row($res))
    {
        $arrRes[] = $row; 
    }
    return $arrRes;
}

function my_real_escape_string($str)
{
    return strtr($str, array(
        "\x00" => ‘\x00‘,
        "\n" => ‘\n‘,
        "\r" => ‘\r‘,
        ‘\\‘ => ‘\\\\‘,
        "‘" => "\‘",
        ‘"‘ => ‘\"‘,
        "\x1a" => ‘\x1a‘
    ));
}

function getTerm_id($name ,$type){
	if($type == 0){
		$default = 1;
	}else{
		$default = 9;
	}
	$res = handle("select term_id from wp_terms where name = ‘$name‘;");
	if(empty($res)){
		$slug = strtolower(urlencode($name));
		$sql = "INSERT INTO wp_terms (`term_id`,`name`,`slug`) VALUES (NULL,‘$name‘,‘$slug‘);";
		if(true == handle($sql)){
			$res = handle(‘select max(term_id) from wp_terms;‘);
			$id = $res[0][0];
			return intval($id);
		}
	}else{
		return intval( $res[0][0] );
	}
	return $default;
}
?>

wordpress用php脚本导入文章

上一篇:kali21安装中文输入法ibus


下一篇:asp.net 验证正则表达式