WordPress Tutorials

Learn something new every day with WPCandy WordPress tutorials.

properly enqueue scripts in WordPress  themes
There are a lot of themes out there. And a ton of them are not including javascript files properly. So theme developers, please pay attention: this is how to include scripts in your themes properly. And guess what? It’s really easy.

Let’s lay some groundwork:

  1. This example would go in your functions.php file.
  2. This example assumes your theme is a parent theme.

In this example, I’m registering four scripts, and enqueuing two. I’ll explain it afterward.

<?php
/*
* WordPress Sample function and action
* for loading scripts in themes
*/
 
// Let's hook in our function with the javascript files with the wp_enqueue_scripts hook

add_action( 'wp_enqueue_scripts', 'wpcandy_load_javascript_files' );

// Register some javascript files, because we love javascript files. Enqueue a couple as well

function wpcandy_load_javascript_files() {

  wp_register_script( 'info-caroufredsel', get_template_directory_uri() . '/js/jquery.carouFredSel-5.5.0-packed.js', array('jquery'), '5.5.0', true );
  wp_register_script( 'info-carousel-instance', get_template_directory_uri() . '/js/info-carousel-instance.js', array('info-caroufredsel'), '1.0', true );

  wp_register_script( 'jquery.flexslider', get_template_directory_uri().'/js/jquery.flexslider-min.js', array('jquery'), '1.7', true );
  wp_register_script( 'home-page-main-flex-slider', get_template_directory_uri().'/js/home-page-main-flex-slider.js', array('jquery.flexslider'), '1.0', true );

  wp_enqueue_script( 'info-carousel-instance' );
  
  if ( is_front_page() ) {
    wp_enqueue_script('home-page-main-flex-slider');
  }

}
?>

Now let’s break it down

Continue reading

The SEO overlords say “Thou shalt provide links on your 404 page”, or something to that effect. Consider this quick tip simply a word of caution for you power users.

Typically a WordPress 404 page will not be cached by your performance plugin of choice or even Varnish in most cases. And a typical 404 page template may look something like:

  • wp_list_pages( 'title_li=' );
  • wp_list_categories( 'sort_column=name&title_li=' );
  • wp_list_authors( 'exclude_admin=0&optioncount=1' );
  • wp_get_archives( 'type=monthly' );
  • wp_get_archives( 'type=postbypost );

All fair and good, right? 95% no harm no foul. Except in possibly these two following circumstances:

  1. Your site has 500, 1000, or even 10,000 posts/authors/categories/tags etc.
  2. Your permalinks get botched by a plugin, and everything but your homepage is 404′ing. (We see this at Page.ly from time to time; some plugins just love to rewrite permalinks — poorly.)

Continue reading

One thing I spend some amount of time thinking about each week is managing my multi-author blog at WPCandy. There are only a handful of authors that have joined WPCandy in the last year, but we’ve still spent a good deal of time improving our workflow as a multi-author blog. I’ve even picked up a few (what I would call) tips in the process.

If you find yourself in a similar position, hopefully some of these tips will help you along. If you run a blog with a number of authors, be sure to jump down into the comments and share your experiences too.

Continue reading

I always intend to write about how I build things around WPCandy, but when the time comes it doesn’t always happen. I fully intended to write about the creation and development of the Pros section, but then I didn’t. I started working on the discussion board, which in turn I never wrote about either. Around that time work began on The WPCandy Quarterly, which was only just announced the other day. Today, I’m determined to write about the building of the Quarterly micro-site at wpcandy.com/quarterly before moving on to any other projects.

When planning things out, I knew that I wanted to keep a trend going with the Quarterly that I had started with Pros and the forum: keeping everything on this domain, and simply adding new sections to the site. I’ve been avoiding subdomains and brand new sites for some time, really for the sake of simplicity and because I prefer the way it looks within the site. So I set about creating a micro-site just for The WPCandy Quarterly, which I’ll walk you through in this post.

In this post you can expect a smattering of code snippets, a good deal of me explaining my weird website preferences, and of course some bouncing between functions.php files and functionality plugins.

Continue reading

dribbble to wp autoblog

I ran across a fun code snippet today by Tammy Hart that automatically creates posts to a WordPress custom post type from your Dribbble RSS feed. The post type is called “shot” and all it requires is for you to change your Dribbble name in the fetch_feed( ) function.

In addition to Tammy’s creation of the Shot post type, I put together a simple archive template that you can use in your theme. I used markup consistent with the Twenty Eleven theme, and it simply pulls the meta data defined in the post type for the entry title and post image. It also links to the original Dribbble post.

You can get Tammy’s code from her post on Forrst, and the archive template after the jump. Thanks to Tammy for sharing her fun implementation of custom post types!

Continue reading

Permalink

Siobhan McKeown’s notes on running a local meetup

I haven’t done the best job as the organizer of the WordPress meetup in my city. Our last event was in March (eesh) and I don’t spend the time on it that I should.

Siobhan McKeown attended WordCamp Portsmouth and, as far as I can tell, did a killer job covering the event. One session McKeown covered, and published notes on, was a panel all about improving local WordPress meetups. Now I’m encouraged and have a list made to get back on top of our local event.

If you’ve read through part 1 of our introduction to WordPress Hooks, you should be well aware of what constitutes an “action” or “filter” hook and where they can be used. Today we’ll be exploring a little further down the rabbit hole, as we discuss pluggable functions and the concept of creating our own “action” and “filter” hook points, as well as how we can leverage these and create relationships between themes and plugins using various WordPress Hooks.

Note: This tutorial isn’t for the faint of heart (there are a few advanced bits). If you haven’t read part 1, I strongly suggest doing so before reading on.

Continue reading

A common, yet unfortunate practice in the WordPress community involves filling theme functions.php files with tweaks and functionality that is key to a site. The reason this is a bad idea, in short, is that it will tie your critical site functionality to a theme that will eventually change. Good news, though: there is a much better, smarter alternative. It’s called a functionality plugin.

We’re going to create a very simple, very specific functionality plugin that you can (hopefully) use to replace your theme’s functions.php file. After all, the poor guy deserves a break, doesn’t he?

Continue reading

WordPress hooks are arguably the basis of WordPress development, forming a large part of the core functionality and used by almost every plugin and theme available to date. The concept of hooks can also be somewhat daunting for users who are starting out with developing for WordPress. Today, we’ll jump in and find out a bit more about just what exactly WordPress hooks are and how they can help you on your way to WordPress rock stardom.

Continue reading