1

I'm trying to pass a post's content (which may contain HTML) to JavaScript via wp_localize_script(). wp_localize_script() can't handle multi-dimensional arrays, so I'm encoding it in JSON and then decoding it with jQuery.

That works fine as long as you it's only text and you replace the " entity with a regular " before you call $.parseJSON(). If you try to parse a post with HTML in it, though, you get errors like,

JSON.parse: expected property name or '}' http://redacted.local/wp/wp-includes/js/jquery/jquery.js?ver=1.6.1 Line 16

So, I'm guessing I need to do some more string manipulation before trying to parse it, to convert more entities back to the regular characters, but I don't want to just pick the few characters I'm having errors with because I'm sure there are dozens more that I might miss. Is there a comprehensive or standard way of doing this? I've done a lot of searching and having found any answers, so it makes me think I'm missing something obvious.

Here's the PHP side:

public function loadResources()
{
    // ... 

    wp_register_script(
        'bgmp',
        plugins_url( 'functions.js', __FILE__ ),
        array( 'googleMapsAPI', 'jquery' ),
        self::BGMP_VERSION,
        true
    );

    // ...

    if( !is_admin() && $this->mapShortcodeCalled )
    {
        // ...

        wp_enqueue_script('bgmp');

        $bgmpData = array(
            'options'   => $this->getMapOptions(),
            'markers'   => $this->getPlacemarks()
        );
        wp_localize_script( 'bgmp', 'bgmpData', $bgmpData );
    }

    // ...
}

public function getPlacemarks()
{
    // ...

        foreach( $publishedPlacemarks as $pp )
        {
            // ...

            $placemarks[] = array(
                // ...
                'details'   => $pp->post_content,
                // ...
            );
        }

    return json_encode( $placemarks );
}

And here's the JavaScript side

// ...

init : function()
{
    // ...

    bgmpData.markers        = $.parseJSON( bgmpData.markers.replace(/"/g, '"') );

    // ...
},

// ...
6
  • There is a way to get multidimensional arrays to work with wp_localize_script. I wrote an article about it here: wpmods.com/… Commented Jul 23, 2011 at 20:37
  • @tollmanz it would be better and more in line with site's mechanics if you posted that solution as an answer (which would be fine to contain link to your tutorial as source). Commented Jul 23, 2011 at 21:42
  • @Rarst...I did it this because I don't think I was actually answering his question. His question was more about encoding and decoding the JSON and I wanted to just pass along a suggestion that would avoid that mess Commented Jul 23, 2011 at 22:22
  • I commented on Rarst's answer explaining why I don't want to use that method. Commented Jul 23, 2011 at 22:27
  • @tollmanz still it should have gone into answer. Comments are for discussion, answers are for solutions (even if they are not precise answer to the question). Commented Jul 24, 2011 at 16:55

2 Answers 2

3

While it's not exact answer to your precise question, I agree with method suggest in comment. Just skip trying to stuff data into single dimension and make use of l10n_print_after argument instead.

See pass object/JSON to wp_localize_script question and answer there.

4
  • I'm aware of the l10n_print_after hack and that WPSE thread, but I dismissed it because it looks like it's going to be deprecated in the future per trac #11520 Commented Jul 23, 2011 at 22:06
  • @Ian Dunn hmmm, I am not sure that would be for better. The nice thing about print after thing is that it can be used to output code, not just some variable. Anyway if it does get removed and array support introduced - your question goes away. If it doesn't - print after stands. :) Commented Jul 24, 2011 at 8:58
  • Yeah, I guess I'll just use l10n_print_after for now and keep an eye on that ticket, then release an update to the plugin if it changes. Commented Jul 24, 2011 at 16:44
  • @Ian, looks like the future arrived and they decided to keep it for backwards compatibility (see wp-includes/class.wp-scripts.php:482). It still works Commented Jan 17, 2021 at 2:08
0

placemarks[] = array( this means that placemarks is array of arrays, is that right?

also you don't need to entities back and forth , just call $.parseJSON() on all of the bgmpData var:

var bgmpData_parsed = $.parseJSON( bgmpData);

thin you can access markers by bgmpData_parsed.markers

1
  • Yes, $placemarks is intended to be a multi-dimensional array. Not all properties of bgmpData are encoded in JSON, so I need to call it on individual properties, and I don't see a point in creating an additional variable. Commented Jul 23, 2011 at 21:57

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.