Skip to content

Instantly share code, notes, and snippets.

@RalfAlbert
Created December 21, 2013 13:18
Show Gist options
  • Save RalfAlbert/8069187 to your computer and use it in GitHub Desktop.
Save RalfAlbert/8069187 to your computer and use it in GitHub Desktop.
tutorial_testfile.php
<?php
/**
* Plugin Name: 000 Tutorial TestFile
* Description: Simple testfile for writing tutorials
*/
namespace TutorialTestFile;
add_action(
'plugins_loaded',
__NAMESPACE__.'\add_widget_hook'
);
function add_widget_hook() {
add_action(
'wp_dashboard_setup',
__NAMESPACE__.'\add_dashboard_widget'
);
}
function add_dashboard_widget() {
wp_add_dashboard_widget(
'Test-widget',
'Test Widget',
__NAMESPACE__.'\output',
$control_callback = null
);
}
function output() {
add_action( 'test_hook', __NAMESPACE__.'\hook_callback', 0, 2 );
add_filter( 'test_hook', __NAMESPACE__.'\hook_callback', 0, 2 );
// [...]
echo '<div class="wrap">';
$var_old = 'The callback';
do_action( 'test_hook', $var_old, 'do_action' );
$var_new = apply_filters( 'test_hook', $var_old );
printf( '<p>With apply_filters(): %s</p>', $var_new );
echo '</div>';
}
function hook_callback( $hook_var, $action_or_filter = 'do_filter' ) {
if ( 'do_action' === $action_or_filter ) {
printf( 'With do_action(): %s was called by <code>do_action()</code><br>', $hook_var );
return true;
}
$hook_var = (string) $hook_var;
$hook_var .= ' was called by <code>apply_filters()</code>';
return $hook_var;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment