Hi there,
I have a custom field that stores a URL. The content in the field might be "hidden link."
When displayed on a webpage, it displays the same content for both the link target text and the href attribute value:
target text: hidden link
href attribute value: hidden link
What I would like to do is manipulate the link target text and strip out "hidden link" so only the domain appears:
target text: hidden link
href attribute value: hidden link
Is there a filter I can hook into to make this happen? Or should I write a custom shortcode? What's the best way to accomplish this?
Thank you!
Saul
Hi Saul,
Thank you for contacting us and I'd be happy to assist.
There is no built-in filter available for this, so you can use a custom shortcode for creating a link in this format.
For example:
add_shortcode('get-filtered-url', 'get_filtered_url_func');
function get_filtered_url_func($atts) {
$a = shortcode_atts( array(
'field' => '',
), $atts );
$url_field_value = types_render_field($a['field'], array("output" => "raw"));
if(!empty($url_field_value)) {
$url_field_value_arr = explode('://', $url_field_value);
$link_text_URL = rtrim($url_field_value_arr[1],"/");
$content = '<a href="'.$url_field_value.'">'.$link_text_URL.'</a>';
return $content;
}
}
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
After that, you'll be able to use this shortcode in your content like this:
[get-filtered-url field="book-url"]
You'll replace the "book-url" field slug with your website's URL custom field.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
My issue is resolved now. Thank you!