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 :)
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityYes 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 );
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]