Category Archives: Wordpress

XMLRPC – Programmatically post on WordPress with an attach image to post in WordPress

This is how I make it!
Shared on StackOverflow
http://stackoverflow.com/questions/17722743/attach-image-to-post-in-wordpress-xmlrpc/24413781#24413781

References
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts
http://codex.wordpress.org/Post_Status

require_once("IXR_Library.php.inc");
$title = 'My title';
$body = 'My body';
$category="category1, category2"; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
$keywords="keyword1, keyword2, keyword3";
$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format

$title = htmlentities($title,ENT_NOQUOTES,@$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,@$encoding);

$content = array(
'post_title'=>$title,
'post_content'=>$body,
'post_type'=>'some_custom_post_type',
'post_status' => 'draft', // http://codex.wordpress.org/Post_Status
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => array($customfields)
);

// Create the client object
$client = new IXR_Client('http://example.com/xmlrpc.php');
$username = "wp_username";
$password = "wp_username_password";

$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immediately, to save as draft set it as 'false'

if (!$client->query('wp.newPost', $params)) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage().'');

}
else
{
$post_id = $client->getResponse();
echo 'Inserted with id'.$post_id;
$picture = '/full/path/to/pic.jpg';
$content = array(
'name' => basename($picture),
'type' => mime_content_type($picture),
'bits' => new IXR_Base64(file_get_contents($picture)),
true
);
if (!$client->query('metaWeblog.newMediaObject', 1, $username, $password, $content)) {
die('
Something went wrong - newMediaObject'.$client->getErrorCode().' : '.$client->getErrorMessage().'
');
}
else
{
$media = $client->getResponse();
$content = array(
'post_status' => 'publish',
'post_thumbnail' => $media['id']
);
if (!$client->query('wp.editPost', 0, $username, $password, $post_id, $content)) {
die('
Something went wrong editPost - '.$client->getErrorCode().' : '.$client->getErrorMessage().'
');
}
}
}

WordPress caching – Quick Cache

I have a bunch of wordpress blogs, two of them getting 1k unique visitor each.

Both of homepage was taking almost 3 seconds to be compiled…
I’v installed a small WordPress Plugin named Quick Cache, to make cache of pages, and from almost 3 seconds now they are served in less that half of a second.

Before Quick Cache plugin

After Quick Cache plugin

History from some tests on the 10th of March and 17th of March.

Fatal error: Class ‘DOMDocument’ not found

I was moving a WP from a host to another, and using the WordPress Importer while loading the XML of the export “Fatal error: Class ‘DOMDocument’ not found“.

We have two solutions

  • Install the php-xml package on your server.
  • Replace in file parsers.php in plugin wordpress-importer at class WXR_Parser_SimpleXML

Replace in file parsers.php in plugin wordpress-importer at Class WXR_Parser_SimpleXML

Change

$dom = new DOMDocument;
$old_value = null;
if ( function_exists( 'libxml_disable_entity_loader' ) ) {
$old_value = libxml_disable_entity_loader( true );
}
$success = $dom->loadXML( file_get_contents( $file ) );
if ( ! is_null( $old_value ) ) {
libxml_disable_entity_loader( $old_value );
}if ( ! $success || isset( $dom->doctype ) ) {
return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
}$xml = simplexml_import_dom( $dom );
unset( $dom );

For:

$xml = simplexml_load_file($file);

WordPress – languages, translations – edit them!

I’m using Poedit to make the translations.
You can download it from their official website http://www.poedit.net/.

Well, lets start!

Locate your .po file of your theme folder.
I had to make some changes on it, with a text editor – I used coda -.
I had to change the “X-Poedit-SearchPath-0” to my correct path one or poedit crashed.

Make the translations!
You can hit Validate and Update but only when you close Poeditor the .mo file is updated!

I’v uploaded the new file to my theme_name/languages/ folder under pt_PT.po and pt_PT.mo.

This way I was seeing the translated items in the Admin board etc.

 

Add or edit a post to wordpress with PHP

Since 2010 that I’m using this script to post content on some of my 10s blogs.
I got it from WORDPRESS XMLRPC – POSTING CONTENT FROM OUTSIDE WORDPRESS ADMIN.

Add a post to wordpress with PHP

<?php

require_once("IXR_Library.php.inc");
$client->debug = true; //Set it to false in Production Environment

$title="Blog Title"; // $title variable will insert your blog title
$body="Blog Content"; // $body will insert your blog content (article content)
$category="category1, category2"; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
$keywords="keyword1, keyword2, keyword3";
$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format

$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

$content = array(
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => array($customfields)
);

// Create the client object
$client = new IXR_Client('Your Blog Path/xmlrpc.php');
$username = "USERNAME";
$password = "PASSWORD";

$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immediately, to save as draft set it as 'false'

// Run a query for PHP if (!$client->query('metaWeblog.newPost', $params)) {
die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
} else { echo "Article Posted Successfully"; } ?>

Edit a post in wordpress with PHP

Continue reading Add or edit a post to wordpress with PHP

Splash page on wordpress

In a recent work, with wordpress, needed a splash page before anyone enter in the *real* page. In the index.php of the wp root installation, this is what I have to have it working.

if( (strpos($_SERVER['HTTP_REFERER'], 'riadeaveiro.pt') === false) && !$_SERVER['QUERY_STRING']){
 include './splash/splash.php';
 exit();
}

/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');

So, you should replace riadeaveiro.pt for the domain where the wordpress is, and ./splash/splash.php for the path of the slash page that you want to present to your visitor.