Last active
August 29, 2015 14:02
-
-
Save tatemz/101531de2253739cb808 to your computer and use it in GitHub Desktop.
A gist for showing how WordPress actions and filters can be used.
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 | |
// The starting line | |
start(); | |
// 26 mile markers | |
for ( $mile = 1; $mile <= 26; $mile++ ) { | |
if ( $mile == 13 ) // Halfway mark | |
halfway_mark(); | |
// Mile marker for $mile | |
place_mile_marker( $mile ); | |
} | |
// The finish line | |
finish(); |
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 | |
// The starting line | |
do_action( 'start-line' ); | |
// 26 mile markers | |
for ( $mile = 1; $mile <= 26; $mile++ ) { | |
if ( $mile == 13 ) // Halfway mark | |
do_action( 'halfway-mark' ); | |
// Mile marker for $mile | |
do_action( 'mile-marker', $mile ); | |
} | |
// The finish line | |
do_action( 'finish-line' ); |
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 | |
add_action( 'start-line', 'start' ); | |
function start() { | |
// Output the starting line | |
} | |
add_action( 'halfway-mark', 'halfway_mark' ); | |
function halfway_mark() { | |
// Output the halfway marker | |
} | |
add_action( 'mile-marker', 'place_mile_marker' ); | |
function place_mile_marker( $current_mile ) { | |
// Output the mile marker at $current_mile | |
} | |
add_action( 'finish-line', 'finish' ); | |
function finish() { | |
// Output the starting line | |
} |
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 | |
add_action( 'halfway-mark', 'give_water', 10 ); | |
function give_water() { | |
// Drink your water FIRST! | |
the_hydration_station( 'water' ); | |
} | |
add_action( 'halfway-mark', 'give_gatorade', 11 ); | |
function give_gatorade() { | |
// Electrolytes! It's what plants crave! | |
the_hydration_station( 'gatorade' ); | |
} |
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 | |
function the_hydration_station( $type ) { | |
$healthy_liquids = array( 'water', 'gatorade' ); | |
// If $type is healthy... | |
if ( in_array( $type, $healthy_liquids ) ) | |
echo 'Here runner! Drink some ' . $type . '!'; | |
else | |
echo "Don't drink that! You need water!"; | |
} |
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 | |
function the_hydration_station( $type = 'water' ) { | |
$types = array( 'water', 'gatorade' ); | |
// Allow filtering... | |
$healthy_liquids = apply_filters( 'healthy-liquids', $types ); | |
// If $type is healthy... | |
if ( in_array( $type, $healthy_liquids ) ) | |
echo 'Here runner! Drink some ' . $type . '!'; | |
else | |
echo "Don't drink that! You need water!"; | |
} |
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 | |
add_filter( 'healthy-liquids', 'add_healthy_options' ); | |
function add_healthy_options( $types ) { | |
$types[] = 'brawndo'; // It's also got what plant's crave! | |
$types[] = 'vitamin water'; | |
return $types; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment