Following scenario: I'm building a theme framework which I want to reuse in various themes of mine. In this framework there is a collection of many shortcodes. For exemplary illustration of my problem, let's say one of these shortcodes fetches the recent posts ([recent_posts]) and displays them.
Now, maybe I want sometimes to show the post titles above and sometimes below a post thumb image, sometimes I want to show the post date and sometimes not. In short I need some (as handy as possible) way to to change the output code while keeping the shortcode logic itself the same.
Please have a look at the exact point of the concretely described use case: https://github.com/dotwired/Inferno/blob/master/inc/class-shortcodes.php#L591 (the code I want to be flexible starts at the marked line 591 and ends at line 595). I figured out following ways to do so:
- apply_filters():
- pro: feels like doing "WordPress way" ...
- contra: ...but I feel pretty uncomfortable with having too many filters added then (with possibly a large amount of passed arguments, e.g. in case that the post thumb is generated by another class accepting arguments)
- a checking for a template file via file_exists() and then including:
- pro: easy and functional
- contra: would require to add one file for each shortcode where I would want to have the HTML flexible, and the files should be all outside the framework directory. Also, this is surely not the "WordPress way"
- a template string:
- pro: No extra files or modular code needed, just specifying the HTML in the shortcode itself
- contra: easily would get too complicated in shortcode logic and especially in usability.