jQuery and Ajax in WordPress Plugins – Public Pages

My previous post teaches you how to use jQuery and Ajax for the administration pages in your WordPress plugins. To use them in your user-facing pages requires a few changes.

We’ll use here a simlarly contrived example. Let’s say you use <!--more--> in your longer posts so they don’t fill up too much of your page. Normally, clicking the “Read more…” (or whatever text you use) link takes the user to a separate page with the complete post. In our example, rather than sending the reader to a new page, we’ll make an Ajax request to get the rest of the post and insert it directly into the current page. Continue reading “jQuery and Ajax in WordPress Plugins – Public Pages”

jQuery and Ajax in WordPress Plugins – Administration Pages

This is a quick overview of how to use jQuery and its Ajax functions in WordPress. To get the point across, I’ll use a simple and contrived example. We’ll have an admin screen with a list of categories. Clicking on the name of one of the categories will fetch a list of titles of posts in that category and display them as a sub-list of that category. Continue reading “jQuery and Ajax in WordPress Plugins – Administration Pages”

Issue Manager WordPress Plugin

Since we started the Code4Lib Journal, we’ve had a persistent problem of editors accidentally publishing articles early (i.e., before the issue was officially published). Our first workaround for that was to remove the ‘publish_posts’ capability from the editor role. With that capability removed, only the administrator (i.e., me) could publish articles.

That worked, but it was a hassle, as it required me to do what should have been the coordinating editor’s job of publishing each article when it came time to publish a new issue. For that matter, even if permissions weren’t an issue, the publication process would still be cumbersome: open every article in the issue, change its status to “Published”, and save it, doing them all as fast as you can so people didn’t find a half-published issue when they came to the website.

It was to fix these and other problems that I created the Issue Manager WordPress plugin. It connects the publication status of an article to the category it’s in. You can designate a category as “Unpublished”, so that no articles in that category can be published. If you try to publish an article, it’s status changes to “Pending Review”. When it’s time to publish an issue, you change its category’s status to “Published”, which publishes all the “Pending Review” articles in that category. Continue reading “Issue Manager WordPress Plugin”

Save My <pre> WordPress Plugin, or, WTF-8 My Whitespace

If you’ve tried to use code samples in WordPress, you might have noticed unexpected behavior from the WYSIWYG editor. It will edit the whitespace inside of <pre> tags, leaving you with some poorly formatted, hard-to-read code. Today, I set out to stop that.

What causes this?

Whenever the WYSIWYG editor opens, it sends the content of the post through a few filters. You can find most of these in wp-includes/formatting.php. The culprit here is the function wpautop. This function runs a long list of regular expressions to make your content a little prettier and better formatted. But we don’t want that to happen to our <pre> tags; they’re pre-formatted. We want those left alone. To do that, I had to find a way to keep the content of the <pre> element from going through that filter.

WordPress’s plugin system allows you to change its behavior without altering the core code, so you don’t have to re-alter it every time you upgrade. As easy as it would have been, in the short-term, to just edit wpautop to make it behave properly, I wanted a longer-term solution that would be easier to share with other WordPress users. Therefore, a plugin. Continue reading “Save My <pre> WordPress Plugin, or, WTF-8 My Whitespace”