0

I am working on a WP plugin and I want to add a custom function to pluggable.php (located in /wp-includes). I am calling that function from admin.php (located in /wp-admin)

eg. Consider the function auth_redirect which is called from admin.php. auth_redirect is the function defined in pluggable.php which checks for logged users, else it redirects them to login page.

Similarly, I have my own custom function.

So, is there any specific hook or filter which I must use to attach my function to pluggable.php.

Currently, I am using fwrite() to add my custom function to pluggable.php. But, I guess this isn't the right way.

So, can you suggest me the right way of inserting my custom function and calling it from admin.php

Any help will be appreciated.

1 Answer 1

1

You should not and do not need to write to pluggable.php (or any other core WordPress file).

You can override a pluggable function simply by defining it in your plug-in's files (or for themes, functions.php). These get loaded before pluggable.php, and so the function definition given in your plug-in file is used in preference to that in pluggable.php.

You'll want to wrap you function definition inside a function_exists conditional:

if( !function_exists('function_name') ){
   function function_name(){
      return 'Hello World';
   }
}
4
  • Great!!! Thanks for the suggestion. Ok, say now I have defined my custom function in plugins file. But, how do I call it for each page load. eg. auth_redirect is called for each page load since it is called in admin.php. I am checking for a condition in my function and I want to check this condition for each page load. So, how do I call my function for each page load??? I feel calling from admin.php is the only way. Do you know any alternative???
    – Alex
    Commented May 21, 2012 at 15:41
  • @Alex "But, how do I call it for each page load. eg. auth_redirect is called for each page load since it is called in admin.php. I am checking for a condition in my function and I want to check this condition for each page load. So, how do I call my function for each page load???" - That's a separate question entirely, and should be asked separately. Commented May 21, 2012 at 15:51
  • When WordPress calls auth_redirect it will now use your function. You don't (necessarily) call it - WordPress does when it needs to. Commented May 21, 2012 at 15:51
  • Thanks a lot... Finally, got the answer. Stephen, you rock!!!
    – Alex
    Commented May 21, 2012 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.