Basically I'm creating an options page in which the user will click on a button to display a text input field, which will then be populated by the user with a name.

This name will be then saved into the db using ajax (all that's working already). The user will be able to as many names she/he wants.

At the moment, as a feedback to the user, I'm adding a list with the names that were previously saved with jQuery, however I'd like to load the options from the db to check whether the info collected by jQuery is correct.

I've been doing some research and it seems the way to do it is to use wp_localize_script which I'm finding it cumbersome to load dynamically created data.

Is using wp_localize_script the correct way to do it or should I just make an ajax call as you'd normally do?


I don't know if there is really a right answer to this question, and I don't know what you find cumbersome about wp_localize_script, but you should be able to do it either way.

The difference is that with wp_localize_script the data is printed to the source of the page, which you wouldn't want if the data were sensitive, and wp_localize_script should represent a wee bit less load on the server as it doesn't require a separate connection as AJAX does.

I'd lean towards wp_localize_script unless there are compelling reasons to do something else.

| improve this answer | |
  • I find it cumbersome because in order to retrieve a relatively simple data I'll need to create a php function, use add_action and write some js as opposed to simple write some js and retrieve the data. This process will be repeated a few times. Both options have downsides: the wp_localize_script will pollute my application (I'll of course try to concatenate most of the functions together, but maybe not all) whilst creating multiple ajax calls will have a negative impact on performance. However this functionality is for WP admin area only, so won't be public facing. – WagnerMatosUK Jun 2 '13 at 16:25
  • Then take another look at wp_localize_script. If your existing scripts are enqueued correctly then none of that should matter. You already have everything in place that you need and it is a matter of adding one or two lines of PHP. – s_ha_dum Jun 2 '13 at 16:32
  • 1
    "pollute" you application? What does that mean? – s_ha_dum Jun 2 '13 at 16:33
  • Sorry, if I wasn't clear. I meant adding more code than necessary... I'll have another look at wp_localize_script. Maybe the example I've seen wasn't clear enough or I didn't really understand it. At least now I know that seems to be the best way anyway. Thanks for your help. – WagnerMatosUK Jun 2 '13 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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