Is there a way to optimize function that is used for returning data in an ajax-call?

Since you are not using $field_value after if-else, you can start rewriting that if

if (substr($field_value, 0,4) === 'http') {
   $field_value="<a target="_blank" href="" . $field_value . '">' . $field_value . '</a>'; 
} 
/* else {
   no else statement here!
} */

and just use it later like this:

$html .= '<span class="lajax-value">' . $field_value . '</span>';

More importantly, you might want to try using the reference in your loop definition

foreach($fields_cpt as $field_key => &$field_value)

If you want to take it even further you can move that url checking to the client side, so that it will run after content is rendered.

Leave a Comment