admin管理员组文章数量:1429939
I'm trying to save a json array to the database, and i created the following class
defined('ABSPATH') || exit;
class Theme_FAQ {
public function __construct() {
add_action('admin_menu', array($this, 'theme_faq_admin_menu'));
}
public function theme_faq_admin_menu() {
add_menu_page('FAQ', "FAQ", 'manage_options', 'theme-faq', array($this, 'theme_faq_page'), 'dashicons-excerpt-view', 58);
}
function theme_faq_page() {
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-sortable');
wp_enqueue_script('jquery-effects-slide');
$faq = get_option('theme_faq');
if (isset($_POST['theme-faq-save'])) {
if ($faq !== $_POST['theme_faq']) {
$faq = $_POST['theme_faq'];
update_option('theme_faq', $faq);
}
}
include('html-theme-faq.php');
}
}
And in html-theme-faq.php I have a form that has a input with the name 'theme_faq', and for test i use this value
[{"title":"Title 1","content":"Content 1"},{"title":"Title 2","content":"Content 2"}]
But, if i add a var_dump($_POST), i get the following output
array(2) { ["theme_faq"]=> string(101) "[{\"title\":\"Title 1\",\"content\":\"Content 1\"},{\"title\":\"Title 2\",\"content\":\"Content 2\"}]" ["theme-faq-save"]=> string(19) "Save" }
The theme_faq value changes " to \", how can I prevent this? or reverse this?
I've already found a little workaround by changing the input names with a [] to generate an array, but still changes when use ' or " inside...
I'm trying to save a json array to the database, and i created the following class
defined('ABSPATH') || exit;
class Theme_FAQ {
public function __construct() {
add_action('admin_menu', array($this, 'theme_faq_admin_menu'));
}
public function theme_faq_admin_menu() {
add_menu_page('FAQ', "FAQ", 'manage_options', 'theme-faq', array($this, 'theme_faq_page'), 'dashicons-excerpt-view', 58);
}
function theme_faq_page() {
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-sortable');
wp_enqueue_script('jquery-effects-slide');
$faq = get_option('theme_faq');
if (isset($_POST['theme-faq-save'])) {
if ($faq !== $_POST['theme_faq']) {
$faq = $_POST['theme_faq'];
update_option('theme_faq', $faq);
}
}
include('html-theme-faq.php');
}
}
And in html-theme-faq.php I have a form that has a input with the name 'theme_faq', and for test i use this value
[{"title":"Title 1","content":"Content 1"},{"title":"Title 2","content":"Content 2"}]
But, if i add a var_dump($_POST), i get the following output
array(2) { ["theme_faq"]=> string(101) "[{\"title\":\"Title 1\",\"content\":\"Content 1\"},{\"title\":\"Title 2\",\"content\":\"Content 2\"}]" ["theme-faq-save"]=> string(19) "Save" }
The theme_faq value changes " to \", how can I prevent this? or reverse this?
I've already found a little workaround by changing the input names with a [] to generate an array, but still changes when use ' or " inside...
Share Improve this question edited May 14, 2019 at 22:44 Rodrigo Butzke asked May 14, 2019 at 19:40 Rodrigo ButzkeRodrigo Butzke 17513 bronze badges1 Answer
Reset to default 0I've found the function wp_unslash, from wp core.
$value = wp_unslash($_POST['theme_faq']);
if ($faq !== value ) {
$faq = value ;
update_option('theme_faq', $faq);
}
and now it works fine.
本文标签: optionsAdmin page changes the POST data
版权声明:本文标题:options - Admin page changes the $_POST data 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745494072a2660732.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论