Pros and cons of actions over shortcodes

Neither are correct. Just use show_foo(): <?php echo show_foo(); ?> The only reason to ever use shortcodes is to insert dynamic content into a page or post from within the editor (Although, these days the ‘proper’ way to do that would be with a block, although that’s more difficult). Shortcodes should not be used for … Read more

Shortcode with custom content attribute?

The second parameter of your shortcode function is the content. So rewrite the function: function shortcode_call_to_action( $attributes = NULL, $content=”” ) { $stylesheetdir = get_bloginfo(‘stylesheet_directory’); if ( ” === $content ) { // use a default text if there is no content $content=”default text”; } return “<div id=’call_to_action’> <img src=”https://wordpress.stackexchange.com/questions/58438/$stylesheetdir/images/call_to_action.jpg” alt=”” /> <p>$content</p>”; } If … Read more

Pass $this to function nested in another public function of the same class

There are multiple ways to accomplish this. I’m showing you a way that will not fundamentally change how you are doing it: First in My_Plugin class: class My_Plugin { // … private function define_public_hooks() { $plugin_public = new My_Plugin_Public( $this->get_plugin_name(), $this->get_version() ); $this->loader->add_action( ‘init’, $plugin_public, ‘init’ ); // … } // … } Then in … Read more

Passing a shortcode attribute to a sub-function

There are different ways to get the result. You can use a class, store the div content in a class or instance variable and output it when needed. As alternative you can use a function with a static variable, to hold the content. I’ll use this second alternative, converting it in a class is an … Read more

Use content custom filter for all shortcodes

so I would like to execute a filter or something that works on every shortcode Looks like you’re after do_shortcode_tag. It “Filters the output created by a shortcode callback.”. Aurovrata Venet gives a demo usage similar to: add_filter( ‘do_shortcode_tag’,function ($output, $tag, $attr){ //make sure it is the right shortcode if(‘aShortcode’ != $tag){ return $output; } … Read more

Using Echo in ShortCode – Stuck

You can also use output buffering to catch all output you generate. Use it like following: function fl_aries_co_today_shortcode() { global $wpdb; $results = $wpdb->get_results(“SELECT horoscope FROM wpaa_scope_co WHERE date = CURDATE() AND sign = ‘aries'”); ob_start(); foreach($results as $r) { echo “”.$r->horoscope.””; } $content = ob_get_contents(); ob_end_clean(); return $content; } add_shortcode( ‘fl_aries_co_today’,’fl_aries_co_today_shortcode’ );

Can’t seem to get [shortcode]s to work

As Milo indicated: functions.php is a file within your current theme, not the functions.php file in wp-includes. There’s nothing wrong with your code as-is as long as it’s in a file that’s actually being loaded at the correct time. Addressing your other question from the comments, yes, this code can be wrapped up into a … Read more