Shortcodes as plugin.

wordpress

#1

Hi, everyone.

I am about to finish my first theme and have a question for which i am yet to find a definitive answer. I put my CPT, custom taxonomies and meta boxes in separate plugins bundled in my theme and I want to do the same with the shortcodes. But apart from the simple ones(buttons, dropcap etc.) my theme has many theme specific shortcodes that require templates from theme directory and some theme functions, jquery plugins, css rules, fonts which are declared/enqueued in functions.php. Which means, those shortcodes won’t be functional when a user switchs themes unless i duplicate everything in my plugin. Is this the way to go? Should i bundle all necessary files to enqueue them when another theme is active and redeclare functions in my plugin as well?

Thanks in advance…


#2

That’s ok. Shortcodes are not expected to be fully functional in such case. The point is that if the user switch to a different theme and keep the plugin active, he will see a structured content on his front-end instead of [shortcode]content[/shortcode]. It will of course lack CSS and JS, but something like this:

  • my cool list item
  • another cool list item
  • last cool list item

is still better than this:

[my_cool_list]
[my_cool_list_item]my cool list item[/my_cool_list_item]
[my_cool_list_item]another cool list item[/my_cool_list_item]
[my_cool_list_item]last cool list item[/my_cool_list_item]
[/my_cool_list]


#4

Hi. Thank you for your answer.

I know the purpose of putting shortcodes in a plugin and it really makes sense. But if i am to be more specific please consider this scenario:
I have a shortcode that lists a certain number of posts from my custom post type and turn them the into a carousel if the option is checked. In order to do this, in the loop, it uses theme functions to get the post data(title, image, rating, likes etc.) in an associative array and then calls the corresponding template(there are three) using the get_template_part() to populate the elements/slides. I use this method and slides in some other templates in my theme, it is not specific to the shortcode.

So my concern is not the looks but the errors this will generate and i can’t figure out how to solve it without making huge changes starting with discarding get_template_part() in shortcodes. Any ideas?


#5

Why not wrap calls to your theme functions with this:

if( function_exists( ‘your_theme_function’ ) ) {

your_theme_function();

} else {

// …Provide a fallback

}


#6

Yes, i guess you are right. Thank you for your help.