Graham
Graham

Reputation: 162

PHP/jQuery UTF issue

Morning folks,

Having a real head-scratcher of a problem this morning I was hoping you may be able to assist with.

I have a background switcher plugin, external to my Wordpress site. It expects images to be provided in the following format:

["http://www.path/to/image.jpg", "http://www.path/to/image2.jpg"]

I'm passing a string from my site to the external script using the following function:

<?php 
if($page_id == 5) { ?>

  <?php
  $cat            = 62; //category id
  $posts          = get_posts('showposts=-1&order=ASC&cat='. $cat);
  $list           = "'";

  if ($posts) {
    foreach($posts as $post) {
      $imgsrc           = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), "Full");
      $featuredimgpath  = $imgsrc[0];
      $list             .= '"'.$featuredimgpath.'",';
    } 
  }

  $list = substr($list, 0, -1);
  $list .= "'";
  echo "<script charset='utf-8'>var paths=encodeURIComponent($list);</script>";
  ?>

And the receiving function handles it like this...

paths = decodeURIComponent(paths);

if(jQuery().bgswitcher) {
    $(".splash").bgswitcher({
      images: [paths], // Background images
      effect: "fade", // fade, blind, clip, slide, drop, hide
      interval: 4000, // Interval of switching
      loop: true, // Loop the switching
      shuffle: false, // Shuffle the order of an images
      duration: 1500, // Effect duration
      easing: "linear" // Effect easing
    });
}

The result is a 404 for all associated images, although the console log and alert outputs display the string in the expected format.

The only thing I noticed was the receiving script outputting the string with ASCII/UTF-8 chars like %22 replacing quotes. This is why I used the encodeURI function but the problem persists.

Does anyone know how I could get around this? Or even if I'm approaching the problem in the wrong way?

Thanks in advance, as ever! Graham

Upvotes: 0

Views: 43

Answers (2)

Graham
Graham

Reputation: 162

Figured it out! I just printed the jQuery to the page direct using the PHP - that eliminated the cross-file encoding problem altogether :)

Upvotes: 0

Taha Paksu
Taha Paksu

Reputation: 15616

change this line:

echo "<script charset='utf-8'>var paths=encodeURIComponent($list);</script>";

to

echo "<script charset='utf-8'>var paths=$list;</script>";

Upvotes: 1

Related Questions