Category: WordPress

WordPress is the most popular CMS in the world. As a core Committer, I’ve been actively building it for a decade. I like to look at the community around WordPress and ways that you can benefit by being a part of it.

WordPress Widget – Exclude some categories

I’ve often found a need for a simple replacement to the WordPress category widget that allows me to remove some of the categories. I whipped this up real quick for a client and thought others might find it handy. You can copy and paste it or download it from Google Code. Comment with any questions or suggestions.

 'Selective categories', 'description' => 'Show a list of Categories, with the ability to exclude categories' );
                $control_ops = array( 'id_base' => 'some-cats-widget' );
                $this->WP_Widget( 'some-cats-widget', 'Selective Catagories', $widget_ops, $control_ops );
        }

        function form ( $instance){
                $defaults = array( 'title' => 'Catagories', 'cats' => '' );
                $instance = wp_parse_args( (array) $instance, $defaults );
                ?>
                

"; wp_list_categories("exclude=$cats&title_li="); echo ""; echo $after_widget; } } function register_jorbin_widget(){ register_widget('limited_catagories_list_widget'); } add_action('widgets_init', 'register_jorbin_widget'); ?>

WordPress Shortcodes: A how to by example

I love the WordPress shortcode api. I’m willing to stand on a roof and scream it. It makes so many aspects of developing a WordPress site so much easier. It allows you to add dynamic information to any post, page, and with a single line text widget. How easy you ask? Let’s look at some examples so I can show you just how easy it is to use.

A shortcode for this year

Sometimes you just want to include the year inline and there’s no reason you should have to change the blog posts every time you want to do it. Here’s a quick shortcode for the year.
[php]function show_current_year(){
return date(‘Y’);
}
add_shortcode(‘show_current_year’, ‘show_current_year’);[/php]
Just that easy. As you can see there are a few components to a basic short code. The first is a function that returns what we want the shortcode to return and the second is a call to add_shortcode with the first parameters being what we want the shortcode to be and the second is the function we want that shortcode to call.

A custom links list shortcode

So that last one was pretty easy. Maybe we should try something a little harder, like adding an unordered list of links that uses each links image. This time we also add an attribute to allow us to limit it to a specific category.
[php]
function show_bookmark_image_list($atts){
extract(shortcode_atts(array(
‘catagory_name’ => false
), $atts));
if ( $catagory_name == false )
$bookmarks = get_bookmarks();
else
$bookmarks = get_bookmarks("catagory_name=$catagory_name");
ob_start();
echo "<ul class=’link-image-list’>";
foreach($bookmarks as $bookmark){
echo "<li><a href=’".esc_url($bookmark->link_url) ."’><img src=’".esc_url($bookmark->link_image)."’ /></a></li>";
}
echo "</ul>";
$list = ob_get_clean();
return $list;
}
add_shortcode(‘show_bookmark_image_list’, ‘show_bookmark_image_list’);
[/php]
This time we added an attribute called Catagegory_name that we can then retrieve using the shortcode_atts function that also allows us to set a default incase no attribute is set. We then go through the list of bookmarks that we retrieve and echo out the results. Since we want our shortcode function to return the data, we wrap our echo is an output buffer. I prefer to use output buffers because it allows me to use echo (and makes the code more readable in my opinion), but you can also use string concoction and return the string if you want.

Most Recent Tweet

This was removed in August 2013 since it relied upon a non deprecated twitter api

Using Shortcodes inside Text/html widgets

Text widgets can be simple and are great for creating small blocks of content that you can place any where your theme allows. This simple hack will allow you to use shortcodes inside your text widgets. Just add the following single line to your theme’s functions.php file.
[php]add_filter(‘widget_text’, ‘do_shortcode’);[/php]
If you want to download and use these three simple shortcodes and enable the use of shortcodes in your text widgets, download this file, remove the .txt at the end of the file, upload it to your plugins folder, and activate the simple shortcodes plugin.

Those three simple sample shortcodes for WordPress should give you a bit of a base to create your own. What are your favorite shortcodes? What’s a shortcode that you wish existed? Comment below and let me know. Also let me know if there is any part of my code that you don’t understand, and I’ll try to help you understand it.

UPDATE

I’ve added two more twitter shortcodes. You can also download all of my shortcodes and follow along to the development of them on google code.

You Better Know the Basics

When you first start working with WordPress, you need to know that there are five languages you could be interacting with. You don’t need to be an expert at any of them (and in fact can want make amazing sites knowing none of them), but when you want to make a change to your theme or add a feature that no plugin exists for, it’s important to understand what each language is and how it fits in.

To start let’s take one step back and understand that WordPress is software that runs on a server and that allows users to manage content, and the content to be sent to a visitors browser.

Just had to get that little definition out there.  Now that we know that, let’s actually look at the four languages.

HTML – Hypertext Markup Language

This is the main form that the WordPress outputs the content (if we want to get specific, WordPress uses XHTML 1.0 for all it’s internal functions, but explaining that is beyond the purview of this document). HTML is a structured language that browsers can read to display a web page. Content is enclosed in tags that each have a specific meaning. Tags can (sometimes optionally and sometimes mandatory) contain a variety of attributes. An example of a tag is
[html]<a href="http://aaron.jorbin">Aaron Jorbin is cool</a>[/html]
Learning at least basic HTML is important if you want to be able to display content in a web browser. To learn more, I recommend:

CSS – Cascading Style Sheets

As the name hints, CSS is for styling a page. CSS allows you to style html elements. In WordPress, a theme’s style.css file is also the theme’s way of telling WordPress about the theme. If you open any theme’s style.css you notice that the comments at the top are structured in a key: value manner. CSS utilizes the ID’s, Classes and tags from your HTML. An example of some css is:
[css]#header ul{
width: 400px;
color: #990099;
border: 1px solid #009900;

}[/css]
This block will style all unordered lists that are inside an element with the header ID and give it a width of 400 pixels, color all the text with hex color 990099 and apply a 1 pixel solid border of the color 009900.

JS – JavaScript

Once the page is sent to a browser, if you want to make changes to how the page looks, you’re going to need to have the browser execute it. That’s where JavaScript comes in. JavaScript is primarily a client side scripting langauge. This allows it to change the way the page looks, such as by having a set of tabs that change the content of a panel or having an accordion that grows and collapses to show or hide some options. Am example of JavaScript is:
[javascript] $(document).ready(function(){
$(‘#contentheader’).click(function () {
$(‘#contentheader > h2’).toggleClass("hidden");
$(‘#contentheader &gt; h3’).toggleClass("hidden");
} );
});[/javascript]
.

PHP –PHP: Hypertext Preprocessor

This is where the bulk of WordPress is written and the bulk of where the magic happens. WordPress includes a large amount of functions that plugins and themes can use from tasks as simple as echoing a posts title to creating a custom taxonomy for posts. If you’ve ever opened up your theme’s index.php file, you’ve seen text that looks like:

[php]
<img src="<?php header_image(); ?&gt;" width="<?php echo HEADER_IMAGE_WIDTH; ?&gt;" height="<?php echo HEADER_IMAGE_HEIGHT; ?&gt;" alt="">
[/php]

The is from the header.php file from Twenty Ten and controls the display of the header images.

To learn more about PHP, I recommend the following resources:

SQL – Structured Query Language

This is the language used to speak with Relational Databases.   In WordPress, this is used to add, retrieve, update or delete a post or option.  As the name would suggest, it’s a very structured language.  You say what you want to do, to which table you want to do it and how you want it to go down.  An example of sql is:
[sql]SELECT `option_value` FROM&nbsp;`wp_options` WHERE `option_name` = ‘siteurl’ LIMIT 0,1;[/sql]
Running this query will tell you what WordPress thinks your site’s url is (if you didn’t change your table prefix).    Some more resources for understanding SQL are:

I hope these resources give you a better idea of the languages used inside WordPress.

WordPress 2.9

WordPress 2.9 was officially released today and contains 509 changes from 2.8. It continues WordPress’ development towards being the most extendable and intuitive web publishing platform. There are a lot of great developments in this release that excite me. Below I highlight three front end and three back-end changes that I’m particularly excited for.

  • User Enhancements

  • Integrating Image editing – Aaron Hockley from Social Photo Talk has this pretty well covered in a post and video. Check it out if you’re a photo blogger
  • Media embedding Alex aka viper007bond has been making embedding videos and other media easier for thousands of WordPress users with his quicktags plugin. Now you don’t even have to worry about adding a plugin. There are 14 providers included in the core that should cover many of the your needs. Adding another oembed provider is as easy as adding the following code to your theme’s functions.php file: wp_oembed_add_provider( ‘http://site.com/watchvideo/*’, ‘http://site.com/oembedprovider’ ); You can also allow any user with the unfiltered_html capability (just admins on a standard install) the ability to discover any oembed provider with the following in your theme’s functions.php file add_filter(’embed_oembed_discover’, create_function(‘$a’,’return true;’));
  • Trash – No more are you sure you want to delete this pop ups. No more “OH SHIT” moments because you deleted instead of published a post. Now, you can place any u wanted posts in the trash where they will sit for 30 days. You are welcome to change the length of time that posts are trashed by adding the following to your wp-config.php file define( ‘EMPTY_TRASH_DAYS’, 7 );
  • Backend Enhancements

  • Comment Meta – Comments now have a database table for storing all sorts of goodness. What can be stored here? Well really the sky is the limit. Comments are now almost as flexible as posts.
  • Themes can now control the boxes that show up in the post editor. Right now it just supports post thumbnails, but this is really just the start. If you want to enable post thumbnails, add the following your theme’s functions.php file add_theme_support( ‘post-thumbnails’ );
  • Database repair and optimization is now easier. All it takes is adding define(’WP_ALLOW_REPAIR’, true); to wp-config.php and then visiting yoursite.com/wp-admin/maint/repair.php to either repair or optimize your database.

This is just the tip of the iceberg on what’s changed and what I’m excited about. What are you most excited about in 2.9?  Download it and let me know what you think.