Table of Contents
Custom fields have revolutionized developing sites with WordPress and creating custom fields is now part of the WordPress core.
There are several plugins that simplify this task and make it easy for you to define custom fields and metaboxes with just a couple of clicks.
However, the pain starts when you try to display all this information in your templates. For this you’ll need to write code and you’re not really comfortable doing that.
You start by searching the Codex for functions that are supposed to make this tasks simple. But they’re not that straight forward, and after spending precious time trying to fit all the pieces together, you decide this process is too slow. You project needs to be finished yesterday!
So you bounce to using copy paste tutorials without actually understanding what they do. This sound easier, but it all falls apart when you realize you suddenly have some error handling and bug tracking to do.
Why does a simple thing, like getting information regarding images or users, need so many lines of code? Can’t it be simpler and more intuitive?
What if you could use simple to understand functions that give you what you want directly in your templates without extra work to process that information?
What if you could?
- Save time by writing fewer lines of code (than using standard WordPress functions)
- Avoid error handling and bugs that will eventually appear once more and more code is added
- Get access directly to preprocessed data
Introducing WCK Custom Fields API
WCK Custom Fields API is meant to help you with just that. It consists of 3 easy to use, yet powerful functions that make it really easy to work with custom fields in your templates.
Let’s say you need to simply display a custom field in your theme.
With standard WordPress functions you’ll do something like this:
1 | <!--?php $my_meta = get_post_meta( $post->ID, 'my_meta_name', true ); if( !empty( $my_meta[0]['field-name'] ) ) echo 'Value:'.$my_meta[0]['field-name']; ?--> |
Instead using WCK Custom Fields API, you’ll simply need one line of code:
1 | <!--?php the_cfc_field('my_meta_name', 'field-name'); ?--> |
*The function automatically detects the type of field and outputs values accordingly.
Things remain as simple even if you’re dealing with a repeater field, and need to output a specific entry (e.g. the second entry).
Instead of:
1 | <!--?php $my_meta = get_post_meta($post ->ID, 'my_meta_name', true); if (!empty($my_meta[1]['field_name'])) { echo $my_meta[1]['field_name']; } ?--> |
You’ll have:
1 | <!--?php the_cfc_field('my_meta_name','field_name', false, 1); ?--> |
*The 1 at the end, tells which entry to display (index starts at 0).
For frequently used fields like Image Upload, User Select or Custom Post Type Select(for relationships between posts), things become even more complicated with standard functions.
Let’s say you want to display the avatar image from a metabox:
1 2 3 | <!--?php $my_meta = get_post_meta( $post -> ID, 'my_meta_name', true); if (!empty($my_meta[0]['avatar'])){ $src = wp_get_attachment_image_src( $my_meta[0]['avatar'], 'full' ); echo '<img src="'.$src[0].'" width="'. $src[1].'" height="'.$src[2].'"?-->'; } ?> |
Using the Custom Fields API this will simply be:
1 | <img src="<?php the_cfc_field('my_meta_name', 'avatar'); ?>" /> |
*The API function will automatically process and return only the URL.
You can also forget about error handling, no extra verifications being necessary when using the API functions.
This was just a quick glimpse of what can be achieved using the new WCK Custom Fields API. For mode details and code examples have a look at our documentation page.
Related Articles
Best WordPress Monetization Plugins & Tactics to Grow Your Revenue
You will agree that finding the best WordPress monetization plugins (and tactics) to grow your revenue is challenging. If that describes your situation, you're in the right place, and we have your back as always. Like you, I had big dreams when I started my first WordPress site. Also, like you, I was skeptical about […]
Continue Reading5 Best WordPress User Management Plugins
By default, WordPress lets you add different types of users to your website. However, you might be looking for a more flexible way to manage the people who interact with your content. If this is the case, you can use a WordPress user management plugin to manage authors and contributors. With an efficient user management […]
Continue Reading23+ Most Useful WordPress Plugins: Improve Any Site in 2024
Searching for the most useful WordPress plugins to improve your site in all kinds of helpful ways? Plugins are one of the best parts of WordPress. But with so many different options, it's hard to find the best plugins to help you create a better site. To help you skip the grunt work, we've curated […]
Continue Reading
Great work guys! 🙂
I was waiting this for long time, this will be the only omni comprehensive Custom toolkit, with repeatable groups and easy display system, available in WP market.
Can’t wait to play with the shortcode manager that, I guess, will be the next step to manage APIs.
Thumbs up 🙂
Thanks a lot Mac, and yes, you’re assuming correctly regarding the next step. 🙂
It should be out by the end of this week, so stay tuned. We would love to get your feedback!
Hi!
Well, checking your plugin, what is the difference of what plus featured in comparison with Advaced Custom Fields?
Hi Jesper,
Thanks for looking into WordPress Creation Kit.
I think the main difference between WCK and ACF (which is a great plugin btw) is that WCK, besides creating custom fields and repeater field groups, also lets you build custom post types and custom taxonomies with just a few clicks. You won’t need separate plugins (that sometime don’t play well together) to achieve this.
Also this week we’ll release a new module built into WCK that allows you to display custom fields in your templates without writing any code (see what Mac implied in the upper comment).
How can I insert this value inside WP API?