How to add shortcodes dynamically to a WordPress Post or Page

Here is a code example of how to add shortcodes dynamically to a WordPress Post or Page. Why do you want to do this? It happened that I had a request from a client to only show an introduction part of the description section and hide the rest with a “read more” feature.

I found this plugin called “WP show more” on the WordPress repository that is able to hide certain parts of content if the content is wrapped inside the plugin’s shortcodes.

For example, to hide the second paragraph to conserve space you need to add the shortcodes before and after the second paragraph.:

Pellentesque elementum interdum nibh blandit scelerisque. Praesent in nibh eu odio finibus ultricies. Sed sit amet placerat justo. Quisque nec diam ultrices, molestie felis ac, convallis augue. Pellentesque euismod mauris at leo feugiat interdum. Praesent tempor lorem eget erat vulputate varius. Aliquam eget cursus nulla. Suspendisse potenti. Integer placerat augue magna, ac pellentesque ante finibus non. Sed vitae eros convallis, imperdiet massa ultrices, placerat nisi.

[show_more]

Vivamus orci erat, dictum vitae ultrices at, interdum tempus ex. Proin varius sodales vestibulum. Etiam pulvinar tristique lectus. Mauris rutrum nisi maximus tortor vestibulum auctor. Maecenas rhoncus iaculis luctus. Aliquam at enim libero. Donec vitae libero ut nisi vehicula aliquet sed eget enim. Praesent ac odio tortor.

[/show_more]

 

Now to do this with every post is time-consuming, especially if the site contains 500+ posts and you want this feature to be applied to all of them. So I wrote this WordPress hook that hooks into the content of each post just before WordPress renders it on the page. In this example, you will notice the feature is applied to WooCommerce’s Product description content only.

The nice thing about splitting the paragraphs in the string into array values is that you have nice clean breaks of the content. Where with the substrate() function substr (string,$string int $start [, int $length ] )  or similar you end up with broken words. To prevent that you need to implement a complex regular expression clause. So the easy way is to just break each paragraph clean.