Jump to content

Questions about WordPress template development


valandor

Recommended Posts

So I really haven't done much as far as WordPress development and I'm currently creating my first basic template. I've been looking through their examples in their documentation, and I have everything loading correctly so far. I have my walker class working for my Foundation menus etc.

 

However, before I get to far along I'd like to get someones opinion on the best practices on creating WordPress templates. While I can create a huge functions.php file I would like to be able to split the template apart into easier to understand bits of code. I would want to create an auto-loading using psr-4 principals and create custom classes separated out into different files. So if it would be to retrieve the template css directory I could use something like $template->get->cssDir(). I would use the built in features of WordPress but build it all into different classes. 

 

So really I am wondering if this is a waste of time, or is it worth it to make the code easier to read or understand for other developers later on?

 

Thanks,

Valandor

Link to comment
Share on other sites

What I would suggest is to split your code up into template files and functionality. Use the theme for all your front end templates, meaning anything that is going to show on the front end.

 

Then put all custom functionality into a plugin. This allows the functionality to be easily reused when the theme is changed. Take a look at http://wppb.io/ for a plugin boilerplate. This is a very popular plugin boilerplate that is great for getting into developing WordPress plugins. If you don't want to use the boilerplate, just use the plugin as a more flexible functions.php file. You can code in any style you like. Though OOP with an autoloader is a good way to go. Also, remember to always prefix your classes and functions, or use namespacing.

 

Next, don't reinvent the wheel. There are a lot of built-in functionality in WordPress core. Your example of $template->get->cssDir() is done with 

get_template_directory_uri() . '/css'

Which compiles out to

path/to/your/theme/css

If you start creating your own code for doing things that are already built into WordPress. You run the risk of something changing in WordPress and breaking your code, and it's just completely useless to a more experienced WordPress developer. I've had to just completely scrap themes before because the previous developer created everything with their own custom code instead of using the baked in functionality.

 

Lastly, get familiar with the WordPress Codex https://codex.wordpress.org/. This will be your lifeblood while developing for WordPress.

Link to comment
Share on other sites

Thank you for your response. I really appreciate you taking the time to answer me. I'll check out the boilerplate and creating a custom plugin, or plugins is a great idea. Once I have more than the basic template up and running I'll be sharing it to so people can use it. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.