1

I have an existing widget area / sidebar. Rather than adding content to it via Appearance ? Widgets in the Dashboard, I want to inject content into the widget area from functions.php.

Is there hook that allows this?

Thanks :)

0

Yes by using widget_text inject content into the widget area

Add the following code into your function.php file

function widget_content( $content )
{
    // add $content as you want
    return $content;
}

add_filter( 'widget_text', 'widget_content', 99 );
  • Great. Thank you, @amitrh. How would I target a specific widget area? I know the widget name. – iltdev Dec 18 '14 at 13:07
  • Hey use is_active_widget to target widget area by passing widget id – Amit Mishra Dec 18 '14 at 13:17
0

Also, you can write a function in the functions.php file and call that function via the widget.

Functions.php:

function myFunction() {
    //do something here
} 
add_shortcode('wp_my_function', 'myFunction');

//Allow write shortcode in widgets  
add_filter('widget_text', 'do_shortcode');

In the Widget:

Use the text widget, write in the shortcode: [wp_my_function]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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