Formatting of curly brackets array from WP database to get more readable output

This is a serialized value, so you should run it through maybe_unserialize() or just unserialize() before you edit it.

When you work in plugins or themes, always use the API: update_option() and get_option() for example. These functions will un/serialize the values for you, so you don’t have to worry about the database.

See also:

If you need just a simple converter, use WordPress’ internals: a simple dashboard widget and the built-in functions.

enter image description here

<?php # -*- coding: utf-8 -*-
/* Plugin Name: Unserialize Dashboard Widget */

add_action( 'wp_loaded', 't5_unserialize_dashboard_widget' );

function t5_unserialize_dashboard_widget()
{
    $handle="t5sdw";

    add_action( 'wp_dashboard_setup', function() use ( $handle ) {

        wp_add_dashboard_widget(
            $handle . '_widget',
            'Unserialize',
            function() use ( $handle ) {

                print '<form action="' . admin_url( 'admin-post.php?action=' . $handle ) . '" method="post">';
                wp_nonce_field( 'update', $handle . '_nonce' );
                $content = esc_textarea( var_export( maybe_unserialize( get_option( $handle ) ), TRUE ) );

                print "<textarea name="$handle" class="code large-text" rows=10>$content</textarea><p>";
                submit_button( 'Unserialize', 'primary', $handle . '_unserialize', FALSE );
                print '</p></form>';
            }
        );
    });

    add_action( "admin_post_$handle", function() use ( $handle ) {

        if ( ! isset ( $_POST[ $handle ] )
            or ! wp_verify_nonce( $_POST[ $handle . '_nonce' ], 'update' ) )
            return;

        parse_str( file_get_contents( 'php://input', 'r'), $arr );
        update_option( $handle, $arr[ $handle ] );
        wp_redirect( admin_url( "https://wordpress.stackexchange.com/" ) );
        exit;
    });
}

Leave a Comment