View Sidebar

Post Tagged with: WordPress

WordPress Theme Design Service

WordPress Theme Design Service

If you need a custom WordPress theme design, you are in the right place. Whether you want it done from scratch, or you want to convert your existing web site or add a themed blog to your site, I am your source.

Click for more information
March 15, 20120 commentsRead More
Implementing Custom Post Types with a Custom Plugin

Implementing Custom Post Types with a Custom Plugin

This post tells the story of my journey of learning about WordPress custom post types, playing with them, preparing a meetup presentation on them and implementing them on a client website. I ended up with a custom plugin file that contained all my code for registering, administering and using my custom post type on the site.

I first used the standard method of adding code to my theme’s functions.php file and creating a page template to display my post data. Justin Tadlock has a great tutorial for this. That method worked just fine for a while.

The problem with this method is that when/if I change themes, my custom post type code is gone because a new theme has its own functions.php file and page templates. I would have to edit the new theme to include my code and customizations.

I then came across an slide deck by Mark Jaquith where he mentioned using the Custom Post Type UI plugin and a custom plugin that creates a shortcode for displaying my custom data. This allows custom post types and display of data to be portable from theme to theme.

My goal with a client site was to create a way to list Graduates of the orthodontics department of the University of North Carolina’s school of dentistry (see www.uncoaa.com). I needed an easy way for a site editor or admin to enter new graduates and have them automatically appear correctly in the graduates list.

Creating the Custom Post Type

I installed the Custom Post Type UI plugin and used it to create a Graduate post type with just a few mouse clicks. Note that I did not check “Custom Fields” as a supported item even though I am using a couple of custom fields. Supporting custom fields adds support for the custom fields dialogue section on the add/edit page of the dashboard. I can still add/edit custom field values with code, which you will see later.

Creating a new custom post type

Below are the some of the advanced options selecting what your new post type supports.

Advanced Options

You can also create custom taxonomies with this plugin.

Creating the Custom Plugin

In my /wp-content/plugins/ folder, I created a .php file to store my plugin code. Note that if you do this, you need to give the file a very unique name for its function and your site so you avoid conflicting with any plugins you install later. I start this file with plugin header text.

/*
Plugin Name: UNCOAA Graduates
Description: Creates custom post type "Graduate" to store graduate info (name,username,year graduated); short code to list graduates; admin page setup
Version: 1.0
Author: David Sullivan
Author URI: http://davidsullivan.net/
*/

Shortcode for Data Display

I tailored Mark Jaquith’s code to fit my needs and added it to my plugin file. This code allows me to simply put the shortcode [graduates] on any page and have my data list displayed.

// [graduates] list of graduates Short code
function uncoaa_graduate_list() {
	global $post;
	$old_globals = $GLOBALS;
	$graduates = new WP_Query(
		array(
			'post_type' => 'graduate',
			'posts_per_page' => 50,
			'orderby' => 'meta_value', 'meta_key' => 'uncoaa_year_graduated',
			'order' => 'ASC'
			)
	);
	$return = '';
	uncoaa_css_hack();
	while ( $graduates->have_posts() ): $graduates->the_post();
		$uncoaa_username = get_post_meta($post->ID, 'uncoaa_username', true);
		$uncoaa_year_graduated = get_post_meta($post->ID, 'uncoaa_year_graduated', true);
		if ( username_exists( $uncoaa_username ) ) :
			$userID = username_exists( $uncoaa_username );
		else :
			$userID = 0;
		endif;
		$return .= "
";
		if(function_exists('userphoto_the_author_photo'))
		{
			$return .= userphoto__get_userphoto($userID, USERPHOTO_FULL_SIZE, "", "", array(), "");
		}
		$return .= "
"; $return .= get_the_title(); $return .= " - ". $uncoaa_year_graduated . "
";
		$return .= get_the_author_meta( 'user_description', $userID );
		$return .= "
";
		$return .= "";
	endwhile;
	$GLOBALS = $old_globals;
	return $return;
}
// end [graduates] list of graduates Shortcode
Screen shot shortcode on page Graduates listed on a page

Now if I change themes, this page will still display my graduate list without requiring me to create a custom page template.

Customizing the Admin Panel

Next, I wanted to customize the Admin panel when adding or editing my graduates so that my custom fields would show up just how I wanted them. Devin Price has some great tips for this.

Admin Panel for Graduates

Notice above the format that my custom field “Year Graduated” is in.

One Less Plugin

After displaying some of this work at a local WordPress Meetup, a comment was made about limiting the number of plugins to limit server overhead. I thought about the Custom Post Type UI plugin’s feature to “get code” that displays the code to generate the types and taxonomies you created with the plugin. I copied my graduate type’s code and pasted that into my custom plugin. Then I deactivated the CPTUI plugin. So now, all the work for my own custom post types is done solely in my one custom plugin. If I ever need to edit settings for the graduate type, I could just activate CPTUI, do the edits, get code and paste to my plugin and then deactivate CPTUI again.

Activate My Plugin

All that is left now is to activate my plugin. Then on any page where I want to display my Graduate data, I just drop in the [graduates] shortcode. If I create editor or administrator role users, they can easily go in to the dashboard and add new graduates.

Activating my Custom Plugin

One other note: I had a list of over 300 graduates to add. I was not about to attempt entering all these by and. Instead, I used the CSV Importer plugin seen above to import the list right into WordPress as a Graduate post type including my custom fields.

In Summary

To accomplish what I did, you can do these steps:

  • create a my_unique_file_name.php file in the /wp-content/plugins/ folder
  • add in appropriate plugin header text
  • add code to register your custom post types
  • add code to create a shortcode that you can use to display your data on pages
  • if you wish, add code to customize the admin panel for your post types
  • activate your custom plugin

I hope this helps someone or inspires something even better.

- Peace and long life

      

September 21, 20111 commentRead More
Introducing Trapezio

Introducing Trapezio

www.trapezio.com

www.trapezio.com

Since June 2009 I have been employed by the Academy of Orthodontic Assisting. Yesterday we announced a new name – Trapezio. Personally, I like that it’s easier to say and write, and it sounds cool. The AOA has been a great company, both as an employer and as a school to train orthodontic staff. But we have re-branded in order to create a broader identity to boast a range of training offerings beyond clinical assistant certification. Next I’ll be working with the Trapezio educational staff to build in the additional e-learning courses on other topics.

My last few weeks have been primarily focused on developing a new company website. As I am not a designer or graphic artist, the company hired a third party designer to create the new logo and website concept art. We hired another party to slice the site art and build a foundation WordPress theme. I took those assets and ran with them. The www.trapezio.com site is a full WordPress CMS. So many of the WordPress features provided opportunities to automate functions.

Most of the site consists of Pages. There are two page templates: the home page and the main default. Each page displays a sidebar and main content. At the bottom of each page, I list that page’s child pages.

I created custom Category templates to handle a few items using the category-2.php function of WordPress. A custom category template is used for the News page to list Press Releases that are blog Posts of the news category. That template also displays Links added under the In The News link category to list outside references to the company. I also use custom category templates for a Job Postings page and a Posted Resumés page. In addition to the custom category templates, I use custom Single Post templates using single-2.php naming convention. This was achieved by adding a filter to the functions.php page based on a post on Nathan Rice’s blog.

I use several handy plugins that I will list below.

  • I am using a Redirect plugin by Nick Berlette to make a couple of sidebar and nav menu pages redirect to custom Category pages.
  • I use TDO Mini Forms by Mark Cunningham to allow job and resume listings to be added as Posts with custom fields.
  • I use Contact Form 7 by Takayuki Miyoshi to have multiple custom “contact us” type forms that email our staff
  • I use WP Super Cache by Donncha O Caoimh to improve site performance

Being a WordPress site lends to easy site updating, automated RSS feeds and the power of plugins. We received great fanfare from clients and friends upon the release of the site and new name. Now on to the hard work ;)

September 3, 20090 commentsRead More