Skip Navigation

[Resolved] ACF URL filed for link

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

Problem:

The user wants to use a URL value contained in an ACF URL field within a Toolset View to create a link with the post title.

Solution:

Create a custom shortcode to retrieve the URL value from the ACF field.
Use the custom shortcode within the Toolset View to generate the link.
Here's an example of a custom shortcode implementation:

add_shortcode('get_url_by_field', 'get_url_by_field_fn');
function get_url_by_field_fn( $atts ) {
    $data = shortcode_atts( array(
        'field' => ''
    ), $atts );

    if(!empty($atts['field']) && $atts['field'] === 'durl') {
        $url = get_field('durl');
        if($url) {
            return esc_url($url); // Always escape and validate URLs
        }
    }
}

Usage within the Toolset View:

<p style="font-weight:bold; font-size: 1.3em; margin-bottom: 0;">
    <a href="[get_url_by_field field='durl']">[wpv-post-title]</a>
</p>

This shortcode retrieves the URL value from the ACF field named 'durl' and generates a link with the post title. Make sure to replace 'durl' with the actual name of your ACF URL field.

Relevant Documentation:

Toolset Views: https://toolset.com/documentation/user-guides/views/
ACF Shortcodes: https://www.advancedcustomfields.com/resources/shortcode/
WordPress Shortcodes: https://developer.wordpress.org/plugins/shortcodes/

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.

This topic contains 5 replies, has 2 voices.

Last updated by Christopher Amirian 10 months, 2 weeks ago.

Assisted by: Christopher Amirian.

Author
Posts
#2681459

Tell us what you are trying to do?

I want to use a URL value contained in an ACF URL field in the following Toolset Fields and Text block.

<p>[wpv-post-title]</p>

The ACF URL filed name is 'durl'

Is there any documentation that you are following?

No

Is there a similar example that we can see?

No

What is the link to your site?

hidden link

#2681659

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

That is something you need to ask ACF support.

The text and field function is just a normal editor that contains buttons for Toolset specific content, but you can add whatever you want when it comes to other data.

ACF does not have the view component like Toolset, so most probably you will need to crate a custom shoertcode that retrieves the content of the ACF field in PHP and prints it.

Then you can use that shortcode instead of ???? in the code example you mentioned.

It is something related to ACF.

Thanks.

#2681696
Screenshot 2024-02-04 at 9.03.44 AM.png

My apologies, Christopher .. I didn't explain myself well.

I am using a Toolset View.

I need [wpv-post-title] to link to URLs values in an ACF field called 'durl'.

Are you saying this can't be done?

I can easily pull the ACF 'durl' data visibly (red arrow in image) via a Toolset Single Field.

Can I not use these same URL values to link the Title (green arrow in image)?

hidden link

#2681865

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

That is correct, for the Text and Field you will need to create a shortcode as it does not have a meta feature like the single field.

You can follow the same logic mentioned in the code below:

https://toolset.com/forums/topic/how-do-display-acf-image-field-in-a-toolset-view/#post-1795045

It is for the ACF image field, but the same thing can be used for the URL field.

Thanks.

#2681902

Thanks - what do you think of this solution provided by ChatGPT?

Seems to work 🙂

-----------

add_shortcode('get_url_by_field', 'get_url_by_field_fn');
function get_url_by_field_fn( $atts ) {
$data = shortcode_atts( array(
'field' => ''
), $atts );

if(!empty($atts['field']) && $atts['field'] === 'durl') {
$url = get_field('durl');
if($url) {
return esc_url($url); // Always escape and validate URLs
}
}
}

<p style="font-weight:bold; font-size: 1.3em; margin-bottom: 0;">
[wpv-post-title]
</p>

#2682078

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

You can test that for sure, but it is the best to ask ACF support on the best way to get a URL field from a shortcode context.

Thanks.