Last active
June 5, 2016 12:13
-
-
Save mhull/7d21b313ab405a7e28b1ef929f063121 to your computer and use it in GitHub Desktop.
Code examples from my Asheville WordCamp 2016 talk entitled "Using Hooks (and creating your own)"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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!! */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Remove the "Edit" link for this plugin | |
* | |
* @param array $links The current actions links | |
* @return array | |
*/ | |
$plugin = plugin_basename( __FILE__ ); | |
add_filter( 'plugin_action_links_' . $plugin, 'my_plugin_links' ); | |
function my_plugin_links( $links ) { | |
# remove the "Edit" link | |
if( array_key_exists( 'edit', $links ) ) { | |
unset( $links['edit'] ); | |
} | |
return $links; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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