Create a Fully Custom Post in WordPress

Author: | Posted in Open Source, Wordpress No comments

Create a Fully Custom Post in WordPress

I love WordPress as a framework, but sometimes my business partner and I need to literally gut the contents of a post and use our own code. This is a common problem for us, and the typical workaround is a shortcode to call a particular function.

But sometimes we want more absolute control with our code embedded directly into the page. This particular issue came up recently, so my partner came up with a process that we now use exclusively when it’s time to get aggressive with a custom feature.

Why Would I Need a Custom Post?

Here’s a scenario I run into regularly: my client needs a highly customized process that requires a form and unique database tables. Instead of being limited to a plug-in – and there are some really great ones out there – I have to develop a completely unique process just to keep the website branded as well as readable. And we all know how important website readability is, no matter if you are using WordPress or another content management system.

It’s a lot easier if you can just create a PHP script, add it to a post, and call it good. The alternative is to be completely dependent upon plug-ins that are either already available or to make your own. The following are the steps you need to take to create your own custom post in WordPress:

Step 1 – Custom Posts Function

The first step is to tell WordPress to check the ID of a post and, if there’s a custom version available, to use your file. Log into WordPress and go to Appearance -> Editor. Select the functions.php file, and add this code:

[sourcecode language="php"]
/* Creates a path for custom post templates and looks for a post ID */
define(SINGLE_PATH, TEMPLATEPATH . ‘/single’);
add_filter(‘single_template’, ‘my_single_template’);

function my_single_template($single) {
global $wp_query, $post;

if(file_exists(SINGLE_PATH . ‘/single-’ . $post->ID . ‘.php’))
return SINGLE_PATH . ‘/single-’ . $post->ID . ‘.php’;

}
[/sourcecode]

Step 2 – Custom Posts Path

This code requires that you make a folder in your template path file. This is going to be something like: yoursite.com/wp-content/themes/{yourtheme}/single

Of course, you can define your own path by modifying the code in Step 1. Instead of the call to TEMPLATEPATH, you can use either a hard-coded path or even one of many that WordPress has built in. Here’s an example that points to the wp-includes folder:

[sourcecode language="php"]
/* Creates a path for custom post templates and looks for a post ID */

$wp_includes_url = includes_url(); // Uses the built-in WP includes_url function

define(SINGLE_PATH, $wp_includes_url . ‘single’); // Note that there’s no forward slash in front of single anymore

add_filter(‘single_template’, ‘my_single_template’);

function my_single_template($single) {
global $wp_query, $post;

if(file_exists(SINGLE_PATH . ‘/single-’ . $post->ID . ‘.php’))
return SINGLE_PATH . ‘/single-’ . $post->ID . ‘.php’;

}
[/sourcecode]

No matter how you set up your path, make sure there is a folder so you can start putting your custom files into it and WordPress can find them.

Step 3 – Create a Post

Now it’s time to create the first post you want to use. Do the usual method. Log into WordPress and go to Posts -> Add New. Leave it blank and click Publish.

That’s it! You’ll have a blank Post at this point, but that’s what we’re going for.

Step 4 – Find the Post ID

The easiest way to find the ID of the post, which we need for the next steps, is to log into WordPress and go to Posts. Hover your mouse over the Post you just created in Step 3 and look at the URL in your browser. In Chrome, here’s what you’re looking for:

Create a Fully Custom Post in WordPress

In the example above, the Post ID is 198. You’ll need this number shortly.

Step 5 – Download the single.php File

The easiest way to make sure your posts retain the general layout of your WordPress theme is to start with the single.php file already in your theme’s folder. I use FTP to download this file, but you can also go to Appearance -> Editor and find the single.php file.

If you are using the FTP method, simply rename this file to single-{post-id}.php. Using my sample from Step 4, my file name would be single-198.php.

If you used the Appearance -> Editor technique to find your single.php file, simply create a blank file on your computer and name it single-{post-id}.php and paste the code from WordPress into the file. You now have a file you can edit to your heart’s content and upload when you’re ready.

Step 6 – Create a Simple Test

My single.php file is very simple, so I want to keep most of what the theme file already has. But for the sake of testing, let’s strip the file down to almost nothing just to demonstrate how this process can work.

Here’s the content of my single-198.php file:

[sourcecode language="php"]

<?php get_header(); ?>

<h1>Test Post</h1>
<p>Here’s my test post with custom content.</p>

<?php get_footer(); ?>

[/sourcecode]

Put whatever code you want into your file. You now have complete control. In the above example, I retain my WordPress header and footer content so the post looks like the rest of the site.

Step 7 – Upload Your File

You now simply have to upload the single-{post-id}.php file to the path you created in Step 2. You’re going to have to use FTP or your domain hosting service file manager tool. Just navigate to the folder you created and upload the new file.

Step 8 – Test Results

Now just go to the post you created on your WordPress site! You should see your code. Using my sample code from Step 6, here’s what I got:

custom post - Test Results

Step 9 – Troubleshooting

It’s inevitable that some of you are going to run into problems, so here are the things we’ve found that tend to cause issues:

  • Check your path – in steps 1-2 we defined the custom path. Make sure you actually have the folder in place and you’ve uploaded your file to the right folder.
  • Check your post ID – sometimes we add the wrong post ID to the single-{post-id}.php file name, so WordPress doesn’t see our custom post.

Now you have a powerful tool for quickly creating completely customized posts in WordPress. You’re not dependent upon anything WordPress does, although you still have access to all the power under its hood.

Have fun and let me know if this helps you out!

Author Bio

 has a degree in English and has found her niche writing about marketing, advertising, branding, web and graphic design, and desktop publishing. She writes for PrintPlace.com, an online printing company that offers booklet printing, business cards, flyers, posters, postcard printing and mailing, catalogs, and more printed marketing media. In addition to her writing career, Tara also enjoys spending time with her husband and two children. Connect with @TaraHornor on Twitter.

Add Your Comment

You must be logged in to post a comment.

como comprar viagra viagra cialis levitra which is best viagra price turkey http://www.tutoriallounge.com/what-if-a-girl-takes-viagra/ viagra drug name