I am ussing bbPress and want to extend the BBP_Shortcodes class in order to replace a function with my own.
Here is my basic snippet which seems to do nothing:
class WPTUMBLE_Shortcodes extends BBP_Shortcodes { /** * Display an index of all visible root level forums in an output buffer * and return to ensure that post/page contents are displayed first. * * @since bbPress (r3031) * * @param array $attr * @param string $content * @uses bbp_has_forums() * @uses get_template_part() * @return string */ public function display_forum_index() { echo 'Yo Adrian'; } } function wptumble_register_shortcodes() { global $bbp; // Bail if bbPress is not loaded if (is_a($bbp, 'bbPress')) return; $bbp->shortcodes = new WPTUMBLE_Shortcodes(); } add_action( 'bbp_init', 'wptumble_register_shortcodes', 25 );
Looking at the bbpress functions setup it says:
add_action( 'bbp_init', 'bbp_register_shortcodes', 18 );
What am I missing?
*I have tried every priority from hooking to init at priority 1, all the way through where I posted above, but none seem to do anything.
Even stranger:
I basically paste that code into a plugin file I called xyz and I receive no errors, just nothing happens when I view what should be outputted. Meaning I get the original output for display_forum_index() instead of my new one.
Here is where it gets even worse:
Instead of simply creating a plugin with that code in it, I place the code into my ‘full’ plugin.
I use the following:
include_once (WPTUMBLE_PLUGIN_DIR.'twentyeleven-bbpress-child/inc/tumble-core-shortcodes-single.php');
I then have the WPTUMBLE_Shortcodes class pasted into the file above.
*Trying to keep things clean so separating my different functions into different files in my custom plugin
What happens is when I do that I end up with a fatal error saying BBP_Shortcodes is not defined.
The ONLY difference is I am putting the code into a file that is include_once..
What in the world could be going on there?
Either I get nothing happening when it’s a single plugin file, or I get fatal error if I include_once and place the code in another file.
Soooo confused now.
I can’t tell you what you’re doing wrong with your includes and such, but your method of extending the class is basically correct.
However, the most likely scenario that I can see is that your add_action is basically executing in the wrong place. Also, you’re doing-it-wrong by allowing the original action to stand instead of replacing it.
Try this:
This removes the original action and replaces it with your own, and it does it at the plugins loaded hook, ensuring that it happens after both your plugin and the bbPress plugin is loaded, and presumably all the action hooks for bbPress were in place, but not yet executed (since init hasn’t been reached).