4

I have text widgets that contain text. I'd like to programmatically edit that text.

Is there a way to do that?

2
  • How do you want to identify target widget for starters?
    – Rarst
    Jul 18, 2014 at 18:25
  • @Rarst, By any means necessary, really. It'd be nice to do it by widget name, i.e. "Homepage Left Widget."
    – Jonathan
    Jul 21, 2014 at 13:37

1 Answer 1

5

I guess you want to locate the widget by it's title, so you can try this function:

/**
 * Update a widget text located by it's title
 * 
 * @see https://wordpress.stackexchange.com/a/155518/26350
 *
 * @param string $search_title
 * @param string $new_text
 * @param boolean
 */

function wpse_155046_update_widget_text_by_title( $search_title, $new_text )
{
    // Get all data from text widgets 
    $widgets = get_option( 'widget_text' );

    foreach( $widgets as $key => $widget )
    {       
        // Compare and ignore case:
        if( mb_strtolower( $search_title ) === mb_strtolower( $widget['title'] ) )
        {
            // Replace the widget text:
            $widgets[$key]['text'] = $new_text;

            // Update database and exit on first found match:
            return update_option( 'widget_text', $widgets );
        }
    }
    return false; 
}

where we only replace the text of the first widget instance that matches the given title.

Usage example:

You can apply the above function like demonstrated here:

if( wpse_155046_update_widget_text_by_title( 
        'My fruits', 
        'Five green apples and nine oranges.' 
     ) 
)
{
    echo 'success';
}
else
{
    echo 'no success';
}

where we replace the text of the widget with the title My fruits.

Before:

before

After:

after

You can also check out my answer here on where the widget data is located in the database.

Update:

A good question from @Tony in the comments below, how one could replace the widget text given the instance number. Here's an untested idea:

/**
 * Update a widget text located by it's instance number
 * 
 * @see https://wordpress.stackexchange.com/a/155518/26350
 *
 * @param string $id
 * @param string $new_text
 * @param boolean
 */

function wpse_155046_update_widget_text_by_instance_number( $instance_number, $new_text )
{
    // Get all data from text widgets 
    $widgets = get_option( 'widget_text' );

    if( isset( $widgets[$instance_number]['text'] ) ) 
    {
        // Replace the widget text:
        $widgets[$instance_number]['text'] = $new_text;

        // Update database and exit on first found match:
        return update_option( 'widget_text', $widgets );
    }
    return false; 
}
2
  • thanks @birgire for your helpful answer but how i can search for that widget by it's id instead of it's title?
    – Antwan
    Oct 9, 2014 at 10:59
  • @Tony I guess you mean the instance number, I updated the answer. Hopefully it helps.
    – birgire
    Oct 9, 2014 at 11:32

Your Answer

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

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