I'm new to both plugin development and XML-RPC.
I'm currently trying to re-work the seo-slugs plugin so that it removes the stopwords from the post slug when I'm posting via XML-RPC. Currently seo-slugs works properly when posting using the dashboard, but when I try to post using XML-RPC the stopwords aren't trimmed from the slug. I'm using the metaWeblog.newPost call to post via XML-RPC using a very simple script.
I've tracked the offending code to about line 2050 in the core wordpress file xmlrpc.php, inside the mw_newPost() method.
$post_name = "";
if(isset($content_struct["wp_slug"])) {
$post_name = $content_struct["wp_slug"];
}
We eventually need to get the trimmed slug value into $post_name, which is used later in the wp_insert_post() function around line 2235. It would appear to me that the easiest way to do that is to set the value of $content_struct['wp_slug'] via a filter or action hook. $content_struct is generated from the $args fed into mw_newPost().
There is an action hook near the top mw_newPost():
do_action('xmlrpc_call', 'metaWeblog.newPost');
which I have been trying to hook in to so that I can modify $content_struct['wp_slug']. Here's the content of my modified version of seo-slugs with my attempt at a hook:
// Add hook
add_action("xmlrpc_call", add_slug, 0, 1);
// This sets the slug
function add_slug($args) {
global $content_struct, $wpdb, $args;
$slug = strip_slug($content_struct['title']);
$content_struct["wp_slug"] = $slug;
return $content_struct;
}
// Slightly modified version of original seo_slugs() function
function strip_slug($slug) {
global $wpdb, $content_struct, $stopwords;
$seo_slug = strtolower($slug);
$seo_slug = preg_replace('/&.+?;/', '', $seo_slug); // kill HTML entities
// kill anything that is not a letter, digit, space or apostrophe
$seo_slug = preg_replace ("/[^a-zA-Z0-9 \']/", "", $seo_slug);
// Turn it to an array and strip common words by comparing against c.w. array
$array = explode(" ", $seo_slug);
$array2 = array_diff($array, $stopwords);
$seo_slug = join("-", $array2);
$seo_slug = ereg_replace(' ', '-', $seo_slug);
return "$seo_slug";
}
$stopwords = array(
'this',
'the',
'a',
// plus a whole list of stopwords
);
However, the problem I'm having is that content_struct isn't global outside the mw_newPost method and it can't be read or written to by add_slug(). If I edit xmlrpc.php manually to declare $content_struct to be global at the top of mw_newPost, then the code works and the slug is trimmed and saved correctly when posting via XML-RPC. However, I don't want to edit a core file, but would prefer to make a proper plugin so there are no problems with later updates to wordpress.
What am I doing wrong? How do I make the value of $content_struct readable and writeable without editing xmlrpc.php? I have also read about adding filters to xmlrpc methods (like http://josephscott.org/archives/2008/11/adding-xml-rpc-methods-to-wordpress/ ) - should I be trying to do this instead?