One way is to use static methods from class itself, as @leymannx explained in his answer.
I prefer a little bit different approach, which is a little bit nicer/cleaner (but it's only my opinion, so feel free to argue with that statement). I do it like so:
- If some function should be available for use in templates, then I register is as a template tag (so I call it the same way WordPress does -
get_X
for getting something and the_X
for printing something.
- If some function should be available for use as a shortcode, then I register such shortcode.
- I then register my own filters/actions hooks for the functions/shortcodes above and in my class I use these hooks and write proper code - this way the code is encapsulated in class properly.
And some (very basic) example:
In file template-tags.php
I'll add this code:
function the_wpse_hello_world_message( $name = '' ) {
echo get_wpse_hello_world_message( $name );
}
function get_wpse_hello_world_message( $name = '' ) {
return apply_filters( 'get_wpse_hello_world_message', 'Hello World', $name );
}
So anyone can easily use both of these functions anywhere in the code, and it's pretty easy to find out what is the "public API" of my plugin.
And later in my class I do something like this:
class My_WPSE_Super_Plugin {
protected function initHooks() {
add_filter( 'get_wpse_hello_world_message', array( $this, 'get_wpse_hello_world_message_filter' ), 10, 2 );
}
...
protected get_wpse_hello_world_message_filter( $result, $name ) {
if ( trim($name) ) {
return 'Hello, ' . $name . '!';
}
return $result; // or return 'Hello world!'.
}
}
The pros for this method are (IMHO):
- it's easier to learn the "public API" of a plugin (which is important if the team is bigger, or if you will work with given site for a long time)
- the real code inside the class gets properly encapsulated
- the "public API" is separated from real implementation, so it's easy to document (write some comments for users) and to do refactoring later someday... Let's say the class gets to big and you want to divide it to smaller classes? What then? Will all users have to change all their templates?
- it allows to modify these parts using filters/actions (and we all love WP exactly for that, right?)