I have a custom field that collects user's Instagram handles in the form of "@XXXXX." I need them in this form for use with another plugin that auto-posts my content.
However, I also want to display a link to the Instagram handle in a Content Template. To post the link, I need to filter the field's value to remove the @ symbol.
The code in the Content Template should look something like this:
[wpv-conditional if="( $(types field='guest-instagram) ne '' )"]
hidden link field='guest-instagram' output='raw'][/types]" rel="noopener noreferrer">Follow on Instagram
[/wpv-conditional]
Except that this will keep the @ symbol in the URL, breaking it:
href="hidden link field='guest-instagram' output='raw'][/types]"
Any idea how to filter it out?
Thank you!
Hi, there is nothing exactly like that feature built into the Types field API, so I think you would need a custom shortcode to drop the '@' symbol from the field value. At first glance, it seems like it might be easier to collect the handles without the @ symbol, and add that symbol as needed in other operations. If that is not possible, you could use a function like PHP's ltrim to strip the first '@' symbol from a custom field value:
https://www.php.net/manual/en/function.ltrim.php
A custom shortcode might look something like this:
// remove first character '@' from custom field value
// https://toolset.com/forums/topic/remove-symbol-from-values-in-an-custom-field-for-instagram-handles/
add_shortcode( 'tssupp-ltrim-at-symbol', 'tssupp_ltrim_at_symbol_func');
function tssupp_ltrim_at_symbol_func($atts)
{
global $post;
$a = shortcode_atts( array(
'slug' => 'guest-instagram',
'postid' => $post->ID,
), $atts );
$val = get_post_meta( $a['postid'], 'wpcf-'. $a['slug'] , true );
$ltrim = ltrim( $val, '@' );
return $ltrim;
}
Then use it like so to create your custom HTML link tag:
<a target="_blank" href="<em><u>hidden link</u></em> slug='guest-instagram' postid='[wpv-post-id]'][/tssupp-ltrim-at-symbol]" rel="noopener noreferrer">Follow on Instagram</a>