Easy Embedding with oEmbed

Posted May 7, 2013

Copying and pasting embed codes used to be a regular part of sharing videos and other media, but thanks to oEmbed you can say goodbye to embed codes. oEmbed is a standard developed to simplify the process of posting content from one site onto another. Of course you could use a plugin to embed your media from your favorite sources, but oEmbed is the easiest solution and it's already built in. Rather than copying a long code, you just need the content's URL.

Once you've copied the URL of the item you'll be embedding, just paste it onto its own line in the post editor and you're done. Be sure you don't create a link, and if you're using a version of WordPress before 3.5 you may need to enable auto-embeds in Settings → Media. Here's a list of providers WordPress supports by default:

Plugins can be used to add support for additional providers. More information is available here.

WordPress.org vs WordPress.com

Posted May 6, 2013

It usually takes newcomers to WordPress a little while to understand the difference between WordPress.org and WordPress.com, but it's an important distinction to understand when setting up your site or requesting support. WordPress is blogging software, and like any piece of software you'll need a computer (more specifically a web server) to run it on.

WordPress.org

WordPress.org is the home of the free open source software project. When anyone refers to "self-hosted" WordPress, this is what they're talking about. You'll need a hosting provider, domain, and a willingness to learn. Many hosts will offer an automatic installer, but even manually installing WordPress is easy enough for the average computer user. Once you're running a self-hosted copy of WordPress, you have full control of your website. You'll be able to install or create your own themes and plugins to achieve almost anything you want to do.

WordPress.com

Setting up and managing a site using WordPress.com is as easy as having a Twitter or Facebook account. There's no need to worry about things like updates and backups, but there's also a lot less flexibility. Your choice of themes and plugins is limited to those offered by WordPress.com. If you're looking for the easiest way to get a blog online, this is the way to go. If you decide to go with WordPress.com, I'd recommend paying for the upgrade to a custom domain. Not only will your site seem more professional, but it will also be easier to move your site in the future without losing visitors and links.

Head to Head

Self-Hosted (WordPress.org) Managed (WordPress.com)
Some setup required Easiest setup
Manually upgrade, easy to use backup plugins Automatic upgrades and backups
Thousands of themes to choose from or build your own using PHP and CSS Hundreds of themes, paid upgrade for custom CSS
Choose from thousands of free and premium plugins to extend your site's functionality Features limited to included plugins

If you've never used WordPress before, WordPress.com is a great place to get set up quickly and give it a shot. You'll probably find everything you need if you're just planning to share some posts with a few friends for fun. If you're looking to build a more complex site or you're the do-it-yourself type who enjoys learning about the web, setting up a self-hosted installation is a fun challenge and offers more ways to create a truly unique site.

For more information on WordPress.org vs WordPress.com click here.

Understanding WordPress Permalinks

Posted May 6, 2013

One often overlooked but highly important aspect to consider when building a new website is your link structure. Changing the link structure on an established site can result in broken links, so try to pick something you'll be happy with for a long time and only make changes when you really have to. So what's a permalink? Permalinks are links that will typically always point to a specific piece of content, unlike a category or tag link where the article might get pushed off the first page.

Rather than having a link like http://refactored.co/?p=80 which is enough for a server to know what to display (and handy if we need a short URL), we want our links to look something like http://refactored.co/blog/understanding-wordpress-permalinks so that real people can know the contents of a link before clicking it. WordPress uses rewrite rules to translate these human-readable links into which page needs to be displayed.

Setting up your link structure is as easy as going to Settings → Permalinks and choosing from some of the common settings or entering your own. If you choose to create your own, there are several tags you can use in the structure like %postname%, %year%, %category%, and %author%. Once you have your structure set up, save the changes. Usually this is all you'll need to do, but WordPress may give you further instructions about updating your .htaccess file, so I recommend checking out this guide if you're having any issues.

You may have read warnings in other tutorials to always begin your permalinks with a number (like the year or post ID) to avoid performance issues, but as of WordPress 3.3 this issue has been fixed.

You should now see that the links on your site are using the format you've chosen, but you may want to make some additional changes to the slugs for certain post names, categories, or tags. A "slug" is the link-friendly version of a title, which means that it's all lowercase, doesn't have special characters, and uses dashes instead of spaces. These get generated pretty well automatically, but occasionally you'll find a reason to tweak them. Sometimes I'll use an ampersand (&) in the title of a post, but because it's a special character it gets removed from the slug. In these cases I'll manually edit the post's permalink to include the word "and." For example, a post titled "Cats & Dogs" would have the permalink's slug automatically set to cats-dogs, so I'd change it to cats-and-dogs. If you want to change the slug for a category or tag just visit the respective section in the WordPress dashboard. There you'll be able to edit an item and customize the slug.

The last thing to know is that simply visiting the Settings → Permalinks page will flush the rewrite rules. This is something that you probably won't need to do often if ever, but if you're getting 404 errors after creating a new custom post type or installing a plugin that uses custom URLs you may need to do this. Typically plugins that need to do this will flush the rewrite rules when they're being activated or deactivated, but it's worth knowing this trick in case a developer forgot to automatically do so. I've had to waste time remembering this myself a few times after creating a custom post type.

I'll be writing an advanced tutorial soon for developers looking to create their own rewrite rules, but for know let me know if you have any other questions or tips about permalinks.

More information on using permalinks is available on the WordPress Codex.

Getting Started with the WordPress Plugin API

Posted May 2, 2013

There may come a time in your WordPress career when you decide it's time to write your first plugin. Whether you can't find a plugin that does exactly what you need or you just feel the urge to get into coding, don't be afraid of giving it a shot. You probably won't be writing a forum or caching plugin right away, but the sooner you get started the sooner you'll be making progress.

WordPress provides hooks for plugins to extend and modify its default behaviors. The two types of hooks are actions and filters:

  1. Actions are functions that get triggered during certain events like 'save_post'
  2. Filters are used to modify information being saved to the database or displayed to the user

Today we'll be learning how to write our first plugin by using a filter to modify the title displayed for breaking news posts. News sites sometimes add "BREAKING" to the title of certain posts, and instead of doing this manually we'll learn how to automate the process. When we're done, you'll be able to tag a post with "breaking" and for the first 24 hours the title will automatically be prefixed with "BREAKING."

WordPress uses PHP, so you may want to learn a bit about its syntax, but it doesn't take long to get the basics and can be picked up from examples.

The first step to building a new plugin is to create a new file in wp-content/plugins/. We'll call it breaking-news-titles.php. You'll need an FTP client, but if you installed WordPress yourself you're probably familiar with FTP.

Next open up the file in a text editor. Since we'll be writing in PHP, all of our code should be wrapped inside PHP tags.

<?php

// Our code will go here.
// (lines beginning 2 slashes are comments in PHP)

?>

The first thing we'll be putting into our plugin file is some information telling WordPress about the plugin.

/*
Plugin Name: Breaking News Titles
Plugin URI: http://refactored.co/blog/getting-started-with-the-wordpress-plugin-api
Description: Dynamically add BREAKING to the title of select new posts
Author: Refactored Co.
Author URI: http://refactored.co
Version: 0.1
License: GPL2
*/

Now that WordPress knows about our plugin, we'll need to add a function that can modify our titles if it needs to. This function takes two arguments, $title and $post_id, and returns the title we want to display.

// Breaking news title filter
function refactored_breaking_news_title( $title, $post_id ) {
    // Test if the post is tagged 'breaking'
    // and if it was published in the last 24 hours
    if ( has_tag( 'breaking', $post_id ) && time() - get_the_time( 'U', $post_id ) < 60*60*24 ) {
        // Modify the title
        $title = 'BREAKING: ' . $title;
    }
    // Return the title
    return $title;
}

You'll notice that there's an "if" statement in our function. We're using this to make sure we only modify the titles of the posts we want. This not only tests if the post has the tag "breaking," but also tests to make sure the post is less than 24 hours old. To check how old the post is, we're subtracting the time the post was published from the current time. To make things easy, we're using Unix time which is measured in seconds. We then check to make sure it's less than the number of seconds in a day (606024). If both of these conditions are true, we'll prefix the title with "BREAKING:".

Now that we have our filter function written, we need to tell WordPress about it.

// Add our filter to 'the_title'
add_filter( 'the_title', 'refactored_breaking_news_title', 10, 2 );

At this point you should have a working plugin! Here is it all together:

<?php

/*
Plugin Name: Breaking News Titles
Plugin URI: http://refactored.co/blog/getting-started-with-the-wordpress-plugin-api
Description: Dynamically add BREAKING to the title of select new posts
Author: Refactored Co.
Author URI: http://refactored.co
Version: 0.1
License: GPL2
*/

// Breaking news title filter
function refactored_breaking_news_title( $title, $post_id ) {
    // Test if the post is tagged 'breaking'
    // and if it was published in the last 24 hours
    if ( has_tag( 'breaking', $post_id ) && time() - get_the_time( 'U', $post_id ) < 60*60*24 ) {
        // Modify the title
        $title = 'BREAKING: ' . $title;
    }
    // Return the title
    return $title;
}

// Add our filter to 'the_title'
add_filter( 'the_title', 'refactored_breaking_news_title', 10, 2 );

?>

Hopefully you've been able to follow along and learn a little about the WordPress plugin API and PHP. If you don't have an idea for your own plugin yet, you could try expanding our breaking news plugin a bit. You could have the "breaking" tags remove themselves after the 24 hours or create some type of tags that would apply to your site.

If you're having any trouble or want to start getting more advanced, check out Writing a Plugin on the WordPress Codex where you can find many other helpful resources for your journey.

Getting Started with Images in WordPress

Posted May 1, 2013

One of the many tasks of managing a website is adding images to your content. Since most people won't like the idea of using an FTP client to manage multiple sizes of every image, you'll need to get familiar with the WordPress Media Library. This article will focus on how it handles images, but don't forget that it can also be used to store documents, audio, and video. Most of this tutorial may be common sense, but hopefully it shines some light on pieces that can be a little confusing. WordPress 3.5 made some major improvements to the Media Library, so if you aren't on the latest version it's worth an upgrade!

Drag and DropAdding images to your posts isn't hard, just click "Add Media" on the editor screen then you'll be prompted to either select a file to upload or drag and drop your files. I personally prefer the ease of dragging and dropping since I usually have the image file ready.

Once you've uploaded the image you'll have the option to give it a title, caption, alt text, and and description. You may have to scroll down to see it, but then it gives you options for how you'd like to display the image in your post. You can choose how to align the image (if alignment isn't working check that your theme's CSS has all the WordPress classes), if you'd like to link it to something (directly to the media file, to a page containing the file, or to a custom link), and the size you'd like to display the image.

Media details

Depending on the size of the image you've uploaded, you'll notice the options for thumbnail, medium, large, or full. Full is exactly what it sounds like. The other sizes can be customized in by going to Settings > Media from the WordPress dashboard. WordPress automatically generates a file for each size (as long as the dimensions are smaller than the original - it doesn't upscale) when you upload an image. Your theme or certain plugins may also tell WordPress to generate additional sizes for things like featured images or retina images. Even though every image in your media library will have multiple files because of the different sizes, most hosting plans offer enough storage that you probably won't run into any problems. Some hosts do restrict the number of files in a directory, so be sure that in your media settings you have "Organize my uploads into month- and year-based folders" enabled. If your host is warning you about folders with too many files, there are a few plugins that allow more detailed customization for organizing your media into subfolders.

Once you have everything set how you'd like, you can click "Insert into post" and WordPress will add the necessary HTML into your post.

You might have also noticed that there's an option to create a gallery. This might be something you'll need occasionally, but I'd recommend testing out some gallery plugins if it's something you plan on using on a regular basis.

WordPress also has the ability to set a featured image for a post which can then be used around the site to represent that post. You'll see a "Featured Image" section on the post edit page if your theme supports this feature.

Soon I'll go into some more advanced ways to take advantage of the Media Library, so keep an eye out if you're a veteran user looking to get started with your own themes or plugins.

Hello World!

Posted Apr 30, 2013

WordPress makes publishing on the web easy, and we're here to make doing more using WordPress easy. Our plugins and tutorials are here to help give more power to casual users and help fellow developers improve their workflow.

Check out our current lineup of plugins and keep an eye out for tutorials. We already have a few more plugins on the way that were created during the process of building this site.