options - Admin page changes the $_POST data

I'm trying to save a json array to the database, and i created the following classdefined('ABSPATH') || e

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

I'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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745494072a4630116.html

相关推荐

  • options - Admin page changes the $_POST data

    I'm trying to save a json array to the database, and i created the following classdefined('ABSPATH') || e

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信