0

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.

2 Answers 2

0

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).

0

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.

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.