zend php and google webmaster api II : wordpress mu auto-register

Part Deux of automating the registration and verification of a wordpress blog. In the previous post I showed how to add a site to google webmaster tools.

Which site you ask ? Oh dear… in the previous post I did not mention how to create a new blog in wpmu :

$newblogid= wpmu_create_blog('tryout.blacknorati.com', '/', 'tryout', 1);

Very basic, assuming I am the admin user (with ID=1). After creating the blog, I post it’s url to google webmaster tools to start the registration. Then I want to

  • verify the site
  • add a sitemap
  • and blog on!

verifying a site

I can add any url to Google Webmaster Tools, but I only get to use the tools once Google are sure I ‘own’ the domain or subdomain. Verification is done by checking on the presence of a header metatag in the index file, or a specific file on the server. Once Google spots it, Google know I control the site and I can use the webmaster tools.

On a WordPress Mu install I do not, as user, get to have my own template. I currently have 100 standard templates installed to choose from, some with options and widgets and that should be enough. But editing the template itself is not possible for separate users, so I cannot verify sites with a header metatag.

The alternative is putting a file on the server with a particular codename, but users don’t have an actual separate subdomain with a wordpress Mu install, so that one also won’t work.

Eek ! Well, no problem, Google also accept a post with the filename in the url. Just blog a post with the google___.html filename as title, WordPress automatically turns the title into the url and you can use that post to have Google verify the site is yours.

getting the verification filename

A Google Webmaster Tools account has it’s own standard verification code and it’s valid for every site. Once a user registered the site with GWT, I can retrieve that code from the sites data feed :

function get_verification_title($domain, $client) {
		$myfeed = get_site($domain, $client);
  		foreach ($myfeed as $item) {
		$tags     = "";
        	$subjects = $item->{"wt:verification-method"};
        	if (is_array($subjects) and count($subjects) > 0) {
				return $subjects[1];

function get_site($domain, $client) {
		$fdata = new Zend_Gdata($client);
		return $result;

With the get_site function I retrieve the site’s atom list as zend feed. The feed contains two wt:verification-method tags, one for the metatag and one for the html-file. This function loads both in the $subjects array and i pick item[1] (it’s a 0 based array), the html file name. I need that one to go post on the new blog. Here is a php routine taken from Snipplr.

function add_verify_post($domain, $verification, $logon, $pass) {
	$req = 'title='. $verification . '&content=' . $verification . '&category=' . $category . '&logon=' . $logon . '&pass=' . $pass;
	$header .= "POST /remote_post.php HTTP/1.0\r\n";
	$header .= "Host: ". $domain."\r\n";
	$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
	$header .= "Content-Length: " . strlen ($req) . "\r\n";
	$header .= "Connection: Close\r\n\r\n";
	$fp = fsockopen($domain, 80, $errno, $errstr, 30);
	$SUCCESS = false;

	if (!$fp) {
		$status_message = "$errstr ($errno)";
		$res = "FAILED";
	else {
		fputs ($fp, $header . $req);
		while (!feof($fp) && $SUCCESS==false) {
			$res = fgets ($fp, 1024);
			if (strcmp ($res, "SUCCESS") == 0) {
				$SUCCESS = true;
				$last_line = $res;

	if ($SUCCESS == true){
		echo $last_line;

The remote_post.php code is the same as the snippet.

I am the owner of the blog so I can use the standard admin login and password in the function. For security purposes I’d use a different login and password for remote access though (this one does not use SSL).

With a simple call I send one new post to the new blog with the google verification file name as title.

add_verify_post('BlogSubdomain.blacknorati.com', 'google12345.html', 'MyLogin', 'MyPassword');

I had some doubts about google accepting blog.blacknorati.com/year/month/’google12345html but they actually accept it so I don’t have to adapt the permalink settings.

Now I have to send Google a ‘verify’ xml message,

function verify_site($domain, $client) {
	//domain without http
		$fdata = new Zend_Gdata($client);
		return $result;

presto, now Google know I control the site, and I can use the webmaster tools. That means I can add the sitemap. And that in turn means my sites are indexed a lot faster.

function add_webmap($domain, $sitemap, $client) {
	//domain without http

		$fdata = new Zend_Gdata($client);
		$myaddress= "https://www.google.com/webmasters/tools/feeds/".htmlentities(urlencode('http://'.$domain.'/'), ENT_QUOTES)."/sitemaps/";
		return $result;

Happy now. Google Webmaster Tools API was top of my wish-list. Now I can register and verify 32.000 sites with sitemaps automatically, so that saves me at least 2500 hours of work. And it was actually easier than I thought, with the proper examples and snippets available online.

I am going to clean up the code a bit and stuff it in a class, and move on to developing large scale ‘grey’ ops :)

Posted in php, wordpress and tagged , .


  1. wow … thnaks for the great article, I really really loved it. Bookmarked your blog, keep up the articles coming because I honestly find them very interesting.

    P.S: I also subscribed.

  2. Thanks for the article, really need it.

    I know it’s quite old but I got a problem with website verification. After I put my meta in the website and changed your script from html verification to meta with

    I got a Expected response code 200, got 400 Invalid request

    I tried a lot of solutions, including curl direct PUT request but I always got a 400 error. Any Idea?

    Thanks again

  3. Hi thanks for this code. Do you have sample implementation of Zend Framework on getting the ‘top seach queries’ and the ‘Links in my site’ data inside google webmaster?

  4. Pingback: Domain sitemap wpmu | Hi Tech Stuff Reviews & Updates

  5. I also use Zend to add site or verify site in Google webmaster tools. Add site is no problem for me. But I always get 404 error when I use your posted function
    function verify_site($domain, $client).
    Same issue like Andrea posted.
    You mentioned striping out crlf as the solution , but I don’t quite understand it.
    Can you tell me how I can change that verify_site function and get it work?

    Thank you so much

Leave a Reply

Your email address will not be published. Required fields are marked *