I’m curious if it is possible, in a plugin/custom code, to take an URL that has been generated by the currently running WordPress site & parse it to retrieve the $blog_id and $post_id?
ie. taking a string like “myblog.mysite.com/2012/03/16/whatever/” and determining that this is post #6 (“whatever”) on blog #2 (“myblog”).
I realize that I can probably strip the URL and parse the domain myself, but I hope that there might be recommended WordPress methods exposed to make this more robust across URL formats.
As Weston mentioned, if you’re within WordPress and you know what blog the URL came from, then ask WordPress for the blog ID.
But if you’re really, really sure there’s no better method in this case than parsing a URL, then here you go:
Reference material:
http://codex.wordpress.org/Function_Reference/get_id_from_blogname
http://codex.wordpress.org/Function_Reference/url_to_postid
Plain and simple, to get the post id you just use:
That will echo out the post id.
And:
Will echo out the blog id.
http://codex.wordpress.org/Function_Reference/get_the_ID
and
http://codex.wordpress.org/Function_Reference/get_current_blog_id
respectively
The best solution I’ve found for this is the Monkeyman Rewrite Analyzer plugin. It was written by a member of the WPSE community in response to a few similar requests some time ago.
You can give it any URL for your site, and it will tell you explicitly how that request will be mapped/interpreted by WordPress.
For example: