Skip to content

Instantly share code, notes, and snippets.

@ryelle
Last active November 4, 2015 15:37
Show Gist options
  • Select an option

  • Save ryelle/a5a7d900f21dd6ce3639 to your computer and use it in GitHub Desktop.

Select an option

Save ryelle/a5a7d900f21dd6ce3639 to your computer and use it in GitHub Desktop.
Plugin Workshop actions & filters demo plugin
<?php
/*
* Plugin Name: Workshop Demo Plugin
*/
// Action example
function demo_add_html_to_footer() {
echo "<h1 style='color:white;'>I'm an example!</h1>";
}
// add_action( 'wp_footer', 'demo_add_html_to_footer' );
// ------------------------------------------------------------
// Filter example
function demo_enhance_description( $value ){
// In pre_option_blogdescription, $value is empty, and we're preempting the database read
// return 'A new site description';
// In option_blogdescription, we have the site description in $value
return $value . '!';
}
// add_filter( 'pre_option_blogdescription', 'demo_enhance_description' );
// add_filter( 'option_blogdescription', 'demo_enhance_description' );
// ------------------------------------------------------------
// Action with argument
function demo_restrict_homepage_category( $query ) {
// Check out WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category_name', 'travelling' );
}
}
// add_action( 'pre_get_posts', 'demo_restrict_homepage_category' );
// ------------------------------------------------------------
// Filter with multiple arguments
function demo_add_id_to_title( $title, $id ) {
// Formatting function
// return sprintf( '%s [%s]', $title, $id );
// Plain string concatenation
return $title . ' ['. $id . ']';
}
// add_filter( 'the_title', 'demo_add_id_to_title', 10, 2 );
// ------------------------------------------------------------
// All: display all hooks run on a page
function demo_display_all () {
if ( ! in_array( current_filter(), [ 'gettext_with_context', 'gettext' ] ) ) {
var_dump( current_filter() );
}
}
// add_action( 'all', 'demo_display_all' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment