• I want to create a plugin that handles any unrecognized shortcodes. If there is a custom field by the same name as that shortcode, it would translate the shortcode into the value of that field. Otherwise, it would look for a custom post type called “templates” that has a post_name by the same name as the shortcode, and if found, it would output the contents of that post. The basic idea is reusable blocks of text or code that is embedded by name.

    I’ve looked at the code, and it looks like a core change is needed. I’ll have to change the regex to add ‘[a-zA-Z\-\_\.]+’ to the list of alternations, and then if there isn’t a handler for the matched shortcode, call the ‘default’ handler.

    Any suggestions about the “right” way of making that change, and of how to get the change pushed back into the main code base?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    The right way would be via a plugin, but you’ve got an idea that would add a lot of overhead to WordPress (especially since things like [sic] are valid usage in English, and would rarely if ever want to be shortcoded… )

    You’re basically adding in a feature to check EVERY possible [whatever] and [whatever]ksdfjdskhf[/whatever] call for a shortcode and I’m not sure that would work in core for everyone.

    I would email this one to wp-hackers ( https://lists.automattic.com/mailman/listinfo/wp-hackers ) and ask there.

    Thread Starter jarrowwx

    (@jarrowwx)

    But yes, it would add overhead. I am not sure how much, but I acknowledge that it adds overhead. Every shortcode that gets added adds overhead, too.

    What I want to do is, if there is a meta field defined for the post/page being processed (even inside a sub-query), say a meta field called “foo” then I would like to be able to turn [foo] into the value of that field. I can already do [meta key=”foo”] but I like the other much better from the standpoint of readability. If I find that the current post doesn’t contain a meta field by that name, I can just output ‘[foo]’ and call it good.

    I suppose, if there is a way to query for all meta key names that exist in the database, I can just create a handler for each of those, and avoid the unnecessary overhead for all the other cases. It still adds overhead, but the amount of overhead will be small enough to be negligible for a relatively low-traffic site, and will add enough usability to make it better for those creating content on the site.

    Another option for me is to create temporary shortcodes. I have created a [query] shortcode. Inside that, as part of processing each post, I could query for the list of meta keys associated with the post, create shortcode handlers for those that do not already exist, process the record normally, then delete any that are no longer relevant. Then, the overhead of the feature that I want is limited to the times when I actually want it.

    Sounds good. Now all I need is a mechanism for detecting if a shortcode is already registered without having to peek into the internals of the shortcode module. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Ability to create default/unrecognized shortcode handler’ is closed to new replies.