Some more information on the WordPress bug that I think that I’ve found #wordpress
Yesterday I wrote about a problem that I’m having with a WordPress plugin that I’m working on, and I said that I think it’s a bug in WordPress. Here are some more information about it.
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.
The problem
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.
Replies and comments
WordPress Digest
12 november, 2016 16:53WordPress Digest reposted this on twitter.com.
Michael McGinn
12 november, 2016 17:27RT: @synvila-Some more information on the WordPress bug that I think that I’ve found #wordpress blog.henrikcarlsson.se/2016/11/some-m… #waptug 41211
Mikael Jorhult
12 november, 2016 21:53Haft besök hela dagen men ska se om jag inte kan bläddra igenom koden. Vill minnas att jag stött på ett liknande problem tidigare.
Mikael Jorhult
12 november, 2016 22:39Fungerar koden via Press This om det är något annat Post Format? Typ image?
Henrik Carlsson
12 november, 2016 23:07Testade samtliga post formats nu och oavsett vilket jag väljer så returnerar get_post_format() en tom sträng via Press This.
Mikael Jorhult
12 november, 2016 23:14Även om du använder dig av $post->ID istället för $ID? Sneglat i core och Post Formats ska fungera med Press This.
Henrik Carlsson
12 november, 2016 23:25Det fungerar så tillvida att inläggen som publiceras får rätt post format. Det är bara det att det inte verkar vara satt i