In a plugin class I want to provide simple fields for public data: email, phone number, Twitter etc. The list can be extended.
See the plugin Public Contact Data on GitHub for details.
To keep the usage simple, I also want to offer shortcodes which are easy to type:
[public_email]
[public_phone]
[public_something]
The only difference is the second part. I don’t want further arguments for the shortcodes because they are error prone. So I register one shortcode handler for all fields in my plugin class:
foreach ( $this->fields as $key => $value )
{
add_shortcode( 'public_' . $key, array( $this, 'shortcode_handler' ) );
}
Now shortcode_handler()
has to know which shortcode has been called. My question is: How do I do this?
My current workaround is another function:
protected function current_shortcode()
{
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
return $backtrace[3]['args'][0][2];
}
This … works for the_content()
. But it is neither elegant nor robust.
I have read wp-includes/shortcodes.php
, but I cannot see right now how to do it better.