Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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, '"') );

    // ...
},

// ...
share|improve this question
    
There is a way to get multidimensional arrays to work with wp_localize_script. I wrote an article about it here: wpmods.com/… –  tollmanz Jul 23 '11 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). –  Rarst Jul 23 '11 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 –  tollmanz Jul 23 '11 at 22:22
    
I commented on Rarst's answer explaining why I don't want to use that method. –  Ian Dunn Jul 23 '11 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). –  Rarst Jul 24 '11 at 16:55
show 1 more comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
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 –  Ian Dunn Jul 23 '11 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. :) –  Rarst Jul 24 '11 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. –  Ian Dunn Jul 24 '11 at 16:44
add comment

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

share|improve this answer
    
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. –  Ian Dunn Jul 23 '11 at 21:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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