1

I'm outputting a custom user field in Wordpress, which is user's Twitter username. Some users might add it with '@' symbol, some might without. How can I output that field and check if the symbol already exists, if not, add it?

This is code I'm using to output usrename:

<?php echo get_the_author_meta('twitter_name'); ?>
yennefer
  • 189
  • 6
  • 20

2 Answers2

2
<?php
$authorMeta = get_the_author_meta('twitter_name');
if (strpos('@', $authorMeta) !== 0) {
    $authorMeta = '@'.$authorMeta;
}
echo $authorMeta;

you need to check if @ is on 1st place, you can do this in many ways

<?php
$authorMeta = get_the_author_meta('twitter_name');
if ($authoMeta[0] != '@') {
    $authorMeta = '@'.$authorMeta;
}
echo $authorMeta;
Kamil Karkus
  • 1,283
  • 1
  • 11
  • 29
1

Try this...it might help

$a = get_the_author_meta('twitter_name');
$b = explode('@',$a);
if($b[1] == 0){
    echo "@".$a;
}
else{
echo $a;
}
Moid
  • 1,447
  • 1
  • 13
  • 24