Skip Navigation

[Resolved] Displaying a link in php using custom field data

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

Problem:
Displaying a link in php using custom field data

Solution:
You can use Types PHP API function types_render_field() to display the custom field value.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/displaying-a-link-in-php-using-custom-field-data/#post-924636

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created 5 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.

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

Supporter timezone: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 10 replies, has 2 voices.

Last updated by andrewB-7 5 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#923700

Hi there,

I have created a custom field called 'afepi-website' which is linked to a custom post type called 'member'.

I am building the theme in standard php files.

In the php file I want to output the link onto all single pages. I can do that, but I want the url that is entered into the field to provide the content of the href attribute on the anchor tag, while the clickable link on the website front end should simply read 'click here for website'.

But all I can get it to do is to output the raw url on the front end.

The code I am using is as follows:

if(types_render_field("afepi-website", array('class'=>''))){
			 
				return (types_render_field ("afepi-website", array('class'=>'')));  

I hope you can help with the correct code.

Many thanks,
Andrew.

#923901

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - do you mean that you want to display text "'click here for website' as anchor tag and when user click on that user should be redirected on the URL which is stored inside as custom field value. If Yes:

$url = types_render_field("afepi-website", array('ourtpt'=>'raw'));
if(!empty($url)){
    echo '<a href='".$url."'> Click here for website </a>';
}
#923902

Oh wow thank you, let me give this a go and get back to you. And if I wanted to have an icon instead of "click here for website" from the built in WordPress icons, could I do that too in Toolset?

#923908

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad to know that it works for you.

I think yes, you should just try to replace the text with icon within anchor tag.

Please feel free to open a new ticket with your each new question which you may have. This will help other users searching on the forum.

#923933

That code is breaking the site (I get a blank white page when I load it).

Could you please check that it is correct?

Many thanks for your time,
Andrew.

#924023

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

upps. could you please share problem URL and access details and tell me to which file you have added the code.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#924631

Hi there,

Okay I am pasting in here the full code so that you can take a look. It is definitely something in this code that is breaking the site, as when I remove it, the site works fine.

I need to put this in a shortcode in order for it to work in a theme. I think there must be a small piece of syntax that is breaking it, and hopefully you can see this here:


// Shortcode for member profile field: afepi-website

	add_shortcode( 'afepi_website', 'afepi_website' );
	
		function afepi_website() {
		
			// The content of the shortcode to be output
			
			$url = types_render_field("afepi-website", array('output'=>'raw'));

				if(!empty($url)) {
    
					return '<a href='".$url."'> Click here for website </a>';
				}		 
		
		}

Thanks again for your time, and very best wishes,
Andrew.

#924636

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - indeed, there was a issue with quotes with the line of code you return.

Could you please try following code:

add_shortcode( 'afepi_website', 'afepi_website' ); 
 function afepi_website() {
         
            // The content of the shortcode to be output
             
            $url = types_render_field("afepi-website", array('output'=>'raw'));
 
                if(!empty($url)) {
     
                    return '<a href="'.$url.'"> Click here for website </a>';
                }        
         
        }

It should work. Could you please confirm.

#924638

Awesome, thank you!

It works beautifully. I'm trying to get everything to work in shortcodes which I can paste into Elementor, as right now there is no direct integration. And this is a treat, thank you!

Do you have any idea on when that integration will come? Tons of us are really looking forward to that moment.

Thanks for your help,
Andrew.

#924641

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - glad to know that the solution I shared works for you.

The work on Elementor integration is going on I do not have any ETA on it but maybe within couple of months or sooner than that.

Feel free to open a new ticket with your each new question 🙂

#924643

Oh wow! That's great news, thanks Minesh.

I understand you can give no definite answer, but that's great to have some kind of guide.

Have a great day!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.