Questions about Wordpress

Having been away from the WordPress scene since version 2.x days (I think the last time I launched a WordPress website was around 2009), I recently had reason to develop some WordPress plugins, and I wanted to ask some questions about the WordPress coding standards and API that I hope will help enlighten me (and, maybe, other PHP developers coming from other frameworks/platforms to WordPress).

Here are some questions I've had while working on my first WordPress plugin (coming purely from the development side—I'm deliberately ignoring any mention of WordPress's UI, as I don't want to inspire any trolling along the lines of 'WordPress vs. [Another CMS]'):

  • Is it really necessary to mix HTML and PHP all over the place (especially with forms), or are there any documented ways to use templating and preprocessing functions so I can separate my markup from my code?
  • Is there anything like Drupal's Form API? Writing all the form HTML (not to mention form validation, and layout) by hand is tedious.
  • Is it recommended that PHP files include a closing tag? I found a lot of files that did this, but in almost every other system I've used, this is highly discouraged, as it introduces extra whitespace (and other unforeseen consequences) if you end your files with an extra line (as many do).
  • Is it okay to not include a bunch of space around logical statements and array/function parameters like( 'so' ); and if ( ! empty( $title ) ), or is that a strict rule in WordPress's coding standards.
  • According to the Coding Standards doc, it's okay to have a one-line if statement without brackets—in all my experience, in every language (except Python, of course), experience has taught me this is one of the worst ideas ever. Is this an anachronism, or is it actually recommended? Some parts of the Codex contain examples of this strange and never-recommended if statement syntax.
  • For a new contributor, where should I start? I'd like to get some code up on WordPress.org and start figuring out how bug tracking, versioning, and plugin development works, but there's nothing like Drupal.org's developer sandboxes that I've found yet.

I have some thoughts about other odd conventions, like method, class, and parameter naming, but I can understand simple style differences there and in some other areas. The main questions I have (above) are not meant to denigrate WordPress or anything like that, I'm just wondering (a) what the rationale is behind some of those coding standards/conventions, and (b) if I'm just seeing outdated documentation in the Codex, API, and other sites...

[Edit: I've also posted a question with some more detail on the WordPress.org forums: WordPress Development (Coming from Drupal)].

Jeff Geerling is the owner of Midwestern Mac, LLC. He is an active contributor in the Drupal community, and has primarily been a Mac user since the 1990s. He is the author of Ansible for DevOps, and learns and uses whatever tool or language is best for the job.

Comments

Kraft's picture

I've found that Github is where a lot of dev's keep their true development code, tracking, verisioning, etc while only using WP.org's SVN repo for releases. Github is too great not to use.

As mentioned on Twitter, I've done very little on the plugins side of the WordPress coin, so I'll let the more experienced folk tackle those questions.

Jeff Geerling's picture

GitHub++ — If Drupal.org didn't have some pretty awesome Git integration, I think the situation would be the same there. It would be awesome if WordPress.org could find a way to automatically integrate with GitHub for projects. I hate having to sync a bunch of different VCS repositories. (Of course, with Git, it's not nearly as hard as it was back when some people used CVS, others SVN, and others uploaded ZIP files or the like...).

Personal site: jeffgeerling.com.

Kien Tran's picture

Is it really necessary to mix HTML and PHP all over the place (especially with forms), or are there any documented ways to use templating and preprocessing functions so I can separate my markup from my code?

It's "possible" to use outside libraries for templating, but you need to make very sure you load and unload them as needed in each plugin. Wordpress can't handle loading something that's already loaded automaticaly.

Is there anything like Drupal's Form API? Writing all the form HTML (not to mention form validation, and layout) by hand is tedious.

Nope...use a form plugin from someone else.

Is it recommended that PHP files include a closing tag? I found a lot of files that did this, but in almost every other system I've used, this is highly discouraged, as it introduces extra whitespace (and other unforeseen consequences) if you end your files with an extra line (as many do).

You should ALWAYS close php files imo. The reason people don't is because they may sometimes add whitespace to the end of the file after the close tag...which will break things like AJAX. Not having the tag keeps the whitespace as part of that module. Simple solution, don't add white space after the php close tag.

Is it okay to not include a bunch of space around logical statements and array/function parameters like( 'so' ); and if ( ! empty( $title ) ), or is that a strict rule in WordPress's coding standards.

I dunno...i hate all that white space.

According to the Coding Standards doc, it's okay to have a one-line if statement without brackets—in all my experience, in every language (except Python, of course), experience has taught me this is one of the worst ideas ever. Is this an anachronism, or is it actually recommended? Some parts of the Codex contain examples of this strange and never-recommended if statement syntax.

The wordpress Codex being contradictory sounds about right

For a new contributor, where should I start? I'd like to get some code up on WordPress.org and start figuring out how bug tracking, versioning, and plugin development works, but there's nothing like Drupal.org's developer sandboxes that I've found yet.

Git it on your own dev box, and svn push it to wordpress when you are ready for it. Check the Codex. (i don't do a lot of plugin dev)

Jeff Geerling's picture

I dunno...i hate all that white space.
[...]
The wordpress Codex being contradictory sounds about right

lol, I guess nobody can produce perfect documentation...

You should ALWAYS close php files imo. The reason people don't is because they may sometimes add whitespace to the end of the file after the close tag...which will break things like AJAX. Not having the tag keeps the whitespace as part of that module. Simple solution, don't add white space after the php close tag.

I disagree, mostly because (a) Git complains if you don't have whitespace at the end of a file, and I don't like Git complaining ;-), and (b), I've already seen a few WP plugins adding whitespace after closing tags, and (c) this, basically. I think that it's better to just leave 'em out altogether, and not allow any inline HTML markup in .php files... but according to your first answer above, that may not be doable with WordPress :(

Personal site: jeffgeerling.com.

Add new comment