Adding a button
The other day I found a great post that explained how to add a custom button to the WordPress Visual TinyMCE editor. It was fairly simple and concise and even includes a zip file of all the parts you would need to install a button into WordPress’s visual TinyMCE editor. I’m not at all sure I could do a better job than that guy, so I’ll just direct you to the link above. :)
What you get when you don’t use an API
The current version of my WordPress OCD Plugin Stats plugin, is somewhat… clunky. When I wrote it I didn’t know that WordPress.org had an API for it’s plugin statistics. Thus, the current version essentially scrapes particular WordPress.org plugin pages for information and drops it into the WordPress dashboard.
After poking into the stats on one of my plugin pages the other day I discovered that the source code for the page included several calls to a WordPress.org API. Why should I bother scraping and interpreting a page of HTML when I can just ask WordPress.org to deliver the exact data I need in a nifty JSON format?! I’m very much looking forward to adjusting this plugin to use a more direct and simple call to the WordPress API rather than scraping plugin pages.
In any case, if you’re looking for documentation on this, I found a page in the WordPress Codex and a site with a page devoted to documenting the WordPress.org API.
While posting about my DrawBot adventures, I’ve become increasingly annoyed with my own post series WordPress plugin. If only it had a simple button in the WordPress editor to drop the shortcode for an existing series, it would instantly become so much better. Thus inspired, I delved into WordPress.org and sought out the answers. What I discovered is that while it’s not particularly difficult to add a button to the WordPress text editor, it didn’t seem to be very well documented. I’m still working on how to add a graphical button to the WordPress TinyMCE visual editor, so stay tuned for that. I was ultimately able to glean the necessary information from the WordPress “Quicktags API. However, for your sake I’m distilling what I’ve learned right here.
First, I’ll give you an overview of what we’re going to do. If you’re looking to add a button to the WordPress editor, it’s probably because you’re working on a plugin. Thus, I’ll assume you have a working knowledge of plugin development. Your plugin will need to include, at a bare minimum, one action hook and one function which performs the double duty of creating the button and giving the button some kind of ability.
// This action adds a script to the script footer on administrative pages
add_action( 'admin_print_footer_scripts', 'MBSS_quicktags', 100 );
// This is the function that is run at the end of the administrative pages in the script footer
// Optional database interactions
// I used this section in my plugin to run a query on the database and return information that would be used in the button
// Creates button in the text editor
QTags.addButton( 'MBSS_tag_id', 'Add Series', MBSS_add_series_js );
// Function to append the series shortcode to the end of the post
editor = document.getElementById('content');
editor.value = editor.value + "\n" + "EXAMPLE TEXT TO APPEND TO END OF POST";
Lovin’ that source code
While looking at the source code on my Simple Series plugin page, I began to wonder how WordPress.org was serving up all those nifty little stats into the downloads graph. ((Photo courtesy of Windell Oskay)) What I discovered is that there’s a little bit of jQuery magic going on that pulls download data from a WordPress.org API, formats it for niftiness, and inserts it into the dynamically draw SVG graphic of the downloads.
In case you’re interested, the API call looks like this:
I’m now wondering how I can use that data to show something interesting in my OCD Plugin Stats plugin.
This weekend, almost an entire year from the last update, I made some revisions to my Simple Series plugin for WordPress. This is one of the plugins that I use the most often for WordPress. It’s right up there, in terms of frequency, with the WP-Footnotes plugin that lets me insert footnotes.
As much as I loved this plugin, it was always a little bit of pain to use. In order for two posts to be associated together in the same series, the title must be listed exactly the same in each post. So, to make sure that I was getting it right, I would open an old post in the relevant series, copy the exact plugin shortcode with the exact title, and paste it into the new post.
With this update you can click the “Add Series” button in the TinyMCE text editor for a post and insert any series title you’ve previously used. It’s not yet as elegant as I would like, but it makes a few button clicks out of a what used to be an annoying process. I’ve also added the ability to have a series between pages, posts, or any combination of the two kinds of content types.
- This shrimp, it’s not so good as I once thought…
I’d really really like to love ShrimpTest. Apparently it is making the database on one of my sites continually puke errors. Great.
This kinda sucks a lot.
- Adds a caption, that includes the same text as the original photo on Flickr
- Centers the image, using WordPress’s tags
- Makes the photo credit part of the text. I like to include the photo credit using slightly different language. At some point I’ll get around to modifying the plugin so that I can save my format as a setting.
Admittedly, these implementations are just a little bit buggy – I just hacked those bits in without really doing any serious testing on them. Once I have kicked the tires on this code a little, I’ll post it to the plugin’s page. If you want to take a look at it before then, just drop me a line.
Sometimes the right post just needs the right picture – I’ve actually had a lack of a good photo hold up a post before. I’m happy
I’ve written a new WordPress plugin! It uses the Creative Commons API to grab the appropriate license based on your license choices. While there’s still room to improve the plugin, I’m pretty happy with the way it’s turned out. I’ve made a conscious effort to use best plugin programming practices – and learned a lot in the process.
You can download this Creative Commons WordPress plugin here!
Looking back at one’s code from years prior is like looking back at a junior high school picture of one’s self. I’m looking back at the code for my quick-and-dirty pie chart plugin and think, man, why did I write things THAT way?
In the 1,000 years since I wrote that plugin in 2009, I’ve been trying to learn and comply with best programming practices for WordPress plugins. As a result my current plugins tend to be stripped down, simple, don’t create unnecessary options, don’t create unnecessary tables, taxonomies, special post types, or those kinds of things. Learning some Object Oriented programming along the way has been super helpful. By encapsulating your WordPress plugin code into a chunk of objected oriented programming, you reduce the likelihood that your plugins’ function and variable names will collide with those from WordPress or other potential plugins.
If you’re getting started or need to brush up on your WordPress plugin development skillz, you should definitely check out these awesome articles:
I’d also recommend tinkering with jQuery and JSON, if you haven’t already. I don’t know of any really good JSON tutorials, so if you do, please let me know so I can add it to this list.
I’m really happy to report that per the WordPress.org stats, this little post series plugin has been downloaded more than 100 times so far. That’s really awesome. And, now that I’ve figured out how to work this wacky SVN thing, I think it would be fun to release some of the other random little plugins I’ve developed over the last few years.
As of the latest version 1.4, the plugin is now easy to modify with some CSS added to your stylesheet. I could have added this as a text field option a settings page for the plugin, but I really like the stripped down simplicity of the plugin as is. Even with all the comment lines in the plugin, it is only 53 lines of code. If super short code were a goal I could probably cut that in half.
Now I have to find some of my old plugins that others might find useful. I’ve got one for frame escaping, one for making pie charts… I know there are a few other random ones as well.