There is a situation when we want to include post content in another post, page or elsewhere (like widgets). In my case, I'm using this popup plugin, which provides an editor for users to add popup content, but my client - who is not familiar with all settings - doesn't want to use the settings page (which has a lot of things that he can't understand) to add content. So I want to provide them an easier way to enter content for the popup and my solution is just utilize Page in WordPress.
The idea is very simple: instead of force users to go to the settings page to enter content, let them create a new page and include the page content into the settings page via a shortcode.
In order to do that, I need to create a shortcode to include post content:
Creating shortcode
The shortcode to include post content looks very simple:
// [post_content id="..."]
add_shortcode( 'post_content', function( $atts )
{
return wpautop( get_post_field( 'post_content', $atts['id'] ) );
} );
After creating shortcode, just put [post_content id="123"]
into the settings page. You have to make sure the settings page processes shortcodes.
And now, users don't have to go to the settings page and don't need to know all about the settings. All they care is just the popup content and now they can just go to Pages, edit the page and the popup is automatically updated! FTW!
I've been using post content for a while and feel very happy with it. You can apply this technique in some cases such as:
- Allow user to add HTML code (including images) for widgets
- Change gateway descriptions in WooCommerce
- Use
[post_content]
shortcode to build complex layout for homepage
More advanced, you can use more tag to separate 2 parts of the post content, or use post excerpt if you need secondary content, etc.
Conclusion
There are various options where you can let users enter their content and most developers think about a settings page as the first choice. But from the point of view of users, it's not user-friendly as they have to understand everything in that settings page. Utilizing page content as shown in this post may be the better way as it's more friendly and only use built-in feature of WordPress.
Leave a Reply