I’m having trouble figuring out the best way to implement custom sections in a WordPress theme. For example I’m making a theme that will have four featured sections on the homepage. These sections will be in there own area, separate from the page content.
I know this could be done by adding four widget positions, or one widget position and making the user add four widgets. You could also add the four areas into the theme options panel, or even try and use a page builder plugin. I was wondering if anyone has any advice on which method is best and why? Or any suggestions on alternate methods?
Thanks,
David
The base paradigm in WordPress is The Callback Handler. Actions, filters, widgets, metaboxes and so on … everything runs by registering specific callback handlers for some triggers. This is not always the most elegant way, but the one every beginner must learn, so stick to that paradigm, whenever you’re not sure what to do.
So offer four actions:
Then you or a third party developer can register a callback for these actions with
add_action()
.This could be improved: Use a prefix to prevent collisions with plugins or WordPress core changes, and run through an array to keep the code compact and readable.
Now, this might already be too long for a plain template, so you could encapsulate the code in a custom function and register that for another custom action:
Now you can print a custom container only if there are any callbacks registered. Third party developers can register their own callbacks or remove yours. Your
front-page.php
needs just one additional line of code.