If you’ve been developing WordPress for a while, chances are you find yourself repeating the same code over and over. Creating custom post types, taxonomies, meta boxes, and generally structuring your content can all be broken down into reusable chunks. And that’s what the Piklist framework does. It gives you access to development shortcuts for getting your site off the ground.
What You Get with Piklist
Piklist makes common tasks much easier with a few useful functions and code snippets. For instance, Piklist gives you a new function to hook into when creating custom post types and taxonomies making robust settings — like adding labels and parameters — simple. It also allows you to connect taxonomies and post types with just a line or two of code.
But what Piklist is really there for is customizing the admin with metaboxes and settings. Piklist allows you to create several different kinds of metaboxes, such as repeating fields, file upload boxes, color pickers, etc., and then assign them to a custom post type. You can also use Piklist to manage users, admin settings, and widgets.
Basically, Piklist makes developing the WordPress back-end easier by simplifying the most exhausting and reusable bits.
The Folder Structure
Piklist can be used either in a plugin, or straight in your theme files. Either way, it begins with setting up the proper folder structure to make its magic work. Some code can be placed in your theme’s functions.php file, or in your plugins main file, but most will have to be added to a conventional folder structure.
Each custom item contains it’s own folder. For instance, admin settings are all placed in one folder, metaboxes are all placed in another, and dashboard tweaks go in a third. The full file structure ends up looking like this.
With your folder’s set up properly, you can get started using the framework.
Adding a Custom Post Type
Adding custom post types in WordPress is done using the register_post_type function, and is a more or less simple snippet. But Piklist simplifies the code a bit for you, and tacks on a few features.
Custom post type code for Piklist is added in the functions.php file or main plugin file. You can use the piklist_post_types filter to add an array of post types in a relatively familiar syntax:
add_filter('piklist_post_types', 'demo_post_type'); function demo_post_type($post_types) { $post_types['demo'] = array( 'labels' => piklist('post_type_labels', 'Demo'), 'title' => __('Enter a new Demo Title'), 'public' => true, 'supports' => array( 'author', 'revisions' ), 'status' => array( 'draft' => array( 'label' => 'New Order' ), 'new post' => array( 'label' => 'Request for Estimate' ) ) ) ); $post_types['demo-2'] = array( 'labels' => piklist('post_type_labels', 'Demo-2'), 'title' => __('Enter a new Demo 2 Title), 'hide_meta_box' => array( 'slug', 'author' ) ); return $post_types; }
I’ve simplified the code here a bit, but basically I am creating two custom post types — demo and demo-2 — within the same function in a simple array, then returning the $post_types object at the end of the function. You’ll notice that a few parameters are fairly standard and unchanged (‘public’ and ‘supports’). Other parameters are simplified quite a bit. For instance, you can use the post_type_labels filter to simply pass in a single label for your post type. The framework will automatically create singular, plural, and admin labels based on this.
Piklist also adds a few extra parameters for you to use. You can pass ‘status’ into the array to add a custom post status for the post type you are registering. The ‘title’ parameter is also added for the post title, and ‘hide_meta_box’ can be used to hide certain fields (like the main editor, or slug text field) on the post editor page.
Registering custom taxonomies follows a simple format if you need to create those quickly as well. Have a look at the full documentation for registering custom post types to see what Piklist can do for you.
Adding Your First Metabox
In order to get the most out of the WordPress post editor, you probably want to add a few custom fields and metaboxes so that data can be entered in a proper format. For instance, you may want to add a repeating text field, or a file upload form, or a date picker. Piklist allows you to create a simple metabox for any custom post type, and then add specialty custom fields to these boxes with just a few parameters.
The first thing to do is create a new file in your meta-boxes folder. Then, add a comment block to the top of the file.
You'll want to specify two things here. The "Title" is the actual text that will be displayed as the title of the metabox, and the "Post Type" is the post type you want to add the metabox to. This can be any of the standard post types, such as post or page, or a custom post type you created. There are many more parameters you can add to this comment block — like capability or context — but these are the only required fields.
The next thing to do is just start adding custom fields to the metabox. For instance, let's say you wanted to add a very simple text field.
piklist('field', array(
'type' => 'text',
'field' => 'my_text',
'label' => 'Text Box',
'description' => 'Field Description',
'value' => 'Default text',
));
A custom text field will be added to the bottom of the post editor labeled "Text Box." When content is added to this custom field, it will be saved to your database as "my_text." Simple enough. Now, let's add a radio field so users can select from a few different options.
piklist('field', array(
'type' => 'radio',
'field' => 'my_radio_field',
'value' => 'option2' // Sets default to Option 2,
'label' => 'Example Field',
'attributes' => array(
'class' => 'text'
),
'choices' => array(
'option1' => 'Option 1',
'option2' => 'Option 2',
'option3' => 'Option 3'
)
));
Simply edit the "choices" array to add your own radio fields. A new metabox will appear with three radio buttons for users to select.
There's much more that can be done while creating metaboxes, and you can customize lots with just a few simple comments or parameters, but this should give you a taste.
Creating an Admin Page
You can also use Piklist to create a new admin settings page, or a page with simple text. Simply use the piklist_admin_pages filter and pass in a few parameters, and your page will appear in the admin menu.
add_filter('piklist_admin_pages', 'piklist_demo_admin_pages'); function piklist_demo_admin_pages($pages) { $pages[] = array( 'page_title' => __('My Settings', 'piklist'), 'menu_title' => __('Settings', 'piklist'), 'capability' => 'manage_options', 'sub_menu' => 'edit.php?post_type=piklist_demo', 'menu_slug' => 'my-settings' 'setting' => 'custom_settings', 'menu_icon' => plugins_url('piklist/parts/img/piklist-icon.png'), 'default_tab' => 'General', 'save_text' => 'Save these settings' ); return $pages; }
This is a really simple admin page, but it should give you an idea. You can pass in a capability level for access to the page, along with a custom icon and title. The "sub_menu" parameter is used to specify the parent page of the menu option. Again, much more can be done with this function, so be sure to check out the documentation.
Using Piklist
Hopefully this gives you a basic understanding of what Piklist can do. I didn't get to many of the custom options and tweaks that can be made with the framework, and more things are being added all the time. Check out the User Guide if you want to explore it more.
If you find yourself customizing the WordPress admin again and again, using the same blocks of code each time, Piklist is probably a good fit. It gives you code shortcuts and simple utilities in a familiar syntax so you can get your site developed quickly, and move on to the real problem solving.
Have you used Piklist in the past? Let us know in the comments.
Jay Hoffmann is a WordPress developer hailing from NYC. In the strictest sense of the word, he is a WordPress enthusiast with an eye for front-end development and design. He has been working with WordPress since 2006 and currently works for a popular children's media company. This year, Jay started Tidy Repo, a curated list of the best and most reliable plugins from around the web. You can also follow Jay on Twitter.
3 Comments