A Cozmoslabs Product
Documentation / Custom Fields Creator / Upload Custom Field

Upload Custom Field

Overview

This Default Field Type is available in WordPress Creation Kit free version. The Upload Field creates a file upload form. It can be used to upload images, videos, PDFs etc. and uses the default WordPress Media Uploader.

Creating an Upload Field

To add an Upload Field to a previously created Custom Meta Box, under the Meta Box Fields tab simply insert a Field Title and under Field Type make sure you select Upload.

WordPress Creation Kit - Custom Fields Creator - Meta Box Fields - Upload Field

The Upload Field contains options to customize it:

  1. Description – Allows you to specify a description for the Upload Field
  2. Required – Select whether the field is required or not
  3. Default Value – Set a default value for the Upload Field
  4. Attach upload to post – Whether or not the upload should be attached to the post

Page, Post or Custom Post Type Edit Screen

This is how the Upload Field we created above looks like in the Edit Screen:

WordPress Creation Kit - Custom Fields Creator - Meta Box Fields - Upload Field (Editor)

Template usage

The following examples are for a Custom Meta Box with the “Group Name” argument “my_meta_name“. Make sure to replace this with the “Group Name” you have set up. The Custom Meta Box below is setup to be attached to a post.

WordPress Creation Kit - Custom Fields Creator - Meta Box Arguments - Template

Using the WCK Custom Fields API

The Upload Field returns the source string, the attachment post object or, for a image upload, an array with the image details.

For a Single Meta Box

To output a value inside the loop we use the function the_cfc_field() which echoes the src of the upload

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

To assign the value to a variable we use the function get_cfc_field() which for the Upload Field return the attachment post object or, for a image upload, an array with the image details:

Notice: We’re returning an array for image uploads and an attachment object for other file types (like PDF, CSV, etc.)

 

1
<!--?php $attachment_object = get_cfc_field('my_meta_name', 'avatar'); /* For a image the attachment array looks like this: array( 'id' =&gt; 151, 'alt' =&gt; 'Avatar Image', 'title' =&gt; 'Tim', 'caption' =&gt; 'Our number on member', 'description' =&gt; 'a avatar image', 'url' =&gt; "http://localhost/wordpress/wp-content/uploads/2013/10/Tim.jpg", 'width' =&gt; "100", 'height' =&gt; "120", 'sizes' =&gt; Array ( [thumbnail] =&gt; "http://localhost/wordpress/wp-content/uploads/2013/10/Tim-150x150.jpg", [medium] =&gt; "http://localhost/wordpress/wp-content/uploads/2013/10/Tim-300x119.jpg", [large] =&gt; "http://localhost/wordpress/wp-content/uploads/2013/10/Tim.jpg" ) ); */ /* For a normal upload (not an image) the attachment object looks like this: object(WP_Post)[279] public 'ID' =&gt; int 55 public 'post_author' =&gt; string '1' (length=1) public 'post_date' =&gt; string '2014-07-28 09:00:54' (length=19) public 'post_date_gmt' =&gt; string '2014-07-28 09:00:54' (length=19) public 'post_content' =&gt; string '' (length=0) public 'post_title' =&gt; string 'over_1400_downloads_per_week' (length=28) public 'post_excerpt' =&gt; string '' (length=0) public 'post_status' =&gt; string 'inherit' (length=7) public 'comment_status' =&gt; string 'open' (length=4) public 'ping_status' =&gt; string 'open' (length=4) public 'post_password' =&gt; string '' (length=0) public 'post_name' =&gt; string 'over_1400_downloads_per_week-2' (length=30) public 'to_ping' =&gt; string '' (length=0) public 'pinged' =&gt; string '' (length=0) public 'post_modified' =&gt; string '2014-07-28 09:00:54' (length=19) public 'post_modified_gmt' =&gt; string '2014-07-28 09:00:54' (length=19) public 'post_content_filtered' =&gt; string '' (length=0) public 'post_parent' =&gt; int 54 public 'guid' =&gt; string 'http://localhost/wckpro/wp-content/uploads/2014/07/over_1400_downloads_per_week1.ods' (length=84) public 'menu_order' =&gt; int 0 public 'post_type' =&gt; string 'attachment' (length=10) public 'post_mime_type' =&gt; string 'application/vnd.oasis.opendocument.spreadsheet' (length=46) public 'comment_count' =&gt; string '0' (length=1) public 'filter' =&gt; string 'raw' (length=3) */ ?-->

For a Repeater Meta Box

To output all the “Avatar” entries from the repeater field we use the functions get_cfc_meta() and the_cfc_field():

1
<!--?php foreach( get_cfc_meta( 'my_meta_name' ) as $key =&gt; $value ){ ?--><img src="&quot;&lt;?php" />"/&gt;<!--?php } ?-->

To output a specific “Avatar” entry from the repeater field, for example the second entry, we use the function the_cfc_field():

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

The index starts at 0 so that’s why we pass “1” to the function. For the first entry it would be “0”, the second is “1”, the third is “2” and so on…

Using the WordPress default functions

The Upload Field returns the attachment id.

For a Single Meta Box

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;

For a Repeater Meta Box

To output all the “Avatar” entries in the repeater field:

1
2
3
4
<!--?php $my_meta = get_post_meta( $post-&gt;ID, 'my_meta_name', true ); if( !empty( $my_meta ) ){ foreach( $my_meta as $entry ){ $src = wp_get_attachment_image_src( $entry['avatar'], 'full' ); echo '&lt;img src="'.$src[0].'" width="'. $src[1].'" height="'.$src[2].'"?-->';
    }
}
?&gt;

To output a specific “Avatar” entry from the repeater field, for example the second entry:

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

The index starts at 0 so that’s why we pass “1” to the function. For the first entry it would be “0”, the second is “1”, the third is “2” and so on…

The Ultimate Membership Bundle

Combine the power of Profile Builder with Paid Member Subscriptions to set up user registration, memberships, and recurring revenue.

Get 25% off with the bundle

Help & Support

We’re here to help you every step of the way.

Open a Support Ticket