Upload
josh-pollock
View
554
Download
1
Embed Size (px)
Citation preview
@Josh412
CalderaLabs.org
Hi I'm JoshLead Developer: CalderaWP
I make WordPress pluginsI teach about WordPressI wrote a book about the WordPress REST APII am core contributor to WordPressI am a member of The WPCrowd
@Josh412
How To Make A Plugin
Step 1: Add a new directory to the plugins directory (optional).
Step 2: Add a file in that directory or the plugins directory with a valid plugin header.
Step 3: Write code (optional).
@Josh412
Plugins vs Themes: Best Practices
Themes should be use to present a unique design
Everything else should be in a plugin.
@Josh412
Should This Code Go In functions.php ?
Is the answer to any of these questions no:
Do I ever want to reuse this code apart from this theme?Will I miss this code when I switch themes?
Switch for troubleshooting.Switch for new look.
Can I use this code for other purposes?
@Josh412
Main File: The File With A Plugin Header
Plugin HeaderLicense/ Copyright/ Contact InfoCheck Dependencies & RequirementsLoad PluginStart Plugin
@Josh412
Naming Things
There Are No RulesYou Should Have Rules
I like to document naming conventions:)
@Josh412
Naming Things: naming-conventions.txt
Plugin Name -- Text domain -- Function prefix -- Class prefix -- Root namespace -- Hook Prefix --
@Josh412
CalderaLabs.org
Don’t
Hack Core
Modifying Core/ Plugins / Themes Is A Bad Idea
You Will Lose Your Changes On Update
@Josh412
ActionsDo something
Don’t return values*
Actions Vs Filters
FiltersChange something
Must return a value*
@Josh412
Using Actions: Print Tracking Pixel
function my_prefix_pixel() { echo '<img src="https://adnetwork.com/pixel.gif" width="0" height="0">';}
add_action( 'wp_footer', 'my_prefix_pixel' );
@Josh412
Using Actions: Modify WP_Query
function search_filter($query) { if ( !is_admin() && $query->is_main_query() ) { if ($query->is_search) { $query->set('post_type', array( 'post', 'movie' ) ); } }}
add_action('pre_get_posts','search_filter');
@Josh412
Using Filters: Add To Content
add_filter( 'the_content', 'slug_callback' );
function slug_callback( $content ){
return $content . '<p class="cta">Hey Sign Up for My Thing!</p>';
}
@Josh412
Using Filters: Add To Content
add_filter( 'the_content', function( $content ){
return $content . '<p class="cta">Hey Sign Up for My Thing!</p>';
});
@Josh412
Creating Filters: After
function prefix_get_form( $id ){
$form = get_option( $id, [] ); $form = apply_filters( 'prefix_get_form', $form, $id ); return $form; }
@Josh412
Hook Priority
add_filter( 'name','callback', 2 );
add_filter( 'name', 'callback_two', 55 );
@Josh412
Hook Arguments
$form = apply_filters( 'prefix_get_form', $form, $id );
add_filter( 'prefix_get_form', function( $form, $id ){
if( 'CF1234567' == $id ){
// do something to $form
}
return $form;
}, 10, 2 );
@Josh412
HTTP requestsThat functions will be used how you intended themData returned from the databaseYourself
Some Things Not To Trust
@Josh412
Single Responsibility PrincipleFunctions should do one thing.
Do Not Repeat Yourself (DRY)Functions, not copypaste
Making Code Reusable