Pros and cons of actions over shortcodes

Question

i need to output some posts on some pages and i created shortcode:

function show_foo() {
    // output stuff    
}
add_shortcode( 'foo', 'show_foo' );

and I use it like this in templates:

echo do_shortcode('[foo]');

But now I’m thinking if I shouldn’t use action in favor of shortcode, like this:

function show_foo() {
    // output stuff    
}
add_action( 'foo', 'show_foo' );

and in templates use it like this:

do_action('foo');

What are the cons and pros of these two methods? Do I understand it right that shortcodes should be used only when you need to add attributes to your output like this?

echo do_shortcode([foo param_1="bar" param_2="foobar"]);

And in cases that you need to let the user to put the output of the function into the wysiwyg editor because they don’t have access to the templates?

0
, Cafourek 2 years 2019-11-22T04:16:51-05:00 0 Answers 81 views 0

Leave an answer

Browse
Browse