35
WordCamp Montreal 2015 Workshop Yannick Lefebvre Author and Plugin Developer @ylefebvre / ylefebvre.ca ylefebvre.ca/wcmtl15workshop Creating your first WordPress plugin

Workshop: Creating your first WordPress plugin

Embed Size (px)

Citation preview

Page 1: Workshop: Creating your first WordPress plugin

WordCamp Montreal 2015 Workshop

Yannick LefebvreAuthor and Plugin Developer

@ylefebvre / ylefebvre.caylefebvre.ca/wcmtl15workshop

Creating your first WordPress plugin

Page 2: Workshop: 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

Page 3: Workshop: Creating your first WordPress plugin

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

Page 4: Workshop: Creating your first WordPress plugin

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!

Page 5: Workshop: Creating your first WordPress plugin

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

Page 6: Workshop: Creating your first WordPress plugin

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

Page 7: Workshop: Creating your first WordPress plugin

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

Page 8: Workshop: Creating your first WordPress plugin

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

Page 9: Workshop: Creating your first WordPress plugin

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/

*/

Page 10: Workshop: Creating your first WordPress plugin

Your new plugin listing

Page 11: Workshop: Creating your first WordPress plugin

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>...

}

Page 12: Workshop: Creating your first WordPress plugin

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

Page 13: Workshop: Creating your first WordPress plugin

Procedural vs Object-oriented programming

● To learn all about OOP in WordPress, check out CarlAlexander.ca

Page 14: Workshop: Creating your first WordPress plugin

Creating a custom post type

Page 15: Workshop: Creating your first WordPress plugin
Page 16: Workshop: Creating your first WordPress plugin

WordPress Action Hooks

Page 17: Workshop: Creating your first WordPress plugin

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' );

}

Page 18: Workshop: Creating your first WordPress plugin

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

Page 19: Workshop: Creating your first WordPress plugin

Adding a new section to CPT editor

Page 20: Workshop: Creating your first WordPress plugin

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

Page 21: Workshop: Creating your first WordPress plugin

Displaying single custom post type item

Page 22: Workshop: Creating your first WordPress plugin

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

Page 23: Workshop: Creating your first WordPress plugin

Displaying archive page for CPT

Page 24: Workshop: Creating your first WordPress plugin

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]

Page 25: Workshop: Creating your first WordPress plugin

Displaying CPT in shortcode

Page 26: Workshop: Creating your first WordPress plugin

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

Page 27: Workshop: Creating your first WordPress plugin

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)

Page 28: Workshop: Creating your first WordPress plugin

Adding columns to custom post type list page

Page 29: Workshop: Creating your first WordPress plugin

WordPress Filter Hooks

Page 30: Workshop: Creating your first WordPress plugin

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

Page 31: Workshop: Creating your first WordPress plugin

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

Page 32: Workshop: Creating your first WordPress plugin

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

Page 33: Workshop: Creating your first WordPress plugin

Adding category filter to CPT list

Page 34: Workshop: Creating your first WordPress plugin

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

Page 35: Workshop: Creating your first WordPress plugin

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