Jump to content

Recommended Posts

Hello,

 

Let me explain that title, it's kinda involved.

 

I am attempting to generate a dynamic keyword for the Amazon MP3 Widget. No matter which page you're on, the PHP variable $keyword will contain a string. That string is being used within the widget's Javascript code. Here is the part of the code that matters:

 

<script type='text/javascript'>
amzn_wdgt.title='<?php echo $keyword; ?>';
</script>

 

Everything works fine, meaning the keyword is appropriately transferred into the script. However, not all strings are displaying correctly. As you can see in the example below, the widget in the top right corner is not appropriately translating the apostrophe. When the variable is echoed outside the Javascript, there is no problem, the apostrophe displays appropriately. It only occurs when the variable is echoed inside the Javascript:

 

Outside Javascript

Beethoven's Trinklied No. 282

 

Inside Javascript

Beethoven&#8217;s Trinklied No. 282

 

Here is the live site. The widget is in the top right of the sidebar. It is the widget title that is in question:

 

http://www.drinking-songs.com/beethovens-trinklied-no-282

 

I have applied various ASCII decode functions, addslashes, etc. to the variable, but nothing seems to work. I have a feeling there is something within the Javascript knowledgebase that I'm not aware of. Any help is appreciated. Thanks!

Those are HTML entities. Flash is not HTML, so you need to decode them. You could pass the string through html_entity_decode:

 

amzn_wdgt.title='<?php echo html_entity_decode($keyword); ?>';

 

Ideally though you should find where the string is originally converted to entities (either with htmlentities or htmlspecialchars) and remove the escaping there. Also, removing the entity conversion would mean that the single quote would now break the JS syntax, so I would pass the string through json_encode without the surrounding quotes:

 

amzn_wdgt.title=<?php echo json_encode($keyword); ?>;

 

That will result in escaped notation that JS can understand. Don't forget to remove the HTML entity escaping though.

Thanks for the reply, but it doesn't seem to work. This is what I did. First I applied the HTML entity decode function:

 

<?php $keyword = html_entity_decode($keyword); ?>

 

Then I applied the json encode within the Javascript:

 

amzn_wdgt.title=<?php echo json_encode($keyword); ?>;

 

As you can see, it still appears with the special characters:

 

http://www.drinking-songs.com/beethovens-trinklied-no-282

The entities have been double encoded by the look of it.

 

First it was:

Beethoven&#8217;s Trinklied No. 282

 

Now it's:

"Beethoven&#8217;s Trinklied No. 282";

 

So a second time through html_entity_decode() should produce:

Beethoven’s Trinklied No. 282

 

I strongly urge you to investigate why the value is double encoded though, instead of just passing it through several times.

Passing it through a second time didn't work. Neither did applying the json_encode() function outside the Javascript. The string comes from the get_the_category() and get_the_title() Wordpress functions. I'm not sure where the encoding occurs, but it's somewhere in the Wordpress core files, which I would rather not change.

 

Thanks for all your help.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.