Outputting Function When Creating a WordPress Shortcode

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:

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

DON’T DO THAT!

What you need to do is run the function through an output buffer with ob_start(); and ob_get_clean();, like so:

https://gist.github.com/bryceadams/7d4a8343b312920c02af.js

And it works! For the PHP nerds, you may be interested why we’re using ob_get_clean(); instead of ob_get_contents(); and ob_clean(); – then check out this discussion.

How to Include a Retina Icon for Your Custom Post Type’s Menu Item

Here’s a quick little tip for you. Since WordPress 3.8, Dashicons has been included in the core and the menu item icons you see on the left in the dashboard are using the icon font instead of images. Why is this so cool? Well.. they’re retina ready, look awesome and scale infinitely!

So you’ve just created a new custom post type but want an awesome icon instead of the boring Posts pin icon? Well, it’s super easy.

When you’re creating the custom post type, make sure you have the menu_icon argument there and then just let it correspond to the Dashicon you want to use, like so:

https://gist.github.com/bryceadams/92ea4273a3ae09ab7392.js

Find a full list of the icons available and their CSS codes here.

For more on custom post types, visit the WordPress Codex.

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!

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