wp-shortcake / shortcake

Shortcake makes using WordPress shortcodes a piece of cake.
GNU General Public License v2.0
664 stars 142 forks source link

Support parameter name aliases #133

Open bobbingwide opened 9 years ago

bobbingwide commented 9 years ago

There are some shortcodes which accept values through multiple parameter names. I have one ( [bw_link] ) where I accept parameter names of: href, url, src and link.

If the shortcode in the content contains any one of these parameter names, then it would be nice if only one entry field is displayed, for the given parameter name.

cliffordp commented 9 years ago

Wouldn't this be easily handled in the actual shortcode's rendering code?

Just include 1 of the potential attributes in the Shortcake UI (e.g. url) and then, in the code, set a priority of which attributes override the others.

For example, after merging attributes with defaults via shortcode_atts():

$url = $atts['url']; // highest priority
if( empty( $url ) ) { $url = $atts['href']; } // 2nd priority
if( empty( $url ) ) { $url = $atts['link']; } // 3rd priority
if( empty( $url ) ) { $url = $atts['src']; } // lowest priority
if( empty( $url ) ) { return ''; } // no link so no shortcode output

(Untested code and could add some esc_url() in there, but you get the idea.)

If you're looking at the scenario where a user manually enters shortcode attributes like href when the Shortcake UI attribute is url, it will still render just fine. It's just that the Shortcake UI won't have href's value in the url field. If you just gotta have that happen, maybe set the value (Shortcake UI's field default value) to href's value prior (not sure if that'd be possible).

If this solves your request, I'd say :-1: to the alias attributes idea. If I misunderstood or am missing a use case, please let me know. I'm usually one for more features and could be convinced. ;-) I hope this feedback helps.

bobbingwide commented 9 years ago

Hi Clifford, as I noted, it's a low priority. In my code I don't use shortcode_atts(). I prefer to retrieve each value individually. I use a common function that will look for the names given also allowing for numeric (ie. positional ) parameters. In my help routine I show the possible parameter names, comma separated. My shortcode expansion works. It's the shortcode UI that can't really handle the logic when putting the parameter values into the form. I don't want the user thinking they can enter different values for each field.