Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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';
   }
}
share|improve this answer
    
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 May 21 '12 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. –  Chip Bennett May 21 '12 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. –  Stephen Harris May 21 '12 at 15:51
    
Thanks a lot... Finally, got the answer. Stephen, you rock!!! –  Alex May 21 '12 at 17:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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