WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to Override a Function (17 posts)

  1. frank thirteen
    Member
    Posted 2 years ago #

    I read a WP "how to" thread on overriding a function.

    I am trying to override a function that exists in pluggable.php. I am under the impression that all I needed to do is name my function the same thing as it is named in pluggable. Because pluggable (supposedly) first checks to see if the function exists, it will not try to recreate an instance of it. That was my understanding from reading the PW thread.

    So I went to activate my plugin with the identical function name that exists in pluggable and I got an error:

    Plugin could not be activated because it triggered a fatal error.
    Fatal error: Cannot redeclare wp_new_user_notification() (previously declared in /home/abcd/public_html/blogs/wp-includes/pluggable.php:1182) in /home/abcd/public_html/blogs/wp-content/plugins/my_new_user_notification/plugin.php on line 30"
  2. You would want to use a child theme, and in the functions.php file of your child theme would be something like this:

    if ( ! function_exists( 'wp_new_user_notification' ) )
       function wp_new_user_notification() {
         YOUR STUFF HERE....
       }

    EDIT: In PHP, you can NEVER have two functions with the same name (unless you are using a class and the object-oriented-approach). This is why you are receiving the error.

  3. frank thirteen
    Member
    Posted 2 years ago #

    Thanks @Josh. Yes, I know about not using the function_name() twice.

    So if I understand what your saying, I need to move my function to functions.php of my theme rather than adding it as a plugin?

  4. frank thirteen
    Member
    Posted 2 years ago #

    That did not work. I got a white screen -- so I interpret that to mean there was a server side issue when I updated functions.php

  5. frank thirteen
    Member
    Posted 2 years ago #

    Does anyone know if it is possible to shut off a WP function and make your own in its place?

  6. frank thirteen
    Member
    Posted 2 years ago #

    Has anyone had any success overriding a WP function without having to change the core source code files? If so, how did you do it?

  7. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    so moving the function from a plugin to functions.php isn't necessary. It will do the same from either location. I actually prefer it in a plugin, as that way if something gets hosed, it shuts the plugin off rather than WSOD the whole site.

    so, looking at pluggable.php,

    if ( !function_exists('wp_new_user_notification') ) :
    /**
     * Notify the blog admin of a new user, normally via email.
     *
     * @since 2.0
     *
     * @param int $user_id User ID
     * @param string $plaintext_pass Optional. The user's plaintext password
     */
    function wp_new_user_notification($user_id, $plaintext_pass = '') {

    starting on line 1188, it is indeed pluggable. Which my understanding is always that you can redeclare a pluggable function, and your new function will take precedence.

    But, when trying to do just that, I have the same results as you... So I will dig around and try to figure out what the deal (in the meantime, maybe someone smarter will swing by with the answer!)

  8. frank thirteen
    Member
    Posted 2 years ago #

    Thanks @Rev.Voodoo, I appreciate any help I can get. Once I get the process nailed the first time through, then I can obviously repeat this for any other function I wish to override. Getting the 1st one going is just the time consuming and frustrating part.

  9. prionkor
    Member
    Posted 2 years ago #

    I am not an expert on this but this function is pluggable and you should be able to override it from plugins. But it seems pluggable php file runs before your plugin so it gets already defined.

    what you could try is defining the function like @josh said and see if your modification on this function runs. Or try to hook the function to the muplugins_loaded perhaps?

  10. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    http://codex.wordpress.org/Pluggable_Functions

    Here's the documentation on pluggables.... which seems to reinforce our understanding of same...

  11. frank thirteen
    Member
    Posted 2 years ago #

    Thanks @prionkor. I tried Josh's suggestion and I got a white screen (documented above in thread).

    Thanks @Rev.Voodoo -- I read that earlier and tried it out but had no success.

    I really do not want to touch the source code, but it is looking like I have no other choice.

  12. I missed a colon in my original post... Note the colon below:

    if ( ! function_exists( 'wp_new_user_notification' ) ) :
       function wp_new_user_notification() {
         YOUR STUFF HERE....
       }
  13. frank thirteen
    Member
    Posted 2 years ago #

    Ok @Josh but that does not solve my problem.

  14. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    http://sltaylor.co.uk/blog/customizing-new-user-email-pluggable-function/

    Have you seen this article at all? I haven't had time to test yet - but it seems we definitely want to go the plugin route, and not with functions.php, due to load order

  15. frank thirteen
    Member
    Posted 2 years ago #

    Thanks @Rev.Voodoo. That seems to be the answer then.

    Copy the whole function from pluggable.php. Include the function_exists() check it’s wrapped in. If you include this, your function will still get priority over the WP core; it’ll just prevent fatal errors if you activate a plugin that happens to have overridden the same function.

    Even though it does not make logical sense. I'll give it a try and report back.

  16. frank thirteen
    Member
    Posted 2 years ago #

    You're the hero of the hour @Rev.Voodoo!!! Great find at http://sltaylor.co.uk/blog/customizing-new-user-email-pluggable-function/

    Even though it makes no sense that it would work this way, the answer is you need to wrap your overriding function with the function_exists().

    That's the trick.

  17. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    Whew... glad we worked through that....

    As you said, it doesn't make a lot of sense to have to wrap it that way - unless the function is being modified elsewhere as well

    When I tested out my code, I did it on a vanilla WP install, no plugins active to be redefining the function... but, oh wells!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags