Support » Plugin: Custom Content Shortcode » How do I remove spaces from phone number so tel: will work?

  • Hi – I think I’m missing something simple here.

    I have an ACF subfield within a repeater field called ‘phonenumber’. In the back end of the site the client populates this with the phone number including country code and spaces in order to make it easy to read.

    On the front end I am trying to create a tel: link so that people can easily click on the phone number to call it directly. I’m using this:

    [repeater offices_0_phone]
    [field phonename]: <a href="tel:field phonenumber">[field phonenumber]</a>[/repeater]

    But the output is that it replaces all the spaces with %20. So if for example the phone number is +44 1234 567890 then the link becomes +44%201234%20567890.

    So how can I remove the spaces from the phone number?

    Thanks for your time.

    • This topic was modified 1 month, 1 week ago by jamesburden.
    • This topic was modified 1 month, 1 week ago by jamesburden.
    • This topic was modified 1 month, 1 week ago by jamesburden.
    • This topic was modified 1 month, 1 week ago by jamesburden.
    • This topic was modified 1 month, 1 week ago by jamesburden.
    • This topic was modified 1 month, 1 week ago by jamesburden.
Viewing 14 replies - 1 through 14 (of 14 total)
  • Thread Starter jamesburden

    (@jamesburden)

    For some reason the formatting of my shortcodes keeps resetting in the post above. The phonenumber field after ‘tel:’ is surrounded by square brackets of course.

    There is no actual way t do that nicely with this plugin. I ended up writing a shortcode myself.

    There I use preg_replace("/[^0-9]+/", "", $phone); to format a number I pass to the shortcode.

    Could you “split” (and rejoin) the phone number using the space character as a separate?

    Thread Starter jamesburden

    (@jamesburden)

    Thanks @polarracing – really appreciate your help.

    I’m just starting to learn the ropes with php and how to create my own shortcodes.

    So am I right in thinking that I need to create a snippet that creates a shortcode like this:

    function trimnumber_function() {
    	preg_replace("/[^0-9]+/", "", $phone);
    }
    add_shortcode( 'trimnumber', 'trimnumber_function' );

    And then on the front end I reference that shortcode like this:

    [repeater offices_0_phone]
    [field phonename]: <a href="tel:[trimnumber]">[field phonenumber]</a>
    [/repeater]

    But how do I tell the shortcode that $phone is the value of my custom field which has an ACF name of phonenumber and an ACF key of field_6061b942fa7a2 ? Do I need to replace $phone in the shortcode function with the ACF key somehow?

    Apologies if this is really basic – just trying to learn how to do this and build on it in the future!

    • This reply was modified 1 month, 1 week ago by jamesburden.

    This works for me:

    [set telephone]+44 123 456 7890[/set]
    Tel: [get telephone]
    [set telephone2][format split=' ' part=1][get telephone][/format][format split=' ' part=2][get telephone][/format][format split=' ' part=3][get telephone][/format][format split=' ' part=4][get telephone][/format][/set]
    Tel2: [get telephone2]
    • This reply was modified 1 month, 1 week ago by iantresman.
    Thread Starter jamesburden

    (@jamesburden)

    Thanks @iantresman – that looks interesting if it is just UK numbers and if they are always entered in the same number groupings. But I’ve got hundreds of phone numbers across 130 different country codes and formats. Even within countries, the numbers are not uniformly entered in the same grouping. But I hadn’t used the set shortcode before – so that’s a useful tool to have!

    Am hoping I can get my head round this shortcode approach. I think that will be really useful to learn.

    Thread Starter jamesburden

    (@jamesburden)

    @polarracing – I’ve just been trying to understand a bit more about creating the shortcode and how to pass the ACF value to it. So should the shortcode function read as follows?:

    function trimnumber_function() {
    $phone = get_field_object('field_6061b942fa7a2');
    	preg_replace("/[^0-9]+/", "", $phone);
    }
    add_shortcode( 'trimnumber', 'trimnumber_function' );

    And then I can reference [trimnumber] as shortcode that has removed the spaces from the original phonenumber field?

    Thread Starter jamesburden

    (@jamesburden)

    Hmm – I’ve tried the code I posted above and it doesn’t work. I’ve obviously got something wrong with the code to create the shortcode.

    If anyone has any guidance on how I can resolve this I’d be very grateful!

    Does this help:
    Strip whitespace from custom field“.

    There are more pointers if you search Google for ACF Strip spaces.

    You need to pass the value with trimnumber.
    [trimnumber phone={PASSED VALUE}]

    Here is my shortcode – You can use it out of the box with:

    [mri-phone phone='{PASSED_VALUE}’]

    add_shortcode( 'mri-phone', 'mri_phone' );
          function mri_init4(){
    		function mri_phone( $atts ) {
    			$a = shortcode_atts( array(
    				'phone' => '',
    			), $atts );
    			$phone = esc_attr($a['phone']);
    			if ($phone) {
    			$phone = preg_replace("/[^0-9]+/", "", $phone);
    			return $phone;	
    			}
    		}
    	}
    add_action('init', 'mri_init4');
    Thread Starter jamesburden

    (@jamesburden)

    @iantresman – thanks! Yes, I’ve already been through those pages with no success in this particular situation.

    Thread Starter jamesburden

    (@jamesburden)

    Hi @polarracing – I’m so sorry – I’m sure I’m missing something obvious. I’m still trying to wrap my head round how CCS and shortcodes work.

    I’ve added your function exactly as is. And I’ve added the [mri-phone…] shortcode. But what do I put in place of ‘{PASSED_VALUE}’?

    I understand I need the value to come from the ACF ‘phonenumber’ field. What is the passed_value? Is it the ACF field name or ID? Or something different? I’ve tried a number of permutations – none of which seem to work.

    Thanks so much for your patience!

    [set telephone]+44 123 456 7890[/set]
    [pass vars]
    [mri-phone phone='{TELEPHONE}’]
    [/pass]
    Thread Starter jamesburden

    (@jamesburden)

    Thanks @polarracing – I think I’ve got it now 🙂 Appreciate the guidance very much indeed.

Viewing 14 replies - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.