Upload
ylefebvre
View
176
Download
0
Tags:
Embed Size (px)
Citation preview
WordCamp Montreal 2015 Workshop
Yannick LefebvreAuthor and Plugin Developer
@ylefebvre / ylefebvre.caylefebvre.ca/wcmtl15workshop
Creating your first WordPress plugin
About me
● WordPress user since 2004● Released first plugin in 2005
– Link Library● Author of WordPress Plugin
Development Cookbook● 8 Plugins to date on official
repository● Custom plugin development
Goals for this workshop
● Create a functional plugin in WordPress using Custom Post Types
● Learn how to store and retrieve CPT data and custom field data
● Become more self-reliant in project development● See how easy plugin creation can be instead of
stuffing code in your theme files
Ground rules
● Ask questions as they come to avoid getting stuck on a topic
● We may not cover all topics in this workshop
– You can always go back to materials later● Let's have some fun!
Required tools
● Local/remote web server (MAMP, XAMPP, VVV)● Installation of WordPress● Text Editor (TextMate, Notepad++ or other)● FTP Tool (Filezilla or other if working remotely)● Sample Chapter 4 from WordPress Plugin
Development Cookbook– http://bit.ly/1eo6LXQ– Code sample files: http://bit.ly/1MGuHRZ
Outline
● Creating a plugin header● Creating a custom post type● Adding a new section to the custom post type
editor● Displaying single custom post type items using
custom templates● Creating an archive page for custom post types● Displaying custom post type data in shortcodes
Outline (continued)
● Adding custom categories for custom post types● Displaying additional columns in the custom post
list page● Adding filters for custom categories to the custom
post list page● Updating page title to include custom post data
using plugin filters
Creating a plugin header
1.Navigate to WordPress wp-content/plugins directory
2.Create a new directory called book-reviews
3.Go to directory and create new text file book-reviews.php
4.Edit new file in a text editor and add plugin header
Creating a plugin header
/*
Plugin Name: Book Reviews
Plugin URI:
Description: My Book Reviews Plugin
Version: 1.0
Author: Yannick Lefebvre
Author URI: http://ylefebvre.ca/
*/
Your new plugin listing
Creating a custom post type
● WordPress Hooks– Allows you to ask WP to execute your custom code at
specific points during execution– Code can output extra content or make requests to
WordPress to execute various tasks
add_action( 'init', 'book_reviews_init' );
function book_reviews_init() {
...<code to define post type>...
}
Procedural vs Object-oriented programming
● Registered functions can be simple procedural functions or can be methods of objects
● To keep things simple, procedural functions will be used in this workshop
Procedural vs Object-oriented programming
● To learn all about OOP in WordPress, check out CarlAlexander.ca
Creating a custom post type
WordPress Action Hooks
Adding a new section to CPT editor
1.Add action hook to be called on admin section initadd_action( 'admin_init', 'book_reviews_admin_init');
2.Create new editor sectionfunction book_reviews_admin_init() {
add_meta_box( 'book_reviews_details_meta_box',
'Book Review Details',
'book_reviews_display_review_details_meta_box',
'book_reviews', 'normal', 'high' );
}
Adding a new section to CPT editor
3.Function to populate new meta box uses mix of PHP and HTML to generate contents
4.Add action hook to be called when post is savedadd_action( 'save_post', 'br_add_book_review_fields', 10, 2 );
● This call introduces two new arguments to add_action, the priority and the number of arguments.
5.Remove custom-fields section from initial CPT definition
Adding a new section to CPT editor
Displaying single custom post type item
● Can simply create a new single type template under theme directory
– single-book-reviews.php● Can put template in plugin directory and get
WordPress to use this special template when it encounters our new CPT
Displaying single custom post type item
Displaying archive page for CPT
● Can simply create a new single type template under theme directory
– archive-book-reviews.php● Can put template in plugin directory and get
WordPress to use this special template when it encounters our new CPT
Displaying archive page for CPT
Displaying CPT in shortcode
1.Add call anywhere in plugin file to define shortcode and its associated function
add_shortcode( 'book-review-list', 'br_book_review_list' );
2.Callback function should prepare new content and return it at the end.
● No echo function● No inline HTML
3.Shortcodes are easy for casual users to integrate plugin content on their site [book-review-list]
Displaying CPT in shortcode
Adding custom categories to CPT
1.Find register_post_type function in init hook
2.Add call to register_taxonomy to create new taxonomy and associate it with CPT
Adding columns to custom post type list page
1.Use add_filter function to register callback to be executed when posts are displayed for CPT
add_filter( 'manage_edit-book_reviews_columns', 'br_add_columns' );
2.Define new columns for CPT
3.Register action hook to populate new columnsadd_action( 'manage_posts_custom_column', 'br_populate_columns' );
4.Add function to return correct values for new columns given current post (same as the loop)
Adding columns to custom post type list page
WordPress Filter Hooks
WordPress Filter Hooks
● Allows plugin code to modify data before it is used to display pages
● Plugin must be sure to return data after modification to ensure proper functionality
Make custom columns sortable
1.Add filter to set columns to be sortableadd_filter( 'manage_edit-book_reviews_sortable_columns', 'br_author_column_sortable' );
2. Add filter for column requests to modify query when column ordering is selected
Adding category filter to CPT list
1.Register action to be called when posts filtering occurs
add_action( 'restrict_manage_posts', 'br_book_type_filter_list' );
2.Display drop-down list of categories
3.Add filter to modify post queriesadd_filter( 'parse_query', 'br_perform_book_type_filtering' );
4.Add category term to query when present
Adding category filter to CPT list
Altering page title to add new CPT data
1.Add filter to modify page titleadd_filter( 'wp_title', 'br_format_book_review_title' );
2.Implement filter to add book author to page title
Questions? Hungry for more?
● Check out my book, available in print or eBook formats– http://bit.ly/1Ko5MmW
● Contact me to bounce any ideas on your own projects– @ylefebvre– ylefebvre.ca