To complement @s_ha_dum’s answer: If you just want to offer a function to be used in a theme, make it a custom action.
Sample:
add_action( 'call_hello_world', 'hello_world' );
Now, in a theme, the author can call your function with …
do_action( 'call_hello_world' );
… and the function will print its content only where the author needs it.
This has at least three advantages:
- You can disable the plugin anytime, and nothing will break. If there is no callback for an action, nothing will happen.
- You don’t have to check with
function_exists()
, which is always … ugly. - Another plugin can use the same action, or it can replace your callback.
Here is a sample plugin:
<?php
/*
* Plugin Name: Custom Action
*/
add_action( 'call_hello_world', 'hello_world' );
/**
* Demo function.
*
* Usage:
* do_action( 'call_hello_world' );
*
* @wp-hook call_hello_world
* @return void
*/
function hello_world()
{
print '<h1>Hello World!</h1>';
}
// static class method
add_action( 'call_static_method', array ( 'Demo_Class', 'static_method' );
// dynamic class method
$demo = new Demo_Class;
add_action( 'call_dynamic_method', array ( $demo, 'dynamic_method' );
class Demo_Class
{
public static function static_method()
{
print 'I was called statically.';
}
public function dynamic_method()
{
print 'I was called with an instance.';
}
}
Install, activate, and use the do_action()
in a theme as often as you need it.