tags:

views:

1308

answers:

1

Help, I'm trying to create a new post in my wordpress blog with custom fields using the following perl script using metaweblogAPI over XMLRPC, but there seems to be an issue with the custom fields. Only the second custom field (width) ever seems to get posted. Can't get the "height" to publish properly. When I add another field, I get the "Odd number of elements in anonymous hash" error. This has got to be something simple - would someone kindly sanity check my syntax? Thanks.

#!/usr/bin/perl -w
use strict;
use RPC::XML::Client;
use Data::Dumper;

my $cli=RPC::XML::Client->new('http://www.sitename.com/wp/xmlrpc.php');

my $appkey="perl"; # doesn't matter
my $blogid=1; # doesn't matter (except blogfarm)

my $username="Jim";
my $passwd='_____';

my $text=<<'END';

This is the post content...

You can also include html tags...

See you!
END

my $publish=0; # set to 1 to publish, 0 to put post in drafts

my $resp=$cli->send_request('metaWeblog.newPost',
$blogid,
$username,
$passwd,
{
  'title'       => "this is doodoo",
  'description' => $text,
  'custom_fields' => {
    { "key" => "height", "value" => 500 },
    { "key" => "width", "value" => 750 }
  },
},
$publish);

exit 0;
+6  A: 

While techically valid syntax, it's not doing what you think.

'custom_fields' => {
    { "key" => "height", "value" => 500 },
    { "key" => "width", "value" => 750 }
},

is roughly equivalent to something like:

'custom_fields' => {
    'HASH(0x881a168)' => { "key" => "width", "value" => 750 }
},

which is certainly not what you want. (The 0x881a168 part will vary; it's actually the address where the hashref is stored.)

I'm not sure what the correct syntax for custom fields is. You can try

'custom_fields' => [
    { "key" => "height", "value" => 500 },
    { "key" => "width", "value" => 750 }
],

which will set custom_fields to an array of hashes. But that may not be right. It depends on what send_request expects.

cjm
CJM, you rock! Your suggestion worked. Both custom fields published just fine ;) Much thanks!
Jim

related questions