Data Validation for Dynamic Fontawesome Name


#1

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.


#2

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


#3

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


#4

Seems so, yes


#5

@ki-themes,
sorry to bother you again. :).
Do I have to validate a static data?

if($m_service_icon):
echo esc_attr($m_service_icon);
else:
echo esc_attr(‘wordpress’);
endif

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.


#6

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


#7

:slight_smile: