Skip to content

Instantly share code, notes, and snippets.

@szbl
Last active December 21, 2018 05:56
Show Gist options
  • Save szbl/5924483 to your computer and use it in GitHub Desktop.
Save szbl/5924483 to your computer and use it in GitHub Desktop.
Quick Singleton class to be included in WordPress plugin (or theme functions.php file) that will create a simple Person post type and shows some methods of encapsulating functionality in a class as a "namespace," as well as applying filters to things, allowing other users to extend your code.
<?php
class Sizeable_Person
{
const POST_TYPE_SLUG = 'szbl-person';
public static $instance;
public static function init()
{
if ( is_null( self::$instance ) )
self::$instance = new Sizeable_Person();
return self::$instance;
}
private function __construct()
{
add_action( 'init', array( $this, 'register_post_type' ) );
add_filter( 'enter_title_here', array( $this, 'enter_title_here' ) );
add_action( 'save_post', array( $this, 'save_post' ) );
}
public function enter_title_here( $title )
{
if ( self::POST_TYPE_SLUG == get_post_type() )
$title = 'Enter First and Last Name';
return $title;
}
public function register_post_type()
{
$args = array(
'label' => 'People',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'supports' => array( 'title', 'editor', 'custom-fields' )
);
/*
The following line allows other developers to modify the
post type settings via plugin – super flexible!
*/
$args = apply_filters( 'szbl_people-post_type_args', $args );
register_post_type( self::POST_TYPE_SLUG, $args );
}
public function save_post( $post_id )
{
// do something on save, such as store custom meta data
}
public static function get_person_by( $field, $value )
{
$args = array(
'post_type' => self::POST_TYPE_SLUG,
'orderby' => 'menu_order',
'order' => 'asc',
'posts_per_page' => 999
);
switch ( $field )
{
case 'name':
case 'title':
return get_page_by_title( $value, OBJECT, self::POST_TYPE_SLUG );
break;
case 's':
$args['s'] = $value;
break;
// Assumes we're looking for post meta save by this plugin
// and that the keys have a prefix of "szbl_person_"
default:
$args['meta_query'] = array(
array(
'key' => 'szbl_person_' . $field,
'value' => $value
)
);
break;
}
$args = apply_filters( 'szbl_people-get_person_by_args', $args );
return get_posts( $args );
}
}
// initialize all this code
Sizeable_Person::init();
/*
As a Singleton, we're only going to have 1 object in memory, ever,
which is all we need!
Now we can call something like this from anywhere to
get data from this object as well such as the get_person_by()
function...
Example:
$person = Sizeable_Person::get_person_by( 'name', 'Bill Burr' );
$person = Sizeable_Person::get_person_by( 'email', 'andy@sizeableinteractive.com' );
Returns:
Object (post) where ( $person->post_type == 'szbl-person' )
$person_email = get_post_meta( $person->ID, 'szbl_person_email', true );
*/
@philipdowner
Copy link

Great class! Can't wait to use this in a project. Just curious though, why you would set 'posts_per_page' to 999 rather than -1 on line 60? Any significance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment