0

While developing more and more customized Wordpress themes, I began adding more custom functions to my theme's functions.php file. While I try to make their names to be self-explanatory, I think this is not enough.

Considering that you might not be the only web developer working on a theme, or that there might be somebody else who will be put in charge of continuing the development of said theme, I wonder, how should we name our functions to distinguish them from core WP ones?

Edit: Re-worded the question Q: Are there any patterns that help developers co-working on the projects namespace functions to avoid confusion whether they are native to Wordpress or not?

  • 1
    Take a look at Objective Best Practices for Plugin Development? - there are several answers including proposals about naming/prefixing functions and using namespaces. It applies to themes too. I doubt there is more to say to your question. – Nicolai Nov 4 '13 at 16:48
  • 1
    Close-voted as primarily opinion-based: "Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise." – Chip Bennett Nov 4 '13 at 17:59
  • Thanks a lot for the link - very useful piece of information. – Maciej Gurban Nov 4 '13 at 21:49
1

I almost used the same convention as @GhostToas but to me I usually use classes to minimize conflict. For internal or company project: class MyCompany_Classname {} For Client specific project: class ClientProject_Classname {} Using a class will absolutely help you avoid conflicts and thus, you can name your function using generic function names. Say for example:

class MyCompany_ThemeSettings {
    function saveSettings() {
    }

    function displaySettings() {
    }

    function getSettings() {
    }
}
| improve this answer | |
1

As I work for a company, I typically prepend my function names with our company nickname, for functions that tend to stick around. Example: mycompany_custom_excerpt().

For project-specific functions, I tend to use a nickname that represents the project (such as the theme's name). Example: client_related_physicians_check().

Or you could just as easily use your unique handle if you are a freelancer. For example: ghosttoast_hide_admin_user().

Where those prefixes may be found in many functions. But if you are creating so many functions to live in functions.php that you are getting confused, perhaps you should consider including an external file. Example: mycompany_functions.php.

Or consider developing actual plugins that you can reuse across future products: mycompany-custom-excerpt.php.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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