+ All Categories
Home > Internet > Extending WordPress

Extending WordPress

Date post: 11-Apr-2017
Category:
Upload: jonathan-bossenger
View: 588 times
Download: 0 times
Share this document with a friend
23
Extending WordPress A (developers) guide to building your first WordPress plugin @jon_bossenger http://jonathanbossenger.co http://jonathanbossenger.com/extending-wordpress/
Transcript
Page 1: Extending WordPress

Extending WordPress

A (developers) guide to building your first WordPress plugin

@jon_bossenger http://jonathanbossenger.com

http://jonathanbossenger.com/extending-wordpress/

Page 2: Extending WordPress

WHAT IS A WORDPRESS PLUGIN?

• One (or more) files (PHP, HTML, CSS, JavaScript)

• Extend WordPress functionality

• Simple – display 10 most recent posts

• Advanced – ticketing/eCommerce

Page 3: Extending WordPress

PLUGIN BASICS

• Single file

• More complex structure

– PHP

– HTML/CSS (templates)

– JavaScript

• Plugin Header

Page 4: Extending WordPress

PLUGIN HEADER/*Plugin Name: My ToolsetPlugin URI: http://URI_Of_Page_Describing_Plugin_and_UpdatesDescription: This describes my plugin in a short sentenceVersion: 1.5Author: John SmithAuthor URI: http://URI_Of_The_Plugin_AuthorLicense: GPL2License URI: https://www.gnu.org/licenses/gpl-2.0.htmlDomain Path: /languagesText Domain: my-toolset*/

Page 5: Extending WordPress

TASK

• Create a lyrics plugin

• Copy and edit the plugin header from

hello.php to suit your requirements

• The plugin should be available in your plugins

list, in WP admin

Page 6: Extending WordPress

PLUGIN BUILDING BLOCKS

• PHP Functions

• WordPress Action Hooks

• WordPress Filter Hooks

• Templates

• WordPress APIs (eg Options, Settings API)

• CSS/JavaScript (wp_enqueue_script)

Page 7: Extending WordPress

It starts with an idea!

• At the end of each post, show a line from a

song

• Make the line stand out somehow

• (Bonus) Make it possible to edit the song lyrics

Page 8: Extending WordPress

Hooks

• A pre existing event, that triggers either an

Action or a applies a Filter

• Action Hooks

• Filter Hooks

Page 9: Extending WordPress

Actions

• A PHP function that is executed at a specific

point (hook)

• Create custom Actions or remove code from

an existing Action

Page 10: Extending WordPress

Filters

• A function that is associated with an

existing Action

• Create custom Filters to replace code from an

existing Action.

• Filters allow you to replace or alter specific

data.

Page 11: Extending WordPress

Examples

// Action Hook Examplefunction my_action_hook_function(){ // do some things here }add_action(‘some_action_hook’, ‘my_action_hook_function’);

// Filter Hook Examplefunction my_filter_hook_function($some_variable){ // do something with $some_variable here return $some_variable;}add_filter( ‘some_filter_hook’, ‘my_filter_hook_function’ );

Page 12: Extending WordPress

Building our plugin

• ‘the_content’ filter hook, hooks into the ‘the_content’ filter, which is used to filter the content of the post after it is retrieved from the database and before it is printed to the screen.

• add_filter(‘the_content’, ‘my_filter_function’)

Page 13: Extending WordPress

Building our plugin (cont)

• ‘wp_enqueue_scripts’ action hook, used to enqueue assets that are used on the front end (eg CSS, JavaScript)

• add_action(‘wp_enqueue_scripts’, ‘my_enqueue_scripts_function’);

• ‘wp_enqueue_style’ function, used to enqueue a style sheet file (load it on the front end)

• wp_enqueue_style( $handle, $source);

Page 14: Extending WordPress

TASK

• Create a custom filter hook to add one line of text

AFTER the post content

• Create a custom action hook to load the custom CSS file

• Add some CSS styling to your one line of text (you did

wrap it in a div or p, didn’t you?)

• TIP: $plugin_url = plugin_dir_url( __FILE__ );

Page 15: Extending WordPress

TASK

• Using the hello.php ‘hello_dolly_get_lyric’

function, create a function that gets one line

from the song to use in your custom

‘the_content’ filter in the previous step

• It should randomly select a lyric to display

Page 16: Extending WordPress

Saving Plugin Data

• Options API

• Custom Tables

• Settings API

• Adding menus

Page 17: Extending WordPress

Building our plugin (cont)

• ‘admin_menu’ action hook – used to add admin

menu items

• ‘add_options_page’ function – Add sub menu

page to the Settings menu

• add_options_page( $page_title, $menu_title,

• $capability, $menu_slug, $function);

Page 18: Extending WordPress

Building our plugin (cont)

• add_option($option, $value) – add an option to the options table

• update_option($option, $value) – update an option by its key

• get_option($option, $default) – get an option value

Page 19: Extending WordPress

Bonus Task

• Create a Settings menu item for your plugin

• Create a simple form that allows you to save

new song lyrics as a WP option

• Modify your plugin to use these saved lyrics

instead of ‘hello dolly’

Page 20: Extending WordPress

Development Tips and Tricks

• WP_DEBUG

• DebugBar

• BugFu

• PHPStorm

Page 21: Extending WordPress

Resources

• Google ;-)

• https://codex.wordpress.org

• https://developer.wordpress.org/

• https://developer.wordpress.org/plugins/

• https://codex.wordpress.org/WordPress_Coding_Standards

Page 22: Extending WordPress

Resources (cont)

• https://webdevstudios.com/books/professional-

wordpress-plugin-development/

• https://github.com/hlashbrooke/WordPress-Plugin-

Template

• WPSA Slack

Page 23: Extending WordPress

Questions ?


Recommended