Skip to:
Content
Pages
Categories
Search
Top
Bottom

how to override buddypress function using hook?


  • Mitesh Patel
    Participant

    @mitesh-patel

    I am developing locally with all recent versions on wp, bp and bbp.

    Now, I need to override one of the redirect from one of the bp functions namely, messages_screen_compose(), which resides in buddypress > bp-mesages > bp-messages-screen.php

    the relevant line of the function I need changed conditionally (when called from my intended page), without affecting it’s original role is
    `
    bp_core_redirect( bp_loggedin_user_domain() . $bp->messages->slug . ‘/view/’ . $thread_id . ‘/’ );
    `

    Can I do that using action or filter hook?

Viewing 7 replies - 1 through 7 (of 7 total)

  • shanebp
    Moderator

    @shanebp

    bp_core_redirect calls wp_safe_redirect which is pluggable, meaning you can replace it with a function that includes a check for the component and action within the redirect url.


    Mitesh Patel
    Participant

    @mitesh-patel

    Sorry @shanebp, for delayed response. Got disconnected for a while.

    I don’t understand how to do what you suggest. Any pointers in right direction? I’m not a programmer, but I will understand if I could analyse an example.

    Also, if I ‘plug’ the function, as you suggest (not modifying the actual function but by an action hook), will my conditional statement run every time a call to wp_safe_redirect is made from other places (not my form) as well? as wp_safe_redirect would be used hundreds of time.

    Or, can I not rewrite the entire function messages_screen_compose() so that it overrides the one in buddypress, and put it in functions.php of my theme? If that’s possible, what’s correct way of doing it?


    shanebp
    Moderator

    @shanebp

    I’m not aware of a filter for this, but you can definitely over-ride wp_safe_redirect in a manner that doesn’t affect its usage otherwise, ie. check the $location and adjust if applicable.

    To over-ride it, you must do so within a plugin. It’s not about actions or filters.

    More info: https://codex.wordpress.org/Pluggable_Functions

    >Or, can I not rewrite the entire function messages_screen_compose() so that it overrides the one in buddypress, and put it in functions.php of my theme?

    No. And it would be a bad idea even if you could.


    Mitesh Patel
    Participant

    @mitesh-patel

    Thanks @shanebp. A couple of questions before I mark this thread resolved one way or other.

    It seems a whole lot labour to write a plugin (which I’ll have to learn first 🙂 ) for a solitary redirect, but can I not do it in bp-custom.php, which seems to be for the same type of purposes, and conveniently resides in plugins directory?

    Secondly, $location refers to the url of the page from which action originated as opposed to the location where the page will be redirected afterwards, right? I checked the referred wp codex page before I asked the earlier question, but couldn’t figure out what it meant.


    Mitesh Patel
    Participant

    @mitesh-patel

    Never mind, the $location refers to the url of the page where we will be redirected afterwords.

    Will post back, If I ever get it done.


    shanebp
    Moderator

    @shanebp

    A plugin can be very simple.
    For example, maybe all it does is over-ride a pluggable function.

    You can try it in bp-custom.php – which I believe is loaded with all the plugins.
    So it might be loaded before pluggable.php is loaded, therefore it might work.


    Mitesh Patel
    Participant

    @mitesh-patel

    I finally yielded, and messed up (so to speak) the function messages_screen_compose, adding conditional custom redirects with if else statements like…

    `
    if(wp_get_referer()){//to check origin of form submission, returns false for self submission
    bp_core_redirect(wp_get_referer()); //custom redirect, as this isn’t self submission
    } else {
    bp_core_redirect( bp_loggedin_user_domain() . $bp->messages->slug . ‘/view/’ . $thread_id . ‘/’ );// this was originally there, for submission from original php file
    }
    `

    As I think the issue is ‘resolved’, for time being, I should document the solution here.

    I intended to use buddypress private messaging as feedback form, with various feedback options like Question, Suggestion, Feedback etc. Thus, I created corresponding users (Question, Suggestion, Feedback etc.) who will receive the feedback. The idea is super-elegant from my purpose, as not only the submissions be automatically segregated and stored in database, but responding to the feedbacks would be as simple as responding to PM, all without the hassle of email, spam etc. An added benifit is, users can draw attention to various issues site wide by @mension, like :@question What about this?”

    So I tried to imitate the buddypress compose form, and I succeeded, except the redirect problem as described here.
    https://buddypress.org/support/topic/create-a-feedback-form-using-buddypress-messaging-functionality/

    What I was doing was, creating a duplicate form, but calling the original form action (fooling buddypress 🙂 ). I did so, because, I don’t even know how to submit the copy-pasted form I created, and get the same thing done as the original compose.php was doing when it called itself (self submission).

    Anyways, this hack with unforgivable modifications in buddypress files works beautifully. I feel I’ll have to go through the plugin route finally though, as this is make-shift arrangement (but will work as long as I don’t update the buddypress plugin 🙂 ).

    Thanks shane for your help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘how to override buddypress function using hook?’ is closed to new replies.
Skip to toolbar