One of the aspects of WordPress that people really like is the ability to add options. This is something that WordPress theme and plugin creators use heavily. They’ll create an options page or pages so that anyone who uses their theme or plugin can customize numerous different components of the theme… everything from look and feel, to contact form data, to portfolios, to just about anything else under the sun. Creating an option is as easy as storing adding an option:
<?php add_option('nbs_background_color', '#FFCC66'); ?>
Then to use an option in your webpages you just need to call get_option…
get_option('nbs_background_color')
Note: Notice that I used the prefix “nbs” (for Nine Bit Studios) before my option name. This is a common practice to prevent potential collision with other options because each option has to have a unique name. It’s very possible that another plugin that I have installed may have used the option “background_color”. By adding a prefix this is no longer an issue.
That’s all well and good, but there are a few potential downsides to this. For one, depending how your options are set up, it can be a fair amount of work to add, update, or remove options because you have to edit the code to register the option, edit the form for the user to set or update the option, edit the code to handle the values they set, etc. Hopefully, you have your theme or plugin options set up such that making changes is as smooth and easy as possible. It’s not a difficult process, but it can get a bit involved as the number of options that you want to handle grows in size and complexity.
Read More »
I love jQuery Isotope from Metafizzy. As a longtime fan of the Springfield Isotopes, who wouldn’t love an isotopes jQuery plugin? I’ve enjoyed it so much that I bought the developer license so I could use it for my own commercial applications and themes. It’s an outstanding plugin and a lot of the popular templates and themes on sites like Theme Forest use it to create things like filterable portfolios or display content in other dynamic ways.
The Isotope homepage is awesomely flashy and shows some slick implementations on how you can use the plugin. However, because there is a lot going on there on the page, I think there’s the potential that it might be a bit challenging for users who are new to jQuery to find the simplest way to use it in the code examples. There is a lot of styling and code to sift through. Perhaps, it comes down to a matter of personal preference, but I’m more of a fan of the “Start really simple and plain and then build up from there” approach. So I thought I’d show a really simple, very vanilla example of jQuery Isotope so that you can get it up and running in your own applications as soon as possible. You can add more complexity and styling from there if you so desire. The demo of what we’ll be creating and the corresponding download are below…
View Demo Download Files
We’ll be creating a filterable portfolio-style implementation using jQuery Isotope.
Read More »
Spam is inevitable. It’s just a reality of life on the Internet. And if you have a blog or a website running WordPress and you open up your comments form, spam is going to come your way. Fortunately there are great tools such as the WordPress staple Akismet or Antispam Bee that you can use to catch spam in your spam filter. But even though these sorts of tools provide an extra layer of protection, I also think it’s still a decent practice to to moderate all comments because there are times when those spammy comments can still sneak through.
What is the downside to this? Well for one, you’re going to have to be actively monitoring your blog spam filter because there is always the possibility that some actual legit comments get caught in the filter. But if you get a lot of spam, this isn’t really feasible. Who want’s to sift through 10,000 spam comments about larger penises or Louis Vitton handbags on a daily basis. If you’re into those things maybe you do… but for most, they’d probably rather not.
Fortunately there are ways to reduce the number of spam comments that actually get through into your filter. Before I implemented the following approach, I used to get hundreds and hundreds of spam comments a day in my spam filter. After this though, I reduced the amount that came through to around 10% of what it was. On that note, I just want to reiterate though that, this is not a bulletproof solution. You are still going to get some spam (so don’t go setting your WordPress settings to “Allow all comments all the time” just yet). But what this can do is provide a little extra validation to stop some of the the dumber bots from continually hammering your comment form submission system. Less spam to sort through is always a better situation overall. Nobody wishes for more spam.
We are going to add a nonce to our WordPress comment form to provide some additional validation. A nonce is as stated on the WordPress codex…
The nonce field is used to validate that the contents of the form request came from the current site and not somewhere else. A nonce does not offer absolute protection, but should protect against most cases. It is very important to use nonce fields in forms.
You can find more information on the general use of nonces elsewhere on the web. They’re not specific or unique to WordPress.
Read More »
In the following segment, we are gong to look at views in Backbone.js.
The files for demo and download for the following discussion can be found below if you’d like to follow along…
View Demo Download Files
What is a View?
As the name suggests, a “view” has to do with something you “view” on your screen (wow, what a concept!) :). But if you have done any programming in other languages before and you’re familiar with the Model View Controller software design pattern, the same pattern that Backbone.js follows (albeit very roughly), sometimes you have to take a moment to think about what is being referred to when talking about a view. Often times when you think of a “view” in an application framework such as CodeIgniter, ASP.NET MVC, or Django you tend to think of an entire rendered page of output (at least I do). Yes there are things like “partial views” and other components of MVC that don’t necessarily mean that a view is an entire rendered page, but that is just the tendency that I gravitate towards. When you are returning a view in frameworks such as those, you are often doing so in response to a browser’s request and routing them to a particular web page.
Read More »
In what follows we are going to look at 2 different JavaScript methods used when dealing with functions: apply and call. Apply and call are essentially used to call a function elsewhere in your code * using the arguments of another function as the arguments for calling that other function *. So if you were inside a function and needed to do some sort of process that figured out something about the arguments of your current function, you could use apply or call. The only real practical difference is that “apply” passes the arguments as a single array and “call” passes each of the arguments individually as separate arguments.
Let’s take a look at the following…
function calculateMyTotal(number1, number2){
return number1 + number2;
}
function sumArguments1(num1, num2){
return calculateMyTotal.apply(this, [num1, num2]); //passing in array
}
Here you can see we have called the calculateMyTotal function and have used the arguments of the current function as the arguments to pass into another function. The “this” just means this current function — in this case, sumArguments1. Another powerful and useful feature of using the apply and call methods is the ability to change what the “this” pointer points to. When you use apply and call, “this” becomes the function/object you are passing into the apply and call methods. It may not be immediately apparent from our simple demonstrations, but it can be very useful if used properly in other instances.
So, getting back to our example, if we were to do…
sumArguments1(1,2);
sumArguments1(4,8)
The results would be 3 and 12.
Because JavaScript functions all have an arguments property (which is a sequential array-like object of the arguments passed into the function), you could just as easily say…
function sumArguments1 (num1, num2) {
return calculateMyTotal.apply(this, arguments); //passing in arguments object
}
You don’t even necessarily have to use apply on “this” current function. What if we had another function…
function favoriteNumbers(num1, num2) {
return 'My favorite numbers are ' + num1 + ' and ' + num2;
}
Now, elsewhere in your code, we could pass favoriteNumbers in as the function. You could say…
calculateMyTotal.apply(favoriteNumbers, [3,4]); // needs pass in array as 2nd argument
This will give you 7 even though favoriteNumbers is a function that has nothing to do with adding 2 numbers together.
The call method is similar, but instead of using an array as the second argument, you have to use each argument individually. So if we were to do the same as our previous example we could do…
calculateMyTotal.call(favoriteNumbers, 3,4); // passing each number as a separate argument
Which will also give us 7. It’s the same result, but which one you want to use depends on what kind of function you are trying to pass into apply or call (or it might even come down to personal preference). Either way, you can see that you can use a function’s arguments and apply them to another function, or call another function using them.