Changing the output method if a function was called by a short code or not.

[ad_1]

I have a function that is hooked to the end all posts. I’m adding a shortcode so the same function can be called else where. And I need to change the output method based on whether the function was called by a shortcode or the hook.

I have this:

if (did_action(‘hd_related_posts’) > 0) {
// Function was called by the ‘hd_related_posts_shortcode’ shortcode
return ob_get_clean();
} else {
echo” Function was not called by the shortcode”;
echo ob_get_clean();
}

full script posted at https://pastebin.com/nu6XvzgJ

But even when I call the function from the shortcode, this IF statement returns false and the output is echoed when it should be returned. How do I fix this?

[ad_2]
3 Comments
  1. We need to see all the code – you haven’t included enough for us to know why it isn’t working.

  2. Keep in mind, a function is supposed to only do one job. So make another to handle your shortcode. You can execute the hook function within, if it’s necessary.

  3. Can you not just add an parameter like `shortcode = false` to the function and only pass true if it’s invoked via the short code, or something similar ?

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer