Data Validation for Dynamic Fontawesome Name

Hi there,
I have made fontawesome name dynamic using custom field. The code snippet is something like this:

$m_service_icon = get_post_meta( $post->ID, ‘service_icon’, true );

fa-<?php if($m_service_icon): echo $m_service_icon; else: echo ‘wordpress’; endif

And I would like to know that whether I have to validate $m_service_icon or not.

If I have to, then how can I validate that?
In this way… esc_attr($m_service_icon)
or, esc_html($m_service_icon) ?

I am eagerly looking forward to your help. :slight_smile:
Thanks for taking the trouble to read.

You need to validate, use attr if the content doesn’t include HTML

1 Like

Thanks much @ki-themes :slight_smile: . Then in my code I need to use attr, is it?

Seems so, yes

1 Like

sorry to bother you again. :).
Do I have to validate a static data?

echo esc_attr($m_service_icon);
echo esc_attr(‘wordpress’);

In this case… esc_attr(‘wordpress’) ?

‘wordpress’ is a defualt data which comes in action when a user doesn’t set any fontawesome name in the post editor.

Thanks again.

no, you don’t need for static data, you could just use echo “wordpress”

1 Like