Andrew - dziękuję bardzo za pomoc! XML RPC jest o wiele lepszym rozwiązaniem.
Wykombinowałem taki kod, który dodaje nowy post z obrazkiem, jednak obrazek za nic nie chce się dodać. Post wlatuję bez problemów.
I niestety od kilku godzin nic nie mogę wymyślić sensownego - więc proszę Was o pomoc.
<?php
if(!function_exists('mime_content_type')) {
function mime_content_type($filename) {
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',
// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',
// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',
// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);
return $mime_types[$ext];
}
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
else {
return 'application/octet-stream';
}
}
}
include_once( '../k2wordpress/wp-includes/class-IXR.php' );
include_once( '../k2wordpress/wp-includes/class-wp-http-ixr-client.php' );
$usr = 'admin';
$pwd = 'admin';
$xmlrpc = 'http://localhost/k2wordpress/xmlrpc.php';
$client = new IXR_Client($xmlrpc);
// zdjęcie
$img_attach = 'test.jpg';
$img_attach_content = array( 'type' => mime_content_type($img_attach),
);
$status = $client->query( 'wp.uploadFile','1', $usr, $pwd, $img_attach_content );
$image_returnInfo = $client ->getResponse();
// dodanie postu
array( 'key' => 'blabla1', 'value' => 'blabla1_value' ), array( 'key' => 'blabla12', 'value' => 'blabla1_value2') );
'post_type' => 'post',
'post_status' => 'draft', //for now
'post_title' => 'XMLRPC Test',
'post_author' => 3,
'post_name' => 'XMLRPC Test',
'post_content' => 'XMLRPC Test Content',
'custom_fields' => $custom_fields
);
$res = $client -> query('wp.newPost',1, $usr, $pwd, $post_content);
$postID = $client->getResponse();
if(!$res) {
$error = $client->getErrorCode().': '.$client->getErrorMessage();
echo 'Something went wrong....'; }
else {
echo 'The Project Created Successfully('.$res.')<br>Post ID is '.$postID.'<br>'; }
// obrazek
$img_attach_content2 = array( 'post_type' => 'attachment',
'post_status' => 'inherit',
'post_title' => $postID,
'post_name' => $postID,
'post_parent' => $postID,
'guid' => $image_returnInfo['url'],
'post_content' => '',
'post_mime_type' => 'image/jpg'
);
$res2 = $client -> query('wp.editPost', 0, $usr, $pwd, $image_returnInfo['id'], $img_attach_content2);
$postIDimg = $client->getResponse();
// edycja postu
'post_status' => 'publish', //publish
'wp_post_thumbnail' => $image_returnInfo['id'],
'custom_fields' => array( 'key' => '_thumbnail_id', 'value' => $image_returnInfo['id'] ) );
$media2= $client->query('wp.editPost',0, $usr, $pwd, $postID, $post_content2);
?>
Dodam, że wszystko się ładnie wykonuję, obrazek jest uploadowany do Wordpress
Problem leży jedynie w aktualizacji postu i dodaniu do niego obrazka.
Także pewnie końcówka kodu...