How to Fix the WordPress Error ‘Fatal error: Call to undefined function wp_get_current_user()’

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 plugins_loaded.

If for example you were trying to do something like:

https://gist.github.com/bryceadams/0598a529118eab052180.js

…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:

https://gist.github.com/bryceadams/e307a8fb3c9b17bb966a.js

Any questions, let me know!

PHP: Convert 24 Hour Time to 12 Hour Time (and Vice Versa)

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!

Change ‘Add To Cart’ Text in WooCommerce

UPDATE: New updated post here!

Whenever there’s something I struggle to find a clear answer for (and eventually figure out myself), I like to post it for those who need help later. This is one of those times.

If you ever wanted to change the ‘Add To Cart’ text on your WooCommerce site, just add in the following code to your functions.php file (or similar):

Don’t forget to change the text to whatever you’d like your button to say!

Exclude Featured Image From Gallery Shortcode

I came across this great snippet after some extensive searching and just wanted to share it to avoid others having to search long and hard just to find it. Authored by TheDeadMedic over on Stack Overflow, inserting the following into your functions.php file will exclude the_post_thumbnail or featured image of a post from its gallery:

Light – A Free Responsive Lightbox Plugin for WordPress

I decided to put together a small little WordPress plugin today, using the incredible fancyBox jquery lightbox.

It’s called Light, and it’s just that: a Lightbox and a light-weight plugin. No settings or detailed instructions or anything like that. Just install it and it works.

Want to see a demo? Cool. Here you go.

It’s actually pretty flexible, so I wrote a whole lot of documentation for it over on Captain Theme. You can view the documentation for it there.

Regarding the License, I personally don’t care what you do with the plugin but fancyBox is under a Creative Commons License that basically allows you to use it in personal/non-commercial projects for free but you have to pay if it’s for a commercial site (and you should – it’s well worth the money and the developer deserves it!). Read more about the fancyBox License.

It’s also on GitHub! How exciting. Check out the GitHub repository.

Have a question or need help? Leave it below and I’ll try to answer it.


Download 1.0.0

Documentation / Demo

Change Title of WordPress Meta Boxes

I came across this awesome snippet the other day that I thought I’d share and expand on it a little bit.

When you want to rename the title of a meta box, custom or core, the best way is to remove that meta box and then re-add it with a new title, using something like this:

You’ll need to change the fields above though for:

  • METABOX_ID – eg. commentsdiv. See below for full list.
  • META BOX TITLE – Whatever you’d like to rename the meta box.
  • METABOX_CALLBACK – eg. post_thumbnail_meta_box – See below for full list.
  • POST_TYPE – eg. post, page, etc.

Continue reading

Customising the Easy Digital Downloads Checkout Page

In Easy Digital Downloads, by default the Checkout page uses the page.php page template to generate the page. However, there are a few things you can do to customize the page.

Firstly, you can customize the layout of the checkout shopping cart by creating a template for it. In your theme create a folder called edd_templates and in there create a file called checkout_cart.php. Copy into that file the code from here.

It might look a little daunting at first but play around with this file as it enables you to completely customize the shopping cart’s layout and design just like you would any other theme template file.

Continue reading

PHP Random String From Array In WordPress

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*.

Continue reading