0

I have a json file I am displaying the contents of on my wordpress site. The issue stems from the fact that some of the values I'm returning I need to sort through later, and while that value is being echoed as text, an apostrophe is being displayed as an apostrophe, however if I am echoing the exact same value inside HTML(like as a data attribute or in javascript) it returns as a single quote.

I need this value to return the same format no matter where it is being echoed. This is because this data will later be used to filter through a large data table.

<?php $content = file_get_contents('jsonfilepath');
$x = json_decode($content);

$array = array(); 
foreach($x as $row) {

             $value = $row->name;
                        if(!in_array($name, $array)){
                               array_push($array,$value);
                        }

                    };

                    sort($array);
                    foreach( $array as $name ){

                        $name = mb_convert_encoding($name,"UTF-8");
                        $name = ucwords(strtolower($name));
                        $name = html_entity_decode($name, ENT_QUOTES, "UTF-8");
                        $name = str_replace("&#8217;","'", $name); 
                        $name = htmlspecialchars_decode(ucwords(strtolower($name)));
                        $name = implode('/', array_map('ucfirst', explode('/', $name)));
                        echo '<option value="'.utf8_decode($name).'">'.utf8_decode($name).'</option>';
                    }

This returns

<option value="Chicago/O'hare">Chicago/O&#8217;hare</option>

I am almost at the point of running a script after the page loads to replace all instances of this apostrophe, but there has to be a serverside way to handle this.

4
  • Where do you call the code? In a shortcode? A template file? And I don't see any WP-specific functions in your code. Have you tried var_dump( $name ); at the top of the foreach?
    – Sally CJ
    Nov 22, 2019 at 22:55
  • I'm calling it in a template file.
    – Benlov
    Nov 22, 2019 at 23:20
  • How are you calling it? What's the code in your template? And you didn't answer the var_dump question.
    – Sally CJ
    Nov 22, 2019 at 23:36
  • string(14) &#8220;Chicago/O&#8217;hare&#8221; is what it returns
    – Benlov
    Nov 25, 2019 at 14:53

1 Answer 1

0

I found the issue. This was being echoed in the_content and Wordpress's texturize was converting to smartquotes. I've disabled that on The Content and that seems to have solved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.