How to overwrite a function?
-
Hey folks,
When you visit a group, the first page which opens should be the forum. So I looked at the code, where I can change it. Sadly this isn’t defined in the templates, but in the core with the following code (bp-group.php):
bp_core_new_nav_default( array( 'parent_slug' => $bp->groups->slug, 'screen_function' => 'groups_screen_group_home', 'subnav_slug' => 'home' ) );
When I change
- ‘subnav_slug’ => ‘home’
to
- ‘subnav_slug’ => ‘forum’
I get the change I want, but I don’t want to mess around with the core code. I’m scared that this way all my modifications will be reset after the next update.
I think it should be possible to create a fille “modificiations.php” and upload it to the ordner “mu-plugins”. In this file I would like to overwrite the functions, so that I have the change I like. But sadly I don’t know how I can overwrite the function.
Would be great if someone could help me.
Regards
-
You can create a file called /plugins/bp-custom.php and BP will automatically be “aware” of it. You can put custom functions there.
thanks for the statement, but my problem is, that I don’t know how to overwrite the function.
I’ve inserted the file bp-custom.php with the following code:
bp_core_new_nav_default( array( 'parent_slug' => $bp->groups->slug, 'screen_function' => 'groups_screen_group_home', 'subnav_slug' => 'forum' ) );
But that i get the following error message:
Fatal error: Call to undefined function bp_core_new_nav_default() in /home/www/wp-content/mu-plugins/bp-custom.php on line 3
Hope anyone can help me.
I am not a professional programmer but I don’t think you have created the function properly? The usual format of a function is
bp_function_name () {
Insert code here
}
sorry if I am misunderstanding.
OK, I think I have a lead for you.
The function you want to replace may be groups_setup_nav() which is located in bp-groups.php
I believe this is the function that decides (among other things) what the default subnav display item is.
I would first edit groups_setup_nav() to see if it works the way you think it will. If so, then undo your edit, copy the function to bp-custom.php, and rename it my_groups_setup_nav, redo the edit, and then unregister/reregister the function by adding this code to bp-custom.php:
remove_action(‘bp_setup_nav’, ‘groups_setup_nav’);
add_action( ‘bp_setup_nav’, ‘my_groups_setup_nav’ );
The remove_ and add_action SHOULD work but I may be wrong that you can simply drop the remove_action and add_action in bp-custom.php and expect it to work. This BP tutorial might explain the remove/add process better: https://codex.buddypress.org/how-to-guides/modifying-the-buddypress-admin-bar/
@heini – If you overwrite the existing bp_core_new_nav_default line in bp-groups.php with
bp_core_new_nav_default( array( 'parent_slug' => $bp->groups->slug, 'screen_function' => 'groups_screen_group_forum', 'subnav_slug' => 'forum' ) );
it will work.
I can’t seem to make bp_core_new_nav_default work anywhere else. Maybe someone else has some ideas, or maybe it’s just not part of the API to override this right now (like it is with profiles and BP_DEFAULT_COMPONENT).
Another way to do it, which is less elegant but possible without hacking core files, is to put something like this in your bp-custom.php file:
function redirect_to_forum() {
global $bp;
if ( bp_is_group_home() )
$bp->current_action = 'forum';
}
add_action( 'bp_init', 'redirect_to_forum' );you are right, thats the function. I will try to work with it a while. At the moment I get only a white blank page ;(
Have to try a bit more
@Boone Gorges
This way definitely works, sadly it has the side effect that you wont be able to see the activity stream in the group anymore. Maybe I find a solution for this.
I really don’t know what to do. With the method from @3sixty I can overwrite parts of the function, but not all.
When I use the same code which works at the core code, it won’t work anymore as a plugin
heini – Just to be clear: What @3sixty is suggesting is that you copy the *entire* groups_setup_nav() function (it’s pretty big), paste it into bp-custom.php, change the name (eg to my_group_setup_nav), make the change to bp_core_new_nav_default, and then do the remove_action/add_action stuff outside the function.
If you do this right, then it will replace the entire function, though most of it will be replaced by the same code.
I did it exactly like this.
I insert the following code into the file bp-custom.php and uploaded it to mu-plugins
<php
function my_groups_setup_nav() {
[the whole function code]
}
remove_action('bp_setup_nav', 'groups_setup_nav');
add_action( 'bp_setup_nav', 'my_groups_setup_nav' );
?>
I even didn’t make any changes at the code, so it’s for 100% the same function code like in the core. But anyway there are coming now some errors. When I go to the group page the first page is the same like always (regardless of what I do at the new function) but I can’t go to another page of the group anymore. It will always show only the start page.
I’m pretty sure that the bp-custom.php file needs to be in the “plugins” directory, rather than the “mu-plugins” directory.
@airfoil is right. That might be the cause of your problems.
I also tried it with “plugins”, but sadly there is excaktly the same error
Here is the file I used:
http://pnex.de/bp-custom.php.txt
Really don’t know why it isn’t working
@heini-
Your basic idea is correct. The issue is the order in which each action function hooked to the bp_setup_nav action event is being triggered. Let’s look at the code you have in bp-custom.php.
<php
function my_groups_setup_nav() {
[the whole function code]
}
remove_action('bp_setup_nav', 'groups_setup_nav');
add_action( 'bp_setup_nav', 'my_groups_setup_nav' );
?>This fails because your custom code in bp-custom.php is executed before the code in bp-groups.php is executed. So, you are calling to remove an action that has not yet be registered by WordPress.
I’m almost finished with an article about the intricacies of WordPress’ action and filter functions, but here is a brief explanation to try to shed some light on the issue.
Each action and filter event when triggered by a do_action or apply_filters call sends execution to the appropriate do_action or apply_filters function in /wp-includes/plugin.php. These functions do several things, one of which is querying a very large array that contains all of the added filter functions and action functions. This array gets populated by the actions of the add_filter and add_action calls.
To avoid getting too long winded, as each file gets loaded, any and all add_action and add_filter calls that are directly executable at the time, get triggered which builds this big array. This array is then used by the do_action and apply_filters functions to determine the sequence in which each added action of filter will be executed.
You can read my article when it is ready for a much longer but clearer explanation.
With that briefly described, the piece of information that applies to your situation is that when the action event for bp_setup_nav is triggered, via the do_action( ‘bp_setup_nav’ ) call on line 2025 in bp-core.php, the do_action function starts processing each action function that was “added” to it in the order in which it was added (with an important exception). This means that for your custom function my_groups_setup_nav to work, it must remove the action function in the array discussed above so that when the bp_setup_nav action event is triggered it uses your action function instead of the one added by the add_action(‘bp_setup_nav’, ‘groups_setup_nav’) call.
So, what you need to do is trigger your remove and add action calls in the proper sequence–after the action function that creates the groups navigation menus has been added to the filter array but before the bp_setup_nav action event is triggered. I realize this may be hard to understand.
This is what you need to do instead:
<?php function my_groups_setup_nav() {
/* Remove the added action from the array of functions that get run
* when the action event for bp_setup_nav is triggered
*/
remove_action('bp_setup_nav', 'groups_setup_nav');
// Then replace the removed function with your custom content
[the whole function code]
}
add_action( 'plugins_loaded', 'my_groups_setup_nav' );
?>A final bit of clarification. This is what happens in your current custom function:
1. As soon as BuddyPress is initialized, it looks to see if bp-custom.php exists. If it does, it loads it.
2. This will cause your remove_action call to be fired.
3. But the corresponding action has not yet been added to the very large filter array since bp-groups.php has not yet been loaded.
4. So, nothing happens as there is nothing to remove.
5. bp-groups.php eventually gets loaded and at that point the add_action(‘bp_setup_nav’, ‘groups_setup_nav’) call is fired, resulting in the action function that you were trying to remove getting added to the filter array but after the fact.
6. Eventually the bp_setup_nav action event is triggered.
7. When that happens, the first function hooked to it (added to it) is your custom function. It now gets triggered.
8. All other action functions hooked to the bp_setup_nav action event get triggered one by one including the groups_setup_nav function–the one you had tried to remove in step 2.
9. You have a mess with two duplicate functions doing the same thing.
Great explanation Jeff and very helpful. I’m really looking forward to the full article. Where can we watch for it?
I’m not sure if this is part of the article too, but I’d love some description of passing in variables to the various actions and filters. Seems like a few examples of how the actions can be implemented and a description of how the core code will deal with the actions that were added would be really beneficial.
@techguy-
You can check my blog but I’ll also try and remember to post a link to the article within this thread. It will be in a week or so as I’ve turned my attention back toward BP privacy.
It works
Thanks a lot for this great explanation.
Looking forward to read your article in your blog.
Great! I’m glad that you’re back in business. I’m setting this issue to resolved.
It worked fine until the last Buddypress Update. When Ih go in a group now, I will be redirected to the mainpage. I already updated the older function in the custom.php with the new one, sadly it doesn’t work.
Anyone knows what to do?heini, this works:
http://is.gd/cl9Ydthanks ray, but sadly it doesn’t work
I located the error, but don’t know how to fix it.
The arrays “$bp->bp_options_nav” AND “$bp->bp_options_nav” are always empty/false.But maybe I made a mistake at some place, so I would like to describe how I did it.
I created a new empty file custom.php (deleted the old one) and I insert your code to this file
http://pastebin.com/j3n17CVeAfter this I uploaded it to wp-content/plugins. Thats all.
bp-custom will be too early for that to work, as you’ve found out. Try putting it into your theme’s functions.php.
thanks for your feedback. Actually I already tried it, but I get exactly the same error.
“$bp->bp_options_nav” AND “$bp->bp_options_nav” are always empty/false.
- The topic ‘How to overwrite a function?’ is closed to new replies.