Welcome to another episode of “How to WordPress.” How to WordPress is an ongoing collection of WordPress how to resources, tutorials, and videos aimed at making a better WordPress experience for both beginners and seasoned vetrans alike.
In this episode of “How to WordPress” we look at an all important first step: setting up a local installation of a web server using WAMP Server. There is also a MAMP server available for Mac users. What “setting up a local web server” means is that you can run a website or web application entirely on your own computer and have things run just as they would (or close to it at least) as if it were your live server that hosts your website out on the internet. So you could run an entire WordPress website entirely self-contained on your own personal machine. Your local setup is not exposed to the Internet, no nobody can access your site or see anything you’re doing.
Setting up a local web server is great for development, testing, and experimenting with anything else that you might want to do before you push something live to your production server on the Internet. This particular video is more of a general overview of the installation process, but it’s still a very important first step in getting WordPress set up locally.
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:
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.
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.
In what follows, we’re going to take a look at how to create a Facebook Like Box in WordPress. If you create a Facebook Like box on the Facebook developer’s page, Facebook will give you some code to paste into your website. Something like the following…
This is an example of the iframe generated code, though there are a number of different types of code they’ll give you to choose from (HTML5, XFBML, etc).
You could certainly take the approach to paste this code directly into the PHP files in your WordPress theme where you want to show your Facebook Like box, but we’re going to do this in the form of a WordPress shortcode. That way we can pass in variable content to our Facebook Like box and we don’t have to update the raw code anytime we want to make a change.
Create a function in the location in your theme where you normally register your shortcodes. A lot of times this is the functions.php file but in the author’s humble opinion I believe that there are ways to keep your code more organized. Whether you are using functions.php or a class, add the following function to your code wherever it seems good to you…
What we are doing in our function is allowing for a number of different variables to be passed into our shortcode.
id: This is where we can set the “name” of your Facebook box. It is used as an identifier if you want to have multiple different Facebook boxes on the same page. It can have any single word string value but each should be unique because (as you can see in the code), it maps to a CSS ID selector.
path: This where you’d want to se the URL of the Facebook page you want people to “Like”. The default we have set is http://www.facebook.com/platform, but you’d want to change this to your own page.
colorscheme: This setting could have the string value of either “dark” or “light”
showfaces:
bordercolor: Set the color of the border here. The default value is “white.”
We are now all set. Once you have your variables all configured the way that you want them, to use our newly implemented shortcode you could just add the following to your page or post…
Shortcode Sunday! The time when we provide a semi-useful WordPress shortcode to kick your week off on a good note! This shortcode will allow you to display your most popular posts in the last 20 days or 30 days or 60 days or any number of days that you want to specify. The way that it determines what is “popular” is by the number of comments that the post has received. You will also be able to specify a limit to the number of popular posts you want to show.
So go ahead and add this function to wherever it is you are registering your shortcodes in WordPress…
function popularPostsShortcode($atts, $content = null) {
extract(shortcode_atts(array("limit" => '4', "days" => '60'), $atts));
global $wpdb;
$sql = $wpdb->prepare("SELECT comment_count, ID, post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN DATE_SUB(NOW(), INTERVAL $days DAY) AND NOW() ORDER BY comment_count DESC LIMIT 0, $limit");
$posts = $wpdb->get_results($sql);
$list = '<div>';
foreach ($posts as $post) {
setup_postdata($post);
$id = $post->ID;
$title = $post->post_title;
$count = $post->comment_count;
if ($count != 0) {
$list .= '<a href="'.get_permalink($id).'" title="'.$title.'">'.$title.'</a> ('.$count.' comments)';
}
}
$list .= '</div>';
return $list;
}
Then all you have to do is register this shortcode after declaring the function…
And if you want to use it in a page or post somewhere you’d just have to call it like so.
Limit refers to the maximum number of posts you want to show and days refers to the number of days prior to the current day that you want to get popular posts for. So this shortcode would get the top 5 most commented on posts in the last 60 days.