0

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

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.

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.