In WordPress where the content of text widget are stored?

Grey-lover picture Grey-lover · Jan 14, 2017 · Viewed 11.6k times · Source

In Wordpress, when we make a text widget in the sidebar, is the content of the text saved in a variable or in the database?

If it's a variable, which variable is it?

If it's the database, which table is it?

Any help is appreciated.

Update:

What I am trying to achieve is that I will make a plugin that will check if the user has created text widget on their site, if yes, then I would like the plugin to fetch it's content, send it to my server, run it through my spellchecker, bring the updated content back and replace the old content of the text widget.

So, this is why I want to know how I can access the exact content of the text widget, as entered by the site owner.

Answer

Raunak Gupta picture Raunak Gupta · Jan 14, 2017

Content of Text Widget are stored in wp_options table under option_name > widget_text. in serialize form

You can use this MySQL query to get that:

SELECT * FROM `wp_options` WHERE `option_name` = 'widget_text' 

UPDATED

If you only want to get the content of the text widget then you have to create a custom function which will return the content, provided you know the title.

Here is the code:

function abc_getWidgetContentbyTitle($widget_title)
{
    $contect = '';
    $widgets = get_option('widget_text');
    if (!empty($widgets)):
        foreach ($widgets as $widget)
        {
            if ((!empty($widget['title'])) && $widget['title'] == $widget_title):
                $contect = $widget['text'];
            endif;
        }
    endif;
    return $contect;
}

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

Usage

echo abc_getWidgetContentbyTitle('some txt widget 1'); //replace it with your title.

The code is tested and fully functional.

Please Note: I have assumed that you have value like this


UPDATED (after reading your exact requirements)

You can use widget_update_callback hook to get the user entered content and then do your spell check and replace it with the update one.

Here is an example:

add_filter('widget_update_callback', function( $instance, $new, $old, $obj )
{
    if ('text' === $obj->id_base && !empty($instance['text']))
    {
        $title = $instance['title'];
        $content = $instance['text'];

        //...
        //Now do your stuff and update $content.
        //...

        $instance['title'] = $title;
        $instance['text'] = $content;
    }

    return $instance;
}, 10, 4);

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
The code is tested and fully functional.

Reference: