E.g. when I want to edit something in my WP database, I get a headache, literally. Because I see something like this:

 a:92:{s:47:"category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$";s:52:"index.php?category_name=$matches[1]&feed=$matches[2]";s:42:"category/(.+?)/(feed|rdf|rss|rss2|atom)/?$";s:52:"index.php?category_name=$matches[1]&feed=$matches[2]";s:35:"category/(.+?)/page/?([0-9]{1,})/?$";s:53:"index.php?category_name=$matches[1]&paged=$matches[2]";s:17:"category/(.+?)/?$";s:35:"index.php?category_name=$matches[1]";s:44:"tag/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$";s:42:"index.php?ta-9]{1,})/?$";s:50:"index.php?attachment=$matches[1]&cpage=$matches[2]";}

I guess it’s some kind of array, but my text editor cannot format that to something like this:

Array
(
    [a] => apple
    [b] => banana
     => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)

how can this be done with the stored data in PW database? Is there some online tool for that or maybe some plugin for Sublime Text 2 or N++?

1 Answer
1

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:

  • Settings API with arrays example
  • How to store widget fields data as an array?
  • How do I retrieve multi-dimensional arrays from the wp_postmeta table, & display on a website?
  • WordPress serializes options and meta for you.

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 Reply

Your email address will not be published. Required fields are marked *