Skip Navigation

[Resolved] Custom URL output field not displaying

This support ticket is created 2 years, 8 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.

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 10 replies, has 2 voices.

Last updated by ericH-15 2 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#2337201

I am trying to:
Display a Custom URL field on a custom post

Link to a page where the issue can be seen:
hidden link

I expected to see:
"Lakeland PBS" with URL to related website

Instead, I got:
"Lakeland PBS" with empty href field

I am using this to successfully display the Name + URL on a View of custom post types: hidden link

[types field='source-name'][/types]

However when I use that HTML on the individual custom post type it only displays the 'source-name' and href is blank.
If I remove the a tag wrapper, [types field='source-link' output='raw'][/types] displays the correct URL.

#2337209

Shane
Supporter

Languages: English (English )

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

Hi Eric,

Thank you for getting in touch.

Are you using a content template to display your custom post?

If not then this is why it isn't working. Using shortcodes as attributes in HTML tags are not allowed anymore by wordpress and as such for this to work the code must be added to our content template.

We have added a special workaround in our code to allow the shortcodes to still work as attributes for html tags.

Thanks,
Shane

#2337273

Hi Shane,

Thanks for the explanation. I'm using Divi's Theme Builder to display the custom post, not the Toolset content template.

As a work around, I'm trying to do this: [types field='source-link' title="[types field='source-name'][/types]"][/types]
but on the frontend it is displaying as: Lakeland PBS“][/types]

Do you have any recommendations to get the above method to work, or other work-arounds?

Thanks

#2338989

Shane
Supporter

Languages: English (English )

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

Hi Eric,

Given that you're using divi are you using their WYSIWYG editor to add the original anchor tag with the shortcode ? Or are you using their shortcode block.

If you're using the shortcode block can you try using the WYSIWYG editor ?

Failing this the only other solution I can think of is to create a custom shortcode that will basically create the custom link for you.

Thanks,
Shane

#2339043
url-shortcodes.png

Hey Shane,

I'm not sure I fully understand your question. I'll try to explain the setup -

Divi's theme builder only allows editing page templates "on the front end" - the visual editor.
Within this editor, I am using a Text module where I am manually adding my anchor tag and Toolset shortcode (Not using the "Field and Views" button Toolset adds to the Text module. That button has never worked for me.)

The above issue almost appears to be an escape character issue - both shortcodes [types field='source-link' output='raw'][/types] and [types field='source-name'][/types] display the correct data - but when trying to nest the 'source-name' shortcode into the 'source-link' shortcode, its breaks. Is there an escape character I can be using to get this nested shortcode to work?

Hopefully that makes sense.
Thanks

#2339113

Shane
Supporter

Languages: English (English )

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

Hi Eric,

Thank you for the screenshot, however given the case you will definitely need to use a custom shortcode to generate the url.

Try using this one below.


// Add Shortcode
function my_cust_url( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'id' => '',
		),
		$atts
	);

	$url = get_post_meta($atts['id'],'wpcf-source-link');
	$urlname = get_post_meta($atts['id'],'wpcf-source-name');
	
	return "<a href='".$url."' target='_blank' rel='noopener'>".$urlname."</a>";

}
add_shortcode( 'my_cust_url', 'my_cust_url' );

Add it to the Toolset custom code section at Toolset -> Settings -> Custom Code and ensure that you've activated it. You can use the shortcode like this.

[my_cust_url id="[wpv-post-id]"]

Thanks,
Shane

#2339229

Thanks for the custom function Shane.

I added it to Toolset and activated it. However, the custom shortcode renders only as: "] - which makes me believe the issue is with calling a shortcode within the quotes - whether that is Divi or Toolset im not sure.

When I explicitly add the post id, i.e [my_cust_url id="1141"], it renders as:
Array

Any other ideas or suggestions? It seems this issue may lie with Divi's rendering of its own shortcodes and getting tripped up by the nested shortcode in quotes.

#2339661

Shane
Supporter

Languages: English (English )

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

Hi Eric,

My apologies here is a modification for the shortcode so that it will work now.


// Add Shortcode
function my_cust_url( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'id' => '',
        ),
        $atts
    );
 
    $url = get_post_meta($atts['id'],'wpcf-source-link');
    $urlname = get_post_meta($atts['id'],'wpcf-source-name');
     if(!empty($url) && !empty($urlname)){
    return "<a href='".$url[0]."' target='_blank' rel='noopener'>".$urlname[0]."</a>";
}
 
}
add_shortcode( 'my_cust_url', 'my_cust_url' );

Please change the code to this one and let me know if you get the desired results now.

Thanks,
Shane

#2340073

Thanks for fixing that function. When I explicitly pass in a post id, ie [my_cust_url id="1141"] , the function works.
When I try to pass in the nested shortcode, it still renders just as: "].

#2340479

Shane
Supporter

Languages: English (English )

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

Hi Eric,

It seems we are still having issues with the nested shortcodes, so we perhaps need to just get the post ID in the code itself.

Please change the code to this below.

// Add Shortcode
function my_cust_url( ) {
  
  $id = get_the_ID();

    $url = get_post_meta($id,'wpcf-source-link');
    $urlname = get_post_meta($id,'wpcf-source-name');
     if(!empty($url) && !empty($urlname)){
    return "<a href='".$url[0]."' target='_blank' rel='noopener'>".$urlname[0]."</a>";
}
  
}
add_shortcode( 'my_cust_url', 'my_cust_url' );

Please let me know if this alternative works.
Thanks,
Shane

#2340527

My issue is resolved now. Thank you!