So I was recently working on a new plugin that involved shortcodes but ran into a bit of a problem when the contents kept getting output at the top of posts/pages before the rest of the content.
Basically this happens because the shortcode is simply returning the data as soon as it’s generated it, not taking note to output it specifically where the user desires. Normally it’s because the content was echoed and not stored and later returned. You can read more about it in this great post but it’s really as simple as that.
However, I was recently faced with this issue and couldn’t figure out why! I was returning the data but it was still being output at the beginning of the post’s content before everything else. I was returning another function (that echoed the content) within my shortcode’s function but it wasn’t helping.
It looked a little bit like this:
DON’T DO THAT!
What you need to do is run the function through an output buffer with
ob_get_clean();, like so:
And it works! For the PHP nerds, you may be interested why we’re using
ob_get_clean(); instead of
ob_clean(); – then check out this discussion.
You may be seeing the following error on your WordPress site:
Fatal error: Call to undefined function wp_get_current_user()
It’s being caused by the
wp_get_current_user() being called too early. You first have to wait for
If for example you were trying to do something like:
…in a plugin, WordPress may throw back the above error.
To fix it, wrap it in a function and then add it to the
plugins_loaded action, like so:
Any questions, let me know!
I just came across this in a time of need and thought I would share.
If you want to convert a time in 24 hour format, such as 14:00, to 12 hour format, which would be 2PM, you can use the following code.
24 Hour to 12 Hour:
Additionally, if you wanted to convert the 12 hour time format to 24 hour, you can use the next bit of code.
12 Hour to 24 Hour:
PS. Sorry I haven’t posted in a long long time. Been travelling, living, etc. Coding again and working on some new stuff so will try to post more often and reply to questions. Cheers readers!
When I struggle to find a clear answer to something and eventually come up with something that works, I like to repost it here so others can hopefully save themselves some time. This is one of those times.
If you want to display a random string from an array in WordPress, such as a random quote from a collection of your favourites, I’m going to show you how. Note that this isn’t a WordPress-specific solution, but I like WordPress and I like when you do too so I’m going to assume you’re using it*.