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:
// must use a variable for page id
$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.
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:
- Think about what you would type into Google if you were looking for whatever information you’re about to post. Make it specific.
- 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.
- 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.
Updated August 29, 2010: I’ve updated this post to be compatible with the latest version of CodeIgniter. It’s currently compatible with 1.7.0 and later. If you are still running on an older version and want the old version of this code to match let me know; I will send it to you. When I updated the post I also reset the discussion since most of it was specific to the old code.
February 15, 2011 Calvin Froedge rightly notes in the comments that with the release of CodeIgniter 2.0 the old PHP-4-compatible constructor style no longer works. The code below is updated to reflect that. He also has some thoughts about internationalization with language files and storing the visitor’s language in the session variables. This should be back-compatible with old versions CodeIgniter as long as you’re running PHP 5 on your server.
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:
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.
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.