Skip to content

Instantly share code, notes, and snippets.

@tatemz
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatemz/101531de2253739cb808 to your computer and use it in GitHub Desktop.
Save tatemz/101531de2253739cb808 to your computer and use it in GitHub Desktop.
A gist for showing how WordPress actions and filters can be used.
<?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();
<?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' );
<?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
}
<?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' );
}
<?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!";
}
<?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!";
}
<?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