-2

EDIT: I've posted them separately here wordpress.stackexchange.com/questions/137672/… and here wordpress.stackexchange.com/questions/137673/…. I'll vote to delete this one.

I'm working on a Wordpress site, and I'm having a few questions about structuring my PHP code. Although I've have experience with structuring code in other frameworks, I'm wondering how you would do this in Wordpress. I might have known some of this if I was more fluent in PHP.

More specifically:

  • Separation of view and logic

I find that very easily my views become cluttered with logic, e.g. null checks, email validation, and retrieving values from the post object. How would you separate this out in Wordpress? Is there any sort of MVC framework?

  • Common functionality between my own plugins

I read that when creating new post types, creating a settings page, and creating a new taxonomy, you should create a plugin. So I have done that. But I have functionality, which could be generalized, or are identical, in my plugins. An example of this would be the function which saves to the database. Since everything seems to be put in the global scope, if there is such a thing in PHP, should I create another plugin with shared functionality, or put shared functionality in the functions.php file?

  • Is everything global?

As I mention above, is everything global in Wordpress? My impression is that other plugins might very well have function names that clash with my function names; that's why you prefix functions with your themes theme name. Is there a way to avoid this?

Thank you for your time, and sorry if this is duplicate questions.

4
  • 1
    hmm I think you'd be better asking your 3 questions separately, we have a 1 question per question policy to prevent confusion
    – Tom J Nowell
    Mar 11, 2014 at 18:21
  • Googling wordpress mvc found this: wpmvc.org, which might be what you're looking for with your first question. Also related: this answer on Stack Overflow. Possibly also of interest: Tom McFarlin's post on the topic.
    – Pat J
    Mar 11, 2014 at 19:16
  • 2
    I can answer two of those questions thoroughly and definitively, but not the first; as such any answer that I could supply here would only be a partial answer. As @TomJNowell suggests, it would be best if you separate this SE question into three specific questions.
    – bosco
    Mar 11, 2014 at 19:55
  • @boscho I've posted them separately here wordpress.stackexchange.com/questions/137672/… and here wordpress.stackexchange.com/questions/137673/…. I'll vote to delete this one.
    – ptf
    Mar 12, 2014 at 8:11

1 Answer 1

1

I find that very easily my views become cluttered with logic, e.g. null checks, email validation, and retrieving values from the post object. How would you separate this out in WordPress? Is there any sort of MVC framework?

There is no clear separation enforced by WordPress and there is extremely wide range of styles and opinions on this. Simply put you are either on your own managing this in your code or you can look for third party framework and/or convention to make use of.

should I create another plugin with shared functionality, or put shared functionality in the functions.php file?

Since everything is in global scope the question "where things go" is primarily semantic rather than technical. Again you either look for opinion or make up your own about it.

As I mention above, is everything global in Wordpress? My impression is that other plugins might very well have function names that clash with my function names; that's why you prefix functions with your themes theme name. Is there a way to avoid this?

Use PHP namespaces. Well, as above even this is more of an opinion since natively WP aims for compatibility with obsolete PHP 5.2 version and isn't making use of namespaces in core.

Your Answer

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

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