あっ!!星占い

星占い、聖書、サラリーマン川柳、名言集などを公開します。

PHPでWordpressにAll in one SEOのカスタムフィールドをセットしてみた

色々なサイトの情報を頂いてWordpressXML/RPC.phpを使って自動投稿をやろうとしたがカスタムフィールドにAll in One SEOのデータを挿入しようしたら情報によるとmeta_keyの文字列の先頭がアンダーラインになっているとデータがセットできないらしい。アンダーラインを外すとデータはセットされるがAll in One SEOのカスタムフィールドと認識されない。仕方ないのでMysqlのデータベースを書き換えることでなんとかできた。
////////////////////////////////////////////////////
require_once("XML/RPC.php");

$host = "localhost";
$xmlrpc_path = "/wordpress/xmlrpc.php";
$appkey = '';
$user = 'user';
$passwd ='passwd';

$c = new XML_RPC_client($xmlrpc_path, $host, 80);

$appkey = new XML_RPC_Value($appkey, 'string');
$username = new XML_RPC_Value( $user, 'string' );
$passwd = new XML_RPC_Value( $passwd, 'string' );

$message = new XML_RPC_Message(
'blogger.getUsersBlogs',array($appkey, $username, $passwd)
);

$result = $c->send($message);

if(!$result){
exit('Could not connect to the server.');
} else if( $result->faultCode() ){
exit($result->faultString());
}

$blogs = XML_RPC_decode($result->value());

$blog_id = new XML_RPC_Value($blogs[0]["blogid"], "string");

$title = 'テスト投稿';
$categories = array(
new XML_RPC_Value("未分類", "string"),
);

$categories = array(
new XML_RPC_Value('テストカテゴリー', 'string'),
);
$description = 'テストです!!';
$tags = array(
new XML_RPC_Value('タグ1', 'string'),
new XML_RPC_Value('タグ2', 'string'),
new XML_RPC_Value('タグ3', 'string')
);
$custom_fields = array();
//アンダーラインを外してセット
$custom_fields = new XML_RPC_Value(
array(
'key' => new XML_RPC_Value('aioseop_title', 'string'),
'value' => new XML_RPC_Value('テスト', 'string')
), 'struct');
$custom_fields
= new XML_RPC_Value(
array(
'key' => new XML_RPC_Value('aioseop_descripition', 'string'),
'value' => new XML_RPC_Value('テストです', 'string')
), 'struct');
$custom_fields[] = new XML_RPC_Value(
array(
'key' => new XML_RPC_Value('aioseop_keywords', 'string'),
'value' => new XML_RPC_Value('キーワード', 'string')
), 'struct');
$content = new XML_RPC_Value(
array(
'title' => new XML_RPC_Value($title, 'string'),
'categories' => new XML_RPC_Value($categories, 'array'),
'description' => new XML_RPC_Value($description, 'string'),
'mt_keywords' => new XML_RPC_Value($tags, 'array'),
'wp_slug' => new XML_RPC_Value('テストスラッグ'.date('Yms'),'string'),
'dateCreated' => new XML_RPC_Value(time(), 'dateTime.iso8601'),
'custom_fields' => new XML_RPC_Value($custom_fields, 'struct')
),
'struct');
$publish = new XML_RPC_Value(1, "boolean");

$message = new XML_RPC_Message(
'metaWeblog.newPost',
array($blog_id, $username, $passwd, $content, $publish)
);

$result = $c->send($message);

if(!$result){
exit('Could not connect to the server.');
} else if( $result->faultCode() ){
exit($result->faultString());
}
$post_id = XML_RPC_decode($result->value());
//meta_keyのアンダーライン付加
$link = mysql_connect('localhost', 'user', 'passwd');
if (!$link) {
die('接続失敗です。'.mysql_error());
}


$db_selected = mysql_select_db('kazokuburo', $link);
if (!$db_selected){
die('データベース選択失敗です。'.mysql_error());
}


mysql_set_charset('utf8');
$sql = "update wp_postmeta set meta_key = '_aioseop_title' where meta_key = 'aioseop_title'";
if (!(mysql_query($sql))) {
die;
}
$sql = "update wp_postmeta set meta_key = '_aioseop_keywords' where meta_key = 'aioseop_keywords'";
if (!(mysql_query($sql))) {
die;
}
$sql = "update wp_postmeta set meta_key = '_aioseop_descripition' where meta_key = 'aioseop_descripition'";
if (!(mysql_query($sql))) {
die;
}
$close_flag = mysql_close($link);