The Project
The Problem
The problem isWhen Wordpress comes across the first image shortcode in a row, it's expected that it will be replace by a real <img>
tag at that point, but I don't yet know at what size it's supposed to be displayed. There is also no built-in way for the shortcode parser to know when it has reached a line-break.
I need to know how many image shortcodes are on a given line (in the "row") and use all of their dimensions together to calculate the desired sizessize of each image before I'm ready to replace the first shortcode in the row. As far as I've been able to tell, Wordpress can only deal with each shortcode one at a time in the order they are found, and there doesn't seem to be an easy way to tell where the line breaks fall.
Solution?
Am I missingoverlooking an easy solution? Ideally I'd like An ideal one would...
- Leave the images easily separable -- using one tag per image so I can easily reorder them by dragging and dropping between rows, or even between posts (plus, as I mentioned in a comment below, my actual shortcodes are much more complex than my example and would be extremely unwieldy if combined into one tag per row)
- Use Wordpress' built-in shortcode support -- since that's the preferred method for plugins to embed content, and Wordpress has nice parsing tools already built-in for that purpose
- Not require "parent" tags for each row of images -- to keep the code simple and flexible for future development
- Remain as future-proof as possible -- e.g. if I wrote my own complex regex for this and overloaded
do_shortcode()
then there's a greater chance that this could break in the future as Wordpress evolves
Compromise...
After several hours of research I haven't been able to keep this very simple -- working with the built-in shortcode functionality and avoiding nested shortcodes or complicated custom regex matching if possiblefind anything that would meet all these criteria. What's the best way If I have to approach this?compromise on these points, I would start from the bottom of that list. For example...
- Rolling my own
do_shortcode()
and/or modifying the (very complex) regex pattern (defined inget_shortcode_regex()
) -- these are the kinds of creative solutions that I am most interested in - Using parent tags to encapsulate each row, e.g.
[imgrow][img ...][img ...][/imgrow]
-- though that doesn't really answer the question as asked - Abandoning the shortcode tools altogether, e.g. implementing a broader custom filter using
add_filter()
-- which technically maintains the shortcode syntax but gives up all the nice tools (and at that point the answer to this question would simply be "it's not currently possible")