Set link text when showing an URL custom field

If you are showing a custom field which has an URL string as value, for example:

http://google.com

Content Views Pro will convert that URL string to a HTML link (anchor tag):
http://google.com

How to use a custom text for the link?
There are 2 possible ways:

1. Manually replace the URL string in the value of custom field by an anchor tag:

<a target="_blank" href="http://google.com">YOUR TEXT HERE</a>

2. Dynamically replace the URL string by an anchor tag using PHP code.
Please add this code to file functions.php in the theme’s folder (or install this plugin Code Snippets then add this code to the “Code” textarea):

// Content Views Pro - custom text for URL custom fields
add_filter( 'pt_cv_ctf_url_text', 'cvp_theme_ctf_url_text', 100, 2 );
function cvp_theme_ctf_url_text( $args, $key ) {
	if ( $key === 'CUSTOM_FIELD_1' ) {
		$args = 'Text1';
	}
	if ( $key === 'CUSTOM_FIELD_2' ) {
		$args = 'Text2';
	}
	if ( $key === 'CUSTOM_FIELD_3' ) {
		$args = 'Text3';
	}

	return $args;
}

(replace CUSTOM_FIELD_1, CUSTOM_FIELD_2, CUSTOM_FIELD_3 with your custom field keys. If you have only one custom field, just replace CUSTOM_FIELD_1 with your custom field key.)

Best regards,

Scroll to Top