Skip to content

Instantly share code, notes, and snippets.

@adamplabarge
Last active January 8, 2018 21:24
Show Gist options
  • Save adamplabarge/df6f389d7bc5a1383a2ab2374b992e67 to your computer and use it in GitHub Desktop.
Save adamplabarge/df6f389d7bc5a1383a2ab2374b992e67 to your computer and use it in GitHub Desktop.
Portland WP Meetup - Hooks and Filters - Jan 08 2018 - Sample Hooks Plugin Code
<?php
/*
Plugin Name: Sample Plugin
Plugin URI:
Description: Sample code for the Portland WordPress MeetUp Hooks and Filters Talk
Version: 1.0.0
Author: adamplabarge
Author URI: https://github.com/adamplabarge
Text Domain: sample-plugin
License: GPLv2
*/
add_action('genesis_before_header', 'demonstrate_call_user_func', 10);
function demonstrate_call_user_func() {
load_pretend_hooks();
load_sample_loop_and_filters();
}
function load_pretend_hooks() {
$hooks = [
'sample_header_hooks' => [
'simple_print_func_a',
'simple_print_func_b'
]
];
foreach($hooks['sample_header_hooks'] as $func) {
echo "<p>" . call_user_func($func) . "</p>";
}
}
function simple_print_func_a() {
return "I\"m printing the letter A.";
}
function simple_print_func_b() {
return "I\"m printing the letter B.";
}
function load_sample_loop_and_filters() {
$args = [
'post_type' => 'post',
];
$q = new WP_Query($args);
add_filter('the_permalink', 'alter_permalink');
if ($q->have_posts()) :
while ($q->have_posts()) : $q->the_post();
echo '<p>' . the_permalink() . '</p>';
endwhile;
endif;
remove_filter('the_permalink', 'alter_permalink');
if ($q->have_posts()) :
while ($q->have_posts()) : $q->the_post();
echo '<p>' . the_permalink() . '</p>';
endwhile;
endif;
}
function alter_permalink($url) {
return 'This is just a messages.';
return 'http://allyourpermalink.com/are/belong/to/us';
}
//add_action('genesis_before_header', 'demonstrate_custom_actions', 15);
function demonstrate_custom_actions() {
$name = 'bob';
do_action('our_custom_action_hook', $name);
$names = ['bob', 'jane', 'abigail', 'booker'];
foreach($names as $name) {
do_action('our_custom_action_hook', $name);
}
}
add_action('our_custom_action_hook', 'our_custom_action_function');
function our_custom_action_function($_name) {
echo ((strpos($_name, 'b') !== false)
? apply_filters('our_data_filter', $_name)
: $_name)
. ' ';
}
add_filter('our_data_filter', 'our_capitalize_filter');
function our_capitalize_filter($_name) {
return ucfirst($_name);
}
/**
* Development Helpers
*/
//add_action('genesis_header','show_me_hook_info');
function show_me_hook_info() {
$hook_name = 'genesis_header';
global $wp_filter;
dump($wp_filter[$hook_name]);
}
function dump( $var, $txt='' ) {
print "<pre><b>$txt</b> = ";
print_r( $var );
print "</pre>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment