When building out modular templates, we output content in two different ways based on the context.
The full output (content, markup, dynamic content) is used for display on the frontend. A simplified content-only version (no theme markup, no dynamic content) is saved to the
post_content field for search indexing and future-proofing.
The dynamic features of the module are included as placeholder shortcodes – shortcodes that haven’t been built out and are currently disabled.
If I have a “Recent Posts” module that includes title, intro content, and number of posts to display, the content we add to the
post_content field may look like this:
|if( !empty( $module[ 'title' ] )|
|echo '<h4>' . esc_html( $module[ 'title' ] ) . '</h4>';|
|if( !empty( $module[ 'intro' ] )|
|echo wpautop( $module[ 'intro' ] );|
|echo '[module_recent_posts count="' . intval( $module[ 'count' ] ) . '"]';|
Then towards the bottom of the modules.php file we’ll have:
|* Future Developer: These placeholder shortcodes are for you. If you are not using the module|
|* template in your new theme but would like to retain the dynamic features (ex: recent posts),|
|* build out these shortcodes. The shortcodes have been included in the post content, but the|
|* `__return_false` makes them not visible on frontend. This way you can add back that functionality|
|* without having to manually update every page that needs it.|
|add_shortcode( 'module_recent_posts', '__return_false' );|
When the theme changes and the “Modules” template is no longer available, it will use the default template and output the post content. These shortcodes will be in the content area, but not appear on the frontend because of the
When the client or a future developer changes themes, they can:
- Build a modular template for the new theme, in which case the
post_contentis not used on the frontend.
- Build out these shortcodes, so the
post_contentfield is used and the shortcode displays a listing of recent posts in the new theme’s style.
- Don’t do anything. No recent posts are shown on the page, but the unused shortcodes are not visible to site visitors.