Skip Navigation

[Resolved] Custom shortcode get Toolset custom field

This support ticket is created 3 years, 1 month 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 5 replies, has 2 voices.

Last updated by matthewL-7 3 years, 1 month ago.

Assisted by: Shane.

Author
Posts
#2240551

Hi

So I have quite a complicated Toolset conditional output setup and copied it in multiple templates. I realised using a code snippet for a shortcode probably makes a lot more sense and then using IF statements in PHP.

I am fine with all this, however the conditional output is based on a posts custom field.

In the PHP code snippet, how can I get this Toolset custom fields value? Do I need to pass it in a Shortcode parameter? Or can I simply pull it using the Post ID?

E.g.

`if ($wpcf-affiliate-network == "C7" || $wpcf-affiliate-network == "kantar" ) {
echo "Result here, the Post ID is".get_the_ID();
}`

$wpcf-affiliate-network being a Toolset custom field belonging to whatever Post ID is being loaded on the page where this shortcut is being used. However I don't know how to get wpcf-affiliate-network's value.

Thanks.

#2240603

So I found to use get_post_meta.

I tried to use this using the following:

 $network = get_post_meta($post_id, 'wpcf-affiliate-network');
  
  echo $network;

But for some reason the output is the word "Array"? I am guessing since its a select box its not just storing the name but more than that? How can I just get the name selected?

#2240651

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Matthew,

Thank you for getting in touch.
The get_post_meta() function can be used when retrieving values from simple custom fields like "single line" and "multi line" however when using more complex fields such as checkboxes then you will need to use types_render_field( )

Here is an example usage below.


types_render_field( 'field-slug', array( 'arg1' => 'val1', 'arg2' => 'val2' )

In your case you can use it as

types_render_field( 'affiliate-network', array( 'item' => $post_id )

When using the types_render_field() function it is not necessary to use the 'wpcf-' prefix.

Please let me know if this function was able to resolve your issue.

Thanks,
Shane

#2241399

That worked perfectly thanks Shane! I have my shortcode working now.

Is there a way to hook this on to a field automatically? So my shortcode snippet always gets added whenever field "affiliate-network" is used.

Basically I am trying to add some tracking info on to the end of all links. With the old views this was simple because I could add my shortcode on to the end of where I am inserting the URL in HTML. However for sites I am using the block editor not sure how I could do this?

I guess I would recode the buttons in text and fields but would be better to keep using the block editor and also having it do it automatically makes more sense instead of always having to remember to use the shortcode.

#2241669

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Matthew,

You can still do this in the block editor but you would need to use the classic block and construct your links manually.

Meaning you will have your url like this.

<a href='myurl?param=[shortcode]'>My Link</a>

In classic views this is how you would do it but you can still achieve the same by adding the classic editor block to your view and then construct your link.

Please let me know if this helps.
Thanks,
Shane

#2241749

My issue is resolved now. Thank you!