3

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
    [c] => 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

4

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( '/' ) );
        exit;
    });
}
8
  • but, can you tell me how the WP developers check these values in the db via some db client or phpmyadmin, just to check the actions etc. during development. It's very inconvinient this way as it is stored in the database. E.g. I run some function and then check the result in the db. if something was edited or changed as it supposed to be. But if I see the "serialized value" I am completely lost with verifing. Any idea how to solve this issue when developing plugins for wordpress?
    – Derfder
    May 22, 2013 at 11:30
  • @Derfder See my update.
    – fuxia
    May 22, 2013 at 11:44
  • Yeah, htanks , however I would still prefer to make fast corrections and changes via the database editor like Heidi or even phpmyadmin e.g. for unsetting cron for plugins etc. Anyway, thanks for your answer.
    – Derfder
    May 22, 2013 at 11:46
  • 2
    @Derfder Since you have asked for a tool, I have written a simple dashboard widget. It should show how these things work.
    – fuxia
    May 22, 2013 at 12:06
  • 1
    @Derfder Hm, good idea. See PHP.js: serialize() and unserialize().
    – fuxia
    May 22, 2013 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.