Blog / plugins / WordPress API for Custom Fields with WCK

WordPress API for Custom Fields with WCK

Adrian Spiac
Last Updated: 26/04/23

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.

cfc_extra_fields

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-&gt;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 -&gt;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 -&gt; ID, 'my_meta_name', true); if (!empty($my_meta[0]['avatar'])){ $src = wp_get_attachment_image_src( $my_meta[0]['avatar'], 'full' ); echo '&lt;img src="'.$src[0].'" width="'. $src[1].'" height="'.$src[2].'"?-->';
}
?&gt;

Using the Custom Fields API this will simply be:

1
 <img src="&lt;?php the_cfc_field('my_meta_name', 'avatar'); ?&gt;" />

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

From the blog

Related Articles

what-is-wordpress-ver-2

Beginner’s Guide to: What Is WordPress?

Author: Cristian Antohe
Last Updated: February 15th, 2017

Ever now and again the question arises with new clients that aren't really tech savvy: "What Is WordPress?" What I'm hoping to achieve with this post is to drop the technical jargon for a minute and explain in down to earth words what is WordPress, how can it help you, what is WordPress.com, what's a […]

Continue Reading
wordpress monetization

Best WordPress Monetization Plugins & Tactics to Grow Your Revenue

Author: Freddy Muriuki
Last Updated: June 6th, 2024

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 Reading
WordPress Forum Plugins

A Guide to WordPress Forum Plugins: The Best Plugins & How To Manage Them

Author: Alex Denning
Last Updated: June 4th, 2020

If your WordPress site is designed to serve and engage with a community, you’ll need to install a WordPress forum plugin. Forum plugins add a platform where users can ask questions, provide answers, and join in discussions. It promotes engagement and gives users a sense of being part of a community. You can use it […]

Continue Reading

5 thoughts on “WordPress API for Custom Fields with WCK

    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 🙂

    Reply

    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!

    Reply

    Hi!
    Well, checking your plugin, what is the difference of what plus featured in comparison with Advaced Custom Fields?

    Reply

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.