Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to put my own function in a widget but it doesn't run. I want to use it everywhere within the widget.

If I could see an example,that'd be great.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

there are a few way to run a function in widgets:

option 1 - ShortCodes

You can create a custom shortcode and run that function by entering the shortcode into a simple text widget:

//this is to make sure WordPress renders the shortcodes in a text widget
add_filter('widget_text', 'do_shortcode');

add_shortcode('my_short_code','my_function');

function my_function(){
  //do whatever you want
}

and then simply add [my_short_code] to any text widget to run your function.

Option 2 Plugins

You can use one of the many plugins out there that let you run php in a widget like:

Option 3 Custom Widgets

The last way is to write your own widget using the very simple Widgets API

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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