Best Practices for WordPress Theme Development
WordPress is an open source CMS, and when it comes to custom theme development, we have to play by it rules. Writing the theme “the WordPress way” makes that 3rd party plugins are supported and decreases the time of development significantly. There is no need to reinvent the wheel. Let’s take a look at most common good practices.
- Using wp_enqueue_scripts hook to register the css and html files. If you just write the raw html to include that files in header.php, minification plugins won’t be able to reach it and use it’s power. Scripts and styles should enqueued using the functions wp_enqueue_script and wp_enqueue_style
- Setup the thumbnail size using add_image_size instead of making the browser resizie it. WordPress has a great thumbnails system which just works and is easy to do
- Use build in menu system instead of hardcoding it in html. You can register new menu position using register_nav_menus function. Hardcoding the menu in html will make you do additional work, like handle proper url generation (in case if you change the url of the website) or take care of adding css classes to current page element. Seriously, wordpress menus has it all default. If your menu is not extremely sophisticated, there is no need to use anything else
- If you’re developing theme from scratch, don’t forget about add_theme_support(‘post-thumbnails’) otherwise posts and pages won’t have the featured picture.
- Use custom post types to store data and custom taxonomies to categorise it. Usage mysql queries and custom mysql tables is not always a good idea, especially if you want to create something similar to post or page. Custom post types are not only a way to store the data, they can be added to wp menus, they have their own urls and all benefits of regular posts, like CRUD. If you try to do this by your own, you gonna just waste your time
These 6 points are a very basic rules of custom theme development and of course we follow them while doing our themes.