Bogomil Shopov

Human. Artist. Hacker. I care about free and open-source software(F/LOSS), cybersecurity, ethical design, privacy, and technology. 20+ experience in technology. 40+ in being human. I encourage people to become better humans with heavy metal and technologies. I also promote technologies to be considerate of humans. Let's see how it goes!

Mozilla / Open Technologies · July 1, 2010

WRT: How to post to your WordPress using Ajax (+code)

I just finished my “share2” WRT (Web Runtime Toolkit) widget. It allows me to post quick messages to my WordpPress blog or to any blog that uses MetaWeblog API.

Here are some key parts from the script:

Ajax function that sends the requests and gets the result

function ajExec(url, param, callback, isXML) {
    var req = new XMLHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4 && req.status == 200) {
            if(!isXML && req.responseText != null)
                callback(req.responseText);
            else if(isXML && req.responseXML != null)
                callback(req.responseXML);
            else
                callback(null);
        } else if (req.readyState == 4 && req.status != 200) {

            var err = "Error: "+req.status;
            alert(err);
            callback(err);
        }
    }
        req.open("POST", url, true);
        req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        req.send(param);

}

Let’s click on Publish button

function sendButtonClicked(event) {
     saved_username = widget.preferenceForKey("username");
	 saved_pass = widget.preferenceForKey("pass_s");
	 saved_tags = widget.preferenceForKey("tag");
	 saved_server = widget.preferenceForKey("server");
	 pub = blogField.getText();

	ajExec(saved_server,'metaWeblog.newPostMyBlog'+saved_username+''+saved_pass+'titleThis is a postdescription'+pub+'1',showAnswer,1);
}
}

The milestone was to find correct XML structure for the query, because the examples from WordPress website and other websites are all wrong. Please see the XML and it’s structure

Download this script

You can download the application and see how works from here..

Run it

The logo:

The Settings screen:

HOW?:
If you want to run it into your Symbian device – rename it to share2.wgz and upload it to your device. The Symbian will do the rest. Tested on Nokia N95.