What the plugin is supposed to do
The plugin should cross-post/syndicate any new post to Twitter. There are a lot of plugins already that does this, but I wanted a few special features.
- If a post has a title, post the title and a link back to the blog to Twitter. (If the title is too long for Twitter, truncate it.)
- If a post doesn’t have a title and is 140 characters or less, post all of it to Twitter.
- If it is more than 140 characters long, truncate it and post the truncated text plus a link back to the blog to Twitter.
So far so good. This all works as intended. There is however one more thing that it’s supposed to do:
- If I’m posting a link post, a link to the thing I’m linking to, not a link back to my site, should be posted to Twitter along with the title of my post.
This is accomplished by the use of Post Formats. I give every link post Post Format link.
The technical part
The plugin hooks into the publish_post action and early on in its code it checks
if ( get_post_format( $ID ) == 'link' ). If it returns true an XPath query retrieves the href of the first <a> element and uses this instead of the permalink in the Twitter post.
The XPath part works just fine in all my tests.
As long as I post from the WordPress Admin interface, everything works as intended. The problem occurs when I post using the Press This bookmarklet. When I post this way,
if ( get_post_format( $ID ) == 'link' ) never returns
true, regardless of the post format.
If I create the post using Press This but instead of publishing I save it as a draft and then publish from the Admin panel, it works as intended.
Do you have a solution?
If so, please let me know.
The code is available on GitHub but I’ve kept the repository private since I’m a little bit embraced by the code, but let me know if you’d like to check it out and I’ll add you to the repo.