Overwriting Template functions
-
My latest plugin has hit a snag.
I cannot seem to overwrite bb_get_header() using add_filter(‘bb_get_header’, ‘function_i_want_to_run_instead’);.
I’ve tried variations, adding priorities, spelling – y’know the usual, but nothing i can do seems to overwrite that function. I’d appreciate any help. Thanks
-
So 7 hours later and i can’t find a solution to this.
The weird thing is that ‘some’ functions can be overwritten with filters or added to with actions but others are refusing to play ball. Here’s the code that i have:
//works
add_action(‘post_ip_link’, ‘output_what_i_want’);
add_action(‘topic_pages’, ‘output_what_i_want’);
// does not work
add_action(‘post_form’,’output_what_i_want’);
add_action(‘bb_post_header’,’output_what_i_want’);
add_action(‘bb_get_footer’, ‘output_what_i_want’);
When that code runs, the first 2 add_actions’s run fine, but the bottom 3 don’t. For some reason i can’t overwrite those functions.
Now to the filters:
// Working
add_filter(‘post_ip_link’, ‘output_what_i_want, 11’);
add_filter(‘topic_pages’, ‘output_what_i_want, 11’);
add_filter(‘post_form’,’output_what_i_want, 11′);
// Not Working
add_filter(‘bb_post_header’,’output_what_i_want, 11′);
add_filter(‘bb_get_footer’, ‘output_what_i_want, 11’);
This is where it’s a bit weird, as POST_FORM can be added to via a filter (great), but can not be overwritten. BB_POST_HEADER and BB_POST_FOOT cant seem to overwritten or added to via a filter.
So in the short term, i’m looking for a solution to overwriting the BB_POST_HEADER and BB_POST_FOOT functions. I’ve a plugin that’s stalled due to it.
Moving on from there though, i think this is something we need to look at and maybe get some documentation/information posted about somewhere. Because as far as i can tell there are only a few options here:
1) My code is wrong
2) Some functions cannot be overwritten
3) Some functions cannot be added to with an action
4) There’s a bug at play.
Any help would be really appreciated.
Well this is going to look like a shameless bump either way, but as i’ve had 3 different people on the forums ask about a plugin that i’m building that’s been stalled on this i thought i’d check to see if anyone else can shed alight on it.
Thanks
Stupid question – Don’t you have to
remove_filter('filter_i_want_to_replace')
first?It’s not stupid mate, it might be the answer. At this stage i’m up for trying anything.
I don’t have it and will go test it now.
Thank you!
You need a basic understanding of filters vs actions in WordPress before you can get this done.
You can’t replace anything via add_action(‘post_form’… because it’s an action and not a filter. Calling it as a filter doesn’t change the fact that the hook itself is an action.
Filters give you the opportunity to change the data passed.
Actions are just points in the processes that you can inject activity into, but you can’t change what was before or what is coming after. No data is passed to you, no data is taken from your activity.
You cannot change forms this way on the fly, it’s impossible (unless maybe via javascript).
If you want dynamic themes, start with my theme switcher plugin and find some way to keep track of what theme is used without cookies (maybe sessions instead).
HI _ck_,
thanks for this. I’m not trying to stop something being called with an add_action, but i’m sorry if i didn’t phrase it well enough. What I have found is that i cannot either get an added filter nor an action to add to certain functions in BBpress. Thats the issue i’ve hit.
Nor am i wanting dynamic theme’s, as your wonderful plugin covers that.
My plugin does this:
ON activation checks for wordpress integration.
If wordpress integrated copies the header file form the wordpress template to a directory.
Inserts BBcode Hooks into the new file.
Now that bit works well and dandy; but what i want in this instance is to overwrite the function BB_GET_HEADER as it’s hardcoded in the template_functions.php file.
So here is my question, how can i/we overwrite the BB_GET_HEADER function ?
1) can we overwrite functions in the template-functions.php file?
2) how?
3) is there an example of this anywhere?
I ask not to be pedantic, but because i think you answered my post based on what you think i’m trying to do with my plugin rather than the issue at hand – namely that i can over write some functions and not others.
You are pretty much the authority on this, so i really appreciate any help insight you might have on how to overwrite the BB_GET_HEADER function. thank you!
I assume this is just a typo in your example and not what you are really using:
add_filter('topic_pages', 'output_what_i_want, 11');
because the quote should close before the 11
add_filter('topic_pages', 'output_what_i_want', 11);
There’s nothing wrong with the action or filter triggers in bbpress (only that there are not enough of them or in the right places). The code for that is taken right out of wordpress and works fine. My dozens of plugins kinda prove they all work.
HI _ck_,
Yeah it was a typo.
I know that functions can have things added to them and overwitten as your plugins prove, i’m not doubting that for a second, and i’m sorry if it came across that way. What i’m saying is that while i’m able to either overwrite or add to some functions, some i’m only able to add to, and some i’m not able to either add to nor overwrite.
Now if that’s not a bug, then cool. Really cool! But i can’t see why that would be, and i’m hoping you can point me in the right direction. You’ve said countless times it works in the same way as wordpress plugins, but a google search of how to overwrite wordpress functions leads me to (see pages below). And the code supplied here works on every function i’ve tried (so far – 9 plugins in) except BB_GET_HEADER, BB_GET_FOOTER and POST_FORM. and i’m wondering why that is.
I’m absolutely 100% cool with the fault being at my end, it would be a relief tbh, but with no documentation and no examples or other plugins that overwrite these functions i’m totally lost.
Basically, typos and what pepole think i’m wanting do to aside, what i really want to know is this:
What code do i have to write so that my function is called instead of the one hard coded into template-functions.php?
Thank you so much.
Edit: Websites i use as a plugin resource:
http://www.catswhocode.com/blog/how-to-overwrite-wordpress-core-functions
https://codex.wordpress.org/Plugin_API
http://comox.textdrive.com/pipermail/wp-hackers/2008-February/018013.html
The only functions that you can replace are the pluggable functions defined in pluggable.php. What i do to replace a core function is to copy them to a functions.php file in my template, rename the function from “core_function” to “my_core_function”, and then replace the function call in the templates. It’s not very elegant but it works.
THANK YOU detective!
(also thanks to _ck_ and Ipstenu – i’m sure we would not have gotten there without you).
So in idiots terms (so i can understand ;-] )…
All functions hardcoded into BBpress’s template-functions can be overwritten, but only if they are on a hardcoded list within the core?
And if you want to write a plugin to overwrite one of these template functions that’s hard coded into the core, and not on another the hardcoded list in the core, we have to include a file in the theme directory and not the plugin directory?
Am i the only one that thinks that this hampers theme development with all this hardcoded stuff? Anyway, as long as this works it’s cool with me – thank you all for your help!
No, none of bbpress’s functions can be overridden unless they are in pluggable.php
You cannot override the functions in template-functions.php
That’s why filters and actions exist instead.
If you are often trying to replace the entire function, you are probably doing it wrong or in an overly complex way.
Thanks for the input _ck_, as always
I’m not often trying to replace an entire function from template-functions.php, i just want to overwrite one function once. But i get now that it has to be in pluggable.php, which is cool.
I suppose my issue here is this: why are many functions that output hardcoded HTML hardcoded into templates-functions.php in the core and not in pluggable.php so that theme / plugin developers can’t overwrite them?
In my opinion (and it’s just my opinion) there are far too few hooks in BBpress and some of them are in the ‘wrong’ or difficult place for theme development or front-end plugin development. This may seem daft to alot of people and long time BBpress contributors, but with the likes of _ck_ writing so many great back end plugins, most of mine focus on making BBpress work in a slightly more modern way (ajax, nestings, wysiwyg, etc); and i keep hitting these ‘walls’ of where BBpress has hardcoded something.
Basically I struggle to see why BBpress doesn’t have a functions.php file in the theme directory. As it stands we’ve got to create a theme the way we want it, then integrate it into the WordPress theme, then remove the conflicts for the WP and BBpress theme as the standard BBpress theme came with a different div structure to the standard WP one (madness), then add the hooks for all the stuff thats not got a hook, then move hooks for the one in a ridiculous place, then write a plugin to load up all the functions that your theme will use as standard (cos god forbid we’d have a functions.php like wordpress).
It makes developing a theme that does anything more than background colour of posts and what happens around/outside the forum a real nightmare. And what’s worse, it means that any theme to stray away from the ‘default’ kakumei theme, needs to have a plugin installed as well.
While this may seem like no big deal to people (and it’s not a huge deal), given that we use a different structure for our folders to WP (again – crazily stoopid given the market BBpress’s aimed at even at this stage), we’re reliant on the user to install and activate everything properly.
example:
Instructions with a theme if we had a functions.php and WP structure:
- Copy theme folder to your theme directory.
- Load up admin section, and select the new theme.
Instructions with a theme as is:
- Create a my-themes folder in your BBpress directory
- Copy the theme folder into that directory
- Create a my-plugins folder in the your BBpress directory
- Copy the plugin folder into that directory
- load up admin section, and select new theme
- activate new plugin to make theme reliant functions work
Now, obviously there is nothing too taxing on the second version, but it’s long been my experience that the less steps users have to undertake the less errors are made. Especially, and this is the bit I don’t get, when WordPress does things in a really simple manner (and has done so for ages).
I appreciate that forums of the nature we create are never going to be the most exciting or ground breaking in the world, but for those who think i’m wrong about more modern / different theme’s being available for bbpress let me ask this:
- If you look at the Top 20 forums in _ck_’s list (http://bbshowcase.org/forums/view/top100), can you tell it’s a BBpress forum before seeing the “powered by BBpress” at the bottom? If you’re bored do the same for the rest of the top100 and see how many you need to check are running BBpress.
- By comparison, when you go to a non-BBpress forum on the internet, can you automatically tell what forum software it is every time?
- But i bet you can tell it’s NOT BBpress straight away. Why do you think that is?
(infact the first one i’d to scroll to the bottom to check it was made my bbpress and it was – a number have switched away form bbpress – was http://soccerlens.com/forum/ which is using one of my template hacks from last year. the other worth mentioning is http://www.dragonballalcine.com/foro/ which is lovely and innovative – so that’s 2 out of the top100)
I dont suppose this went anywhere?
- You must be logged in to reply to this topic.