Skip Navigation

[Resolved] Remove @ Symbol From Values in an Custom Field for Instagram Handles

This support ticket is created 4 years, 1 month 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 4 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#2041439

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!

    #2041591

    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>