php - JavaScript Object -> AJAX -> wp_options -> echo theObject? -
i've been trying solve using various methods json.stringify
, json.parse
, json_encode()
, json_decode()
, stripslashes()
, find , replace
, etc, etc.
but can't figure out , if encode js object database intact.
here i'm doing @ moment:
1. send js object via ajax wp plugin:
var data = { action : 'store_object', obj: myobject }; $.post(ajaxurl, data).done(function(res) { console.log(res); });
2. save object in wp plugin's options:
$myobject = $_post['obj']; $options = $this->get_admin_options(); $options['settings'] = $myobject; update_option($this->admin_options_name, $options);
3. print saved object javascript object:
$options = $this->get_admin_options(); ?> <script> ;(function ($, window, document, undefined ) { $(document).ready(function() { $('#el').pluginname(<?php echo stripslashes(json_encode($options['settings'])); ?>); }); })(jquery, window, document); </script>
the contents of myobject may contain html code or special characters, , problem comes from. can't figure out how cover corner cases. example, code above translate this:
<a href="http://google.com">link</a> hello! new line.
into this:
<a href="http://google.com">link</a> hello!n new line.n
the html renders properly, \n symbol not.
so, safest way this?
js object -> ajax -> wp database -> echo object js object
Comments
Post a Comment