If you’re involved in WordPress development, a challenge that you’re going to face sooner or later is how to include JavaScript files efficiently.
In this tutorial, I’m going to show you the best way to do that.
Let’s say you have a plugin that adds a custom shortcode. The shortcode needs some JavaScript code which requires jQuery.
If you’re just starting out with WordPress development, you might be tempted to write something like this:
Read on…
Lots of goodies in this release, based on the feedback and requests I got from you guys (and gals).
The Menu Format
First off, there’s a new format called “menu”. It’s basically the year and month menu from the fancy format, but without the post list. It’s meant to be displayed on year and month based archives, to aid navigation.
Usually, those archives are handled by the archive.php template in your theme, so that’s where you should use it:
<?php smart_archives('format=menu'); ?>
New arguments
There are also two new arguments:
month_format – can be one of ‘numeric’, ’short’ and ‘long’
posts_per_month – set the number of posts displayed per month; by default, all posts are displayed.
<?php smart_archives('posts_per_month=25'); ?>
Finally, you can now customize the look and feel more easily. See Advanced Tweaking.
Since version 1.9, you can make modifications to the plugin output, without having to modify the plugin code. This way, your tweaks won’t be lost when you upgrade the plugin.
Custom styling
If you want to customize the appearance of the archives using your own CSS, here’s how you do it:
- Add this code to your theme’s
functions.php file:
add_filter('smart_archives_load_default_styles', create_function('', 'return false;'));
- Copy everything from
smart-archives-reloaded/inc/styles.dev.css into your theme’s style.css file.
- Change the CSS in your theme’s
style.css file, as needed.
Custom markup
If you want to go one step further and change the generated HTML markup, you can do that too (understanding of object inheritance is required).
First, take a look at the SAR_Generator class (in smart-archives-reloaded/generator.php).
In your theme’s functions.php file, you can extend that class, like so:
smart_archives_load_default_generator();
class SAR_Custom_Generator extends SAR_Generator {
protected static function generate_fancy() {
// your custom code here
}
protected static function generate_block() {
// more of your custom code here
}
...
}
Then, all you have to do is add the ‘generator’ argument to the template tag:
<?php smart_archives('generator=SAR_Custom_Generator'); ?>
The number of editable elements continues to grow with this version of your favourite inline editor for WordPress:
Besides tags and terms, you can now edit categories as well, using the same interface. They all have autosuggest enabled, to make it easier to work with.
Custom fields with multiple values can also be edited now. Here’s a code example:
<ul>
<?php
$values = get_editable_post_meta(get_the_ID(), 'my_key');
foreach ( $values as $value )
echo '<li>' . $value . '</li>';
?>
</ul>
Finally, you can now edit all widget titles, not just titles of text widgets.
Other enhancements:
- optimized script loading
- fixed issue with Internet Explorer
- fixed issue with comment paragraphs
- fixed issues with the
$post global
I was just reading this inspiring article called Five things Free Software taught me. One of the things that stuck with me is the idea that knowledge tends to spread, beyond any obstacle.
So instead of working to prevent this spread, why not encourage it? Why not get the ideas, capabilities, and functionality of any given piece of software out to as many people as possible and kindle the flame? There are many ways to make money, so why choose a method that requires investing just as much time and effort into making software that lots of people want to use as trying to prevent the usage of said software? It sounds counterintuitive and/or just plain stupid on paper, but is generally seen as the traditional and conservative way to do it. Physical products must be treated differently than knowledge.
Read on…
This post contains technical information for developers who want to add their own editable fields.
We’re going to enable any option to be editable from the front-end.
The filter
The essential element of each editable field is a WordPress filter. You can use either an existing filter, or a custom one.
We’re going to use a new one:
function editable_option($key) {
echo apply_filters('editable_option', get_option($key), $key);
}
As you can see, editable_option() simply wraps the value returned by get_option() in a filter.
In your theme, you can now write:
<p>< ?php editable_option('my_option'); ?></p>
<p>< ?php editable_option('my_other_option'); ?></p>
Read on…
The plugin toolkit has been getting a lot of useful classes recently, so I though a version bump was in order.
scbUtil
scbUtil is a collection of useful little functions that I was using in a lot of my plugins. My favourite is the debug() function. Then there’s html() and html_link() for HTML generation. There are several other useful bits in there, but you’ll have to discover them yourself.
scbRewrite
One of the areas I wasn’t comfortable working with was the Rewrite API. The scbRewrite class was distilled from a discussion on wp-hackers. It’s only job is to take the rewrite rules you specify and hook them in all the right places.
If you would like to “try before you buy”, you can browse the source here.
The more popular a plugin is, the more options it seems to have. This goes for themes too. Managing options is apparently brain-dead simple, using add_option() and friends.
However, when you find a plugin specifically wrote for cleaning up the Windows registry WordPress options table, you know it’s not all rainbows and unicorns.
People often complain that their options table is filled with cruft from previously installed plugins. How can this happen? The main reason is that those lazy developers are too distracted to take out the trash when you’re done with their plugins.
Because I’m a plugin developer too, ergo hard-working & focused, I wrote a class to help me with this problem. It’s called scbOptions and it’s a part of my little plugin framework.
It takes care of removing the options automatically when the plugin is uninstalled via the WordPress interface. It also encourages and makes it easier to work with consolidated options.
So that takes care of 2 out of the 10 most common mistakes. Do you have any tips for handling options better? Do share.
Straight from the lab, we bring you the latest and greatest in front-end editing:
Swap theme images
Currently, there is no easy way to change an image from your theme. You have to either override the image file through FTP or locate the code in your theme and modify it.
Wouldn’t it be nice if you could just double-click on the image you want to change and then just do it, without leaving the page you’re on?
Good news: now you can, with a little bit of tweaking. To make an image editable, you have to make a one-time edit to your theme.
Read on…
While frantically merging tags on a site using this plugin, I realised that it would be easier if you could use the checkboxes, instead of typing each tag by hand.
Now it’s possible. In the bulk action dropdown you will find the Merge option. When you select it, a new field pops up, where you can write the destination tag:

The old interface is still there, for cases where you have tags that are very wide apart.
Also, the plugin is now ready for translations.
Translations
Belorussian – FatCow
German – Natalya Pastukhova
Polish – Krzysztof Kudłacik