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've developed two plugins in which one function is the same (same name, same functionality). When trying to activate both plugins Wordpress throws an error because it doesn't let me defining the function under the same name twice.

Is there a way to make this function private for the plugin only, without using object oriented programming and without simply renaming the function? I don't want to use OOP because I first would have to learn it. Also I wouldn't like renaming the function because I might want to use it in other plugins too and renaming feels not right to do.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

Well, it is not possible, I guess. If you define function in global scope, then it is visible for all code.

The only way to make it private for plugin is to use OOP (and it's recommended way of writing plugins).

You can rename this function, but it won't solve your problem in the long run (still there is a chance that there will be function with the same name).

share|improve this answer

As far as actual scope, no you can't make a regular function private, that is only for class methods (and class variables and so forth). Using OOP for namespacing is not actually that difficult and there should be some good tuts on the interwebs. However, you could make the function, "pluggable" with a function_exists() wrapper. That way the function will only run if the function does not already exist.

if( ! function_exists('your_function') ) {
  function your_function() {
    // do something
  }
}

If you wrap both functions like this, the function that is loaded last should be ignored and you won't get the "function already declared" error.

share|improve this answer

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.