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!


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 );
| improve this answer | |
  • 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. – Fabian Amran May 11 '17 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 – Fabian Amran May 12 '17 at 7:44
  • It is working fine for me. Could you please share me the phone number and markup where it is not working? – Vinod Dalvi May 12 '17 at 8:19
  • this is the link on code pen codepen.io/anon/pen/GmxVJy – Fabian Amran May 12 '17 at 8:33
  • I tested your code on localhost machine – Fabian Amran May 12 '17 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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