Skip Navigation

[Resolved] Display Custom Fields with PHP

This thread is resolved. Here is a description of the problem and solution.

Problem

How can I display my Custom Fields created with Toolset, using PHP code?

Solution

There are 2 possible approaches to display Custom Fields with code.
Using the WordPress API or the Toolset API.

Toolset API example for a single line field:

echo (types_render_field( 'field-slug', array() ));

(Note that in the

array()

you can also pass a Post ID and more arguments as shown here)

This will output the Field "field-slug" on the Single Post where I saved this Field.

WordPress API:

echo (get_post_meta( $post->ID, "field-slug", false ));
This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 9 replies, has 2 voices.

Last updated by rolandP 6 years, 8 months ago.

Assisted by: Beda.

Author
Posts
#621488

I am trying to: Displaying custom fields on the site’s front-end woocommerce product page

Link to a page where the issue can be seen:

I expected to see:

Instead, I got:

Hi,

I made custom fileds for product page woocommerce.
With views plugin I can show my fields is okay.
But I need use php to show this fields.

What I do:
1. Settings this fields: hidden link
2. Use types_render_field hidden link
3. Dont show up on front: hidden link

Where I made mistake?
How will be correct code for php?

many thanks

#621667

1. How to debug this:

- instead of the Code that you use now in the particular PHP file, insert this:

echo "It is printed";

- head back to the front end
- do you see "It is printed"?

2. Case YES:

- try to call another custom field, or use another API:
get_post_meta()
https://developer.wordpress.org/reference/functions/get_post_meta/

3. Case NO (which I suspect):

- means, the file is not called in the product frontend.
- this may be due to the setting in Toolset > WooCommerce > Template to use.

==> There, choose the template you want to use, for single products, or disable Toolset WooCommerce completely, which will then fall back to WooCommerce rules.

Please let me know if that helps.

#621794

Hi,

Yes I see "It is printed".

How display fields in php file?

I use always wp views and I will do this:
[types field='termin-realizacji'][/types]

How to convert to <?php ??

many thanks

#622539

Generally, in WordPress you display custom fields with get_post_meta():
https://developer.wordpress.org/reference/functions/get_post_meta/

When you use get_post_meta() you must pass a "wpcf-" prefix in the Post Meta Key:
get_post_meta($post_id,"wpcf-my-field", false )

You can also use types_render_field().
In this case, you do not need to add the "wpcf-" prefix.

Both functions only return the value, this means you must "echo" it in your code.
https://toolset.com/documentation/customizing-sites-using-php/functions/

In the code sample which you sent me you do not echo this code.

#623231

Hi,

I tried to do what you write to me but it does not work.

I think there is a communication problem between us. Maybe from my side, sorry. Therefore, I will describe my problem in detail again. I am asking for specific help or please give someone else. No offense.

I use theme from themeforest.
I active plugin:
WP Toolset
Woocommerce

I go Toolset > Post Fields > Add new

Screenshots: hidden link
Field Name: My Field
Field slug: my-field

Now go to Woocommerce > Add new product

Screenshots: hidden link

No to go sftp and edit file meta.php
Screenshots: hidden link
I use: <?php get_post_meta($post_id,"wpcf-my-field", false )?>
Result: hidden link, It does not show up

I try use: <?php types_render_field( "wpcf-my-field", array( ) ) ?>
Result: hidden link, It does not show up

Where is problem?

many thanks

#623232

P.S. Yes I add echo: <?php echo get_post_meta($post_id,"wpcf-my-field", false )?>

#623259

Let's say I create a Single Line Field in Toolset > Post Fields, and I want to display this fields with PHP.

Then I use this:

echo (types_render_field( 'field-slug', array() ));

(Note that in the array() you can also pass a Post ID and more arguments as shown here: https://toolset.com/documentation/customizing-sites-using-php/functions/)

This will output the Field "field-slug" on the Single Post where I saved this Field.

I can also do that directly with the WordPress API:

echo (get_post_meta( $post->ID, "field-slug", false ));

This is how you display Custom Fields in WordPress, or Toolset, using PHP.

Can I ask, why you want to display them with PHP?

See, I am not allowed to provide Custom Code.
I can show you how to create custom code, can help to find the right approaches, review, but I cannot create the full custom code.
We created Toolset so people do not need to learn PHP.

That is why I feel bad in both cases:
- I provide you with a full custom code, which you will not understand and hence cannot fully control
- I do not provide it, but then you cannot move on.

This is why I explain how to use it, but if you need assistance with creating and maintaining your PHP custom code, I suggest to consult a Contractor:
https://toolset.com/contractors/

Now, if in the file where you add the "echo "I see this;" you see "I see this" on the front end, then the types_render_field or get_post_meta will also show, as long there are some values saved in that field for the current post.

I can, for example, add an "example custom field" to your Site, and then add a code to the PHP File to display that, so you can use that as an example.

I already have access to your site, but there I see only a Child theme active that has no Files included but the functions.php file.
If you are editing the Parent theme, I suggest that you instead add that file to the Child theme and edit it there, according to the WordPress Codex:
https://codex.wordpress.org/Child_Themes

If you can show me where you see "I see this" after inserting "echo "I see this";", I can add a call to an example Custom Field there, so you can use it as an example.

But I cannot suggest doing that in the parent theme.

#623267

Hi,

I understand that WP Types is for no-php code. I use lot of projects views plugin but in this project I need use in php.

I can, for example, add an "example custom field" to your Site, and then add a code to the PHP File to display that, so you can use that as an example.
This be the best way. Please.

I disable child-theme.
You can add your code to this:
/wp-content/themes/yolo-sofani/woocommerce/single-product/meta.php Line 44

many thanks

#623270

It is not suggested to edit the parent theme.

This is against the WordPress Codex, and after an update, you will lose all those changes.

However, since you asked me to do so I added an example snippet using get_post_meta() and types_render_field() to the PHP template you indicated, along with an "echo "This is the example: "

Note that to make it work on this template I needed to set the global $post, got the Post ID from it, populated a few variables with the Field values I got.
Since it's a single line field the get_post_meta() here uses false as a 3rd argument, otherwise, you will get an array as output

With the types_render_field() this is all done automatically and hence you do not need to specify those details in the code.

You can see the output here.
hidden link

It's added to the theme file you indicated after the comment "//This is Toolset Support Code Example"

It uses a new test field I created here:
hidden link

Please, note that this is Custom Code, and it mainly involves the usage of PHP and WordPress API.

If you plan to build your site with PHP we will not be able to help with this extensively.

We have all those examples as well in our Documentation which I pointed to earlier.

#623711

Thank you, this what I need!!! Good weekend for you!!