Skip to content

Instantly share code, notes, and snippets.

@mhull
Last active June 5, 2016 12:13
Embed
What would you like to do?
Code examples from my Asheville WordCamp 2016 talk entitled "Using Hooks (and creating your own)"
<?php
/**
* Attempting to register a post type within a plugin, without using a hook,
* results in a fatal error
*/
$args = array(
... etc ...
);
register_post_type( 'my_post_type', $args );
/* OOPS! I broke everything!! */
<?php
/**
* To fix the problem, we can wrap our call to `register_post_type` inside a function
*/
function my_register_post_type() {
$args = array(
... etc ...
);
register_post_type( 'my_post_type', $args );
}
/**
* ...And then we can tell WP to run our function on the 'init' hook
*/
add_action( 'init', 'my_register_post_type' );
<?php
/**
* Register/enqueue CSS and JS files on the front end
*
* - Define a function that registers/enqueues our files
* - Tell WP to run our function at the correct time
*
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
*/
function my_enqueue_scripts() {
# enqueue a CSS file
wp_enqueue_style( 'my-handle', 'filename.css' );
# enqueue a JS file
wp_enqueue_script( 'my-handle', 'filename.js' );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );
<?php
/**
* Send an email upon saving a post with post type 'news_update'
*
* @param int $post_id The post ID being saved
* @param WP_Post $post The post object being saved
* @param bool $update Whether this is a post being updated (as opposed to a new post being created)
*
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
*/
add_action( 'save_post', 'my_project_updated_send_email', 10, 3 );
function my_project_updated_send_email( $post_id, $post, $update ) {
// If this is just a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
return;
}
if( 'news_update' != $post->post_type ) {
return;
}
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
/* ... etc ... */
// Send email to admin.
wp_mail( 'admin@example.com', $subject, $message );
}
add_action( 'save_post', 'my_project_updated_send_email' );
<?php
/**
* Filter the post row actions on the edit.php screen
*
* @param array $actions The existing array of actions
* @param WP_Post $post The post whose row actions are being edited
* @return array
*/
add_filter( 'post_row_actions', 'my_post_row_actions', 10, 2 );
function my_post_row_actions( $actions, $post ) {
# don't do anything if we're viewing posts in the trash
if( ! empty( $_GET['post_status'] ) && 'trash' == $_GET['post_status'] ) {
return $actions;
}
/**
* Remove the "Trash" link
*
* (probably don't actually do this)
*/
if( isset( $actions['trash'] ) ) {
unset( $actions['trash'] );
}
# URL needed here
$actions['my_action'] = '<a href="'.'">My Action</a>';
return $actions;
}
<?php
/**
* Creating our own filter hook
*/
# start with the number 4
$my_number = 4;
# let other people change the number
$my_number = apply_filters( 'edit_my_number', $my_number );
/**
* Probably still equal to 4, unless someone is hooking into our
* filter
*/
echo $my_number;
<?php
/**
* Actually using our own filter hook
*/
# subtract one from the number when it comes time
add_filter( 'edit_my_number', 'subtract_one' );
function subtract_one( $number ) {
return $number - 1;
}
# start with the number 4
$my_number = 4;
# let other people change the number
$my_number = apply_filters( 'edit_my_number', $my_number );
/**
* Now equal to 3 because we've hooked into the filter above
*/
echo $my_number;
<?php
/**
* Using our filter hook multiple times
*/
# subtract one from the number
add_filter( 'edit_my_number', 'subtract_one', 10 );
function subtract_one( $number ) {
return $number - 1;
}
# multiply the number by -3
add_filter( 'edit_my_number', 'times_negative_three', 9 );
function times_negative_three( $number ) {
return $number * (-3);
}
# start with the number 4
$my_number = 4;
# let other people change the number
$my_number = apply_filters( 'edit_my_number', $my_number );
echo $my_number; // now equal to -13
<?php
/**
* Getting crazy with our filter hook
*/
# start with the number 4
$my_number = 4;
# Equal to 0 or 1 depending on current timestamp
$wildcard_number = time() % 2;
# subtract one from the number, if the wildcard is === 0
add_filter( 'edit_my_number', 'maybe_subtract_one', 10, 2 );
function maybe_subtract_one( $number, $wildcard ) {
if( 0 === $wildcard ) {
return $number - 1;
}
return $number;
}
# let other people change the number
$my_number = apply_filters(
'edit_my_number', $my_number, $wildcard_number
);
echo $my_number; // equal to either 3 or 4
<?php
/**
* Hook into the area above the Big Boom Directory fields area and render some HTML
*
* @link https://wordpress.org/plugins/big-boom-directory
*/
add_action( 'bbd_before_fields_wrap', 'my_before_fields_wrap' );
function my_before_fields_wrap() {
?>
<p><a href='#'>Share this listing</a></p>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment