Scott Nellé.com

Archive for the 'Tutorials' Category

Load A Page Into A WordPress Theme Outside Of The Loop

Wednesday, September 23rd, 2009

I build a fair number of WordPress sites on small budgets for my employer, Union Street Media. Sometimes I need to give our clients an editable region in the sidebar, away from the main blog/page content. Here’s a quick and dirty trick I use to pull a page into a theme outside of the loop:

 <?php
// must use a variable for page id
// http://codex.wordpress.org/Function_Reference/get_page
$id = 3;
$p = get_page($id);
echo apply_filters('the_content', $p->post_content);
?>

You can get your page ID by editing the page. It will show up in the url (the ‘post=x’ portion.) It’s important to note that you have to pass a variable to the get_page() function. If you just pass an integer it will throw a fatal error. No need to go into why that is; just keep it in mind. get_page() essentially wraps get_post() so that function’s documentation is a good place to start if you want to learn what’s available to you.

This isn’t a particularly pretty solution, but it’s quick and it works well provided you know your page IDs and you’re not making a theme for distribution. I like to name my page something like ‘**Sidebar Content’ so it is easy to differentiate from regular pages.

Blog Post Title SEO–The Only Tip You Need

Tuesday, December 30th, 2008

Quick tip: Search engines are designed to work for people, not the other way around. They’re designed to pick results that are likely to be what the searcher is looking for, and for a blog the post title is an important part of that. If you want people to find your blog post in a search engine and click through, do the following:

  1. Think about what you would type into Google if you were looking for whatever information you’re about to post. Make it specific.
  2. Put that in the title. You can add some more detail to make the title more “punchy,” but make sure your search phrase is in there.
  3. There is no step 3.

If a searcher sees exactly what she typed in the title of a search result she’s going to click. The trick is that your content has to support the title you picked. Otherwise, even if you trick the search engine, your site visitor will take one look and hit the back button.

Updating Unique Field Validation for CI 1.7.0

Tuesday, November 4th, 2008

Just a quick post to keep my CodeIgniter unique field validation extension up to date. When EllisLab released CodeIgniter 1.7.0, they included a new form validation class which is meant to replace the old validation class. The old class still exists in the code for now but it’s been deprecated, so developers are urged to use the new form validation class instead. Only very minor updates are needed to my validation extension in order to make it work with the new code. These were suggested to me by commenter libre before I even had a chance to look at implementing them myself. (Thanks libre!) For everyone’s benefit, here’s the updated version of the library (and remember, use this if you’re running CodeIgniter 1.7.0 or later:)


<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_Form_validation Class
 *
 * Extends Form_Validation library
 *
 * Adds one validation rule, "unique" and accepts a
 * parameter, the name of the table and column that
 * you are checking, specified in the forum table.column
 *
 * Note that this update should be used with the
 * form_validation library introduced in CI 1.7.0
 */
class MY_Form_validation extends CI_Form_validation {

	function My_Form_validation()
	{
	    parent::CI_Form_validation();
	}

	// --------------------------------------------------------------------

	/**
	 * Unique
	 *
	 * @access	public
	 * @param	string
	 * @param	field
	 * @return	bool
	 */
	function unique($str, $field)
	{
		$CI =& get_instance();
		list($table, $column) = explode('.', $field, 2);

		$CI->form_validation->set_message('unique', 'The %s that you requested is unavailable.');

		$query = $CI->db->query("SELECT COUNT(*) AS dupe FROM $table WHERE $column = '$str'");
		$row = $query->row();
		return ($row->dupe > 0) ? FALSE : TRUE;
	}
}
?>

You’ll want to save this file as system/application/libraries/MY_Form_validation.php (instead of MY_Validation.php.)

When you load the library, don’t forget that it’s now called form_validation instead of just validation. There have also been some changes to the way you set your rules for the new form validation class. You can read all about this in the class documentation. Here’s the basic format, including usage of the “unique” rule from the extension:


$this->form_validation->set_rules('username','User Name','required|min_length[5]|unique[users.username]');
$this->form_validation->set_rules('emailaddress','Email Address','required|valid_email|unique[users.email]');
		

Extending CodeIgniter’s Validation Library To Check For Unique Values

Thursday, September 11th, 2008

Updated November 4, 2008: This extension has been updated slightly to work with CI’s new form validation class, added with version 1.7.0. If you’re using an older version of CI you should still use the extension on this page. If you’re using 1.7.0 or later, you should check out the updated extension.

I’m working on small web application and I’m building it with CodeIgniter. Like most web applications, mine requires user registration and login. And like most login systems, mine is happiest if each user has a unique username. While the CodeIgniter validation library is pretty robust, it doesn’t come with a function for checking a value to see if it is unique or if it already exists in the database. Fortunately there are a couple of ways to remedy that.

The first way is outlined in the documentation for the validation library under the heading “Callbacks: Your own Validation Functions.” I tried this and it works well, but it requires you to write the functions in your controller or model. I thought that looked messy and I wanted something a little more streamlined, so I decided to extend the validation library with my own function. Extending CodeIgniter’s libraries is pretty easy. Here’s what I came up with–I’ll explain the important lines afterward:

(more…)

Introduction to Ajax with Mootools

Sunday, December 23rd, 2007

This is a basic tutorial that offers an introduction to the Ajax component of the mootools javascript library. At the end of this tutorial you’ll be able to use an HTML form to send requests to a PHP script and return a response without refreshing the page. This basic process is the foundation for most of the ajax that you see littered around the internet today. Mootools offers a simple way to get your feet wet with this technology, and plenty of room to grow once you know what you’re doing.

This tutorial is something of a followup to one that I wrote some time ago, describing the same basic process using the moo.ajax class, a precursor to mootools. That article still gets a fair number of visits on my site, but since moo.ajax has been deprecated it isn’t very useful. This tutorial expands on that one a little, but is essentially an update. By now everyone has heard of ajax, so there’s no need to repeat the introduction. Let’s get started.

What You’ll Need

  • Some way to run PHP scripts. Many web hosts offer this. You can also set up a web server on your personal computer, which will make the development process much more simple.
  • A copy of the mootools library. I’ll show you how to get a customized version that meets all of our requirements.

(more…)

Introduction to Moo.Ajax

Tuesday, July 25th, 2006

Update: Moo.Ajax has become deprecated since I originally wrote this article, as its functionality has been incorporated into the new mootools library. I’ve written a follow-up piece that explains this same basic principal using the more current library. I’d suggest checking that out if you’re interested in a dead-simple introduction to ajax.

This is a very basic tutorial which offers a glance at the moo.ajax class from Mad 4 Milk. It doesn’t expand too much on the tutorial that they provide, but I feel that it will help clarify a few things. Don’t let the length of this article frighten you, what you are about to learn is pretty simple. I just use a lot of words to explain it. By the time you’ve worked through this tutorial, you will be able to build a form that interacts with a PHP script and returns data, without refreshing the page. It’s a pretty simple process that can serve as the basis for some very cool applications.

(more…)