0

I am working on a webpage that lists all the access numbers our customers ask for.

What I am trying to do is turn all the text phone numbers into telephone links through a php function rather than manually add the html markup to every number.

I was hoping that I can do it myself but, damn, what a disappointment!

This is the current markup for each phone number:

<div name="Missouri" style="display:none" class="zebra1"> (573) 213 2134</div>

What I am trying to do is turn the above markup like so:

<div name="Missouri" style="display:none" class="zebra1"> <a href="tel:(573) 213 2134">(573) 213 2134</a></div>

Note: This phone number is for the state of Missouri; however there are several phone numbers for each state.

I have only got started and I'm stuck:

function accessnumber($content) {

}
add_filter('the_content', 'accessnumber',60);

There are over 200 numbers so doing it manually is currently not an option for me.

Any help is appreciated!

1 Answer 1

1

You can achieve it by using following custom code.

function accessnumber( $content ) {
    $content = preg_replace( '#(<div[^>]*class=\"zebra[^>]*\">)(.*?)(</div>)#is', '$1<a href="tel:$2">$2</a>$3', $content );
    return $content;
}
add_filter( 'the_content', 'accessnumber', 60 );
8
  • Very impressive! However, It didn't cross my mind to include that not all the name tags within the divs equal Missouri. Each state has several access numbers. May 11, 2017 at 13:11
  • It doesn't, some numbers are not showing up. I'm trying to tweak your code but I am not having any luck May 12, 2017 at 7:44
  • It is working fine for me. Could you please share me the phone number and markup where it is not working? May 12, 2017 at 8:19
  • this is the link on code pen codepen.io/anon/pen/GmxVJy May 12, 2017 at 8:33
  • I tested your code on localhost machine May 12, 2017 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.