What you’d need to do is double-escape, so change echo '\'' . $term->name . '\', ';
to echo '\\\'' . $term->name . '\\\', ';
…but, you’d be much better off just using different encapsulators:
<select class="w-100" name="mf_term">
<option value="
<?php
$terms = get_terms( 'call-type' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo "'{$term->name}', ";
}
}
?>
">All</option>
As you’re echo’ing a string that, not directly appending content to the HTML, you can use the double-quotes to encapsulate the output without having to worry about accidentlly exiting your select object early.
In addition, when using double-quotes with a standard PHP variable ($myVariable
) or class / object property ($myClass->myProperty
/ $myObject->myProperty
) you can just shove it in, and PHP will assess it correctly (I like to add curly braces for good measure, but it’s not absolutely required); meaning you don’t have to escape the string at all.
Finally; the code you’ve submitted will only create a single option in your dropdown, with the value of 'All', 'Completed', 'In progress', 'New',
. If you’re looking to have a list of options in your select box (so you can select All
, Completed
, In progress
or New
), the following may be more effective:
<select class="w-100" name="mf_term">
<?php
$terms = get_terms( 'call-type' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo "<option value="{$term->name}">{$term->name}</option>";
}
}
?>
</select>