Instead you should consider using a shortcode. This is exactly the problem that shortcodes try to solve.
By default, Widgets don’t process shortcodes, but that can be easily changed.
add_filter( 'widget_text', 'do_shortcode' );
This would allow you to use any shortcodes in the Widget text area. Next would be to get the PHP code you want to run in a shortcode.
This can be done with the add_shortcode()
function. More details found here
add_shortcode( 'shortcode_tag', 'prefix_shortcode_tag' );
function prefix_shortcode_tag( $atts ) {
extract( shortcode_atts( array(
'foo' => 'no foo',
'baz' => 'default baz'
), $atts ) );
// your php code here
}
In the widget area, you can now use [shortcode_tag] and run the PHP code you want.
Related Posts:
- Extending the WP_Widget_Text class
- How to make widget title accept php?
- Widget back end radio button issue
- Hook called before text widget save
- How display widget by id
- Create Image Uploader for Widget
- Displaying a variable stored in functions.php inside widget
- Check if widget has content
- How do I create a drop down menu in a widget?
- display most popular tags in two columns
- List authors with posts in a category
- How to add Shortcode (font awesome) in widget title?
- Show Specific Footer Widget for Specific Pages
- WordPress menu deletes when trying to add a hook
- dynamic sidebar not showing anything
- PHP contact form returns warning in text widget [closed]
- How can I add a specific, custom widget to my theme’s header.php?
- Get widget settings function?
- Widgets not showing in my custom theme
- Class ‘WP_Widget’ not found
- nowplaying.include.php Will Not Display Results
- javaScript in section of WP API
- Style every second widget?
- Add before_content and after_content to register_sidebar
- PHP Deprecated: WP_RSSjb has a deprecated constructor
- I can’t get my custom widget area to show on my WordPress site
- How to run PHP code in Text Widget with no plugin in WP 4.4
- WordPress widget/sidebar dividers?
- How to add just one specific page to widget without plugin?
- Create onClick Event to Re-load a Widget
- How to put a variable in a instance in the widget
- How to output widget
- How to make my custom widget appear within WordPress widgets? Plugin development
- get widget number from array dynamically
- Adding widgets to my plugin page instead of WordPress dashboard
- Why my image upload button is not working in wordpress widget area?
- Adding a widget function into the php theme file
- If has $title in widgets
- Widgets in the loop if active
- Show latest posts in a plain HTML website custom widget [closed]
- How to determine if the sidebar is empty? Or not active?
- Is it bad to add html to a widget by closing and reopening the php tags?
- Widgets not showing on custom theme
- My website is not showing Footer section
- Correctly Using checked function of WordPress
- How can I display Custom Post type Custom Columns and its Content in a Dashboard Widget?
- How to get post category list as select in front-end?
- Simple php in wordpress widget [closed]
- How to use title attributes in sidebar widget?
- display specific widget to a page
- Dynamically display font icons in widget
- Correct code to use php in text widget
- random reason on refresh
- Add a select-option to the default widgets
- Stuck with sidebar registering
- Don’t display a sidebar widget when on a specific page
- Some problems in custom widget
- WordPress 6.0 class-wp-widget.php fatal errors in PHP 8.1.6
- How do you create a custom category widget that you can style?
- How a HTML form can trigger a PHP function?
- How to run php code only for a specific widget on a page and not all widgets on that page?
- How to use wp_get_attachment_image or wp_get_attachment_image_src instead of $instance[‘single_feature_image_uri’]
- Loading Widgets Via Child Theme
- How to stop hiding buttons forward / backward in pagination?
- Inserting a class on before_widget if checkbox is checked
- Current WordPress Page Title as Search Parameter into A Tag
- How to create a widget with a populated dropdown?
- dashboard widget form not submit mails
- Add widget area from visual editor
- How to make a “page-option” in WordPress
- Priority call methods – PHPMailer->addAddress(NULL)
- I need to edit a widget, with no dashboard access
- Dynamic_sidebar load in string makeup
- missing admin bar and widget in a template
- Allowing HTML elements in title widgets spacing problem
- My own theme’s custom widget areas are not working
- Add disclaimer before certain posts and pages
- Beyond widget side menu editing, with the php page, custom template
- How to list categories by page id in wordpress
- PHP: Why does my code work in index.php but not a widget?
- Are WordPress widgets cached?
- Widget edit and add in any page
- widget: input the px value from user and use it as inline style in widget function
- Slider from different widgets
- Even with PHP plugin get_posts not working in widget area
- Update title of every instance of widget using text field
- Are widget arguments always set inside My_Widget::widget()?
- WP_Widget dynamic field array with external AJAX
- WordPress – registering sidebar and adding a button directly after .textwidget
- Name Input from widget displays Sidebar name instead of saved data
- WordPress – show number of comments for each post in widget
- Using existing widget code! [closed]
- Banner editable from backend
- My custom widget won’t stay in the widget area after I refresh the widget page
- key( $GLOBALS[‘wp_registered_sidebars’] ) is always showing the same value
- How to change links in the 3 main categories, on widget category, to a javascript function call
- Using a loop to build and update widgets
- PHP Widget and do_shortcode
- How to define template directory in this widget code
- WP Debug enabled Undefined index error in a widget