Using filter in the widget causes fatal error

Your code should look something like this: class my_Widget extends WP_Widget { … function filter_where( $where=”” ) { … } function widget( $options ) { … add_filter( ‘posts_where’, array( $this, ‘filter_where’ ) ); $query = new WP_Query( $args ); remove_filter( ‘posts_where’, array( $this, ‘filter_where’ ) ); } } This will keep the function within your … Read more

Widget only in first post

Assuming you are trying to add content only to the first post in the loop, a static variable should do it: function pietergoosen_insert_content_filter( $content ) { static $first; $first = (!isset($first)) ? true : false; if (true == $first) { ob_start(); $sidebar = dynamic_sidebar(‘sidebar-19’); $new_content = ob_get_clean(); if ( ! is_admin() ) { return pietergoosen_insert_content( … Read more

Why would apply_filters return a non-empty string, when the value returned is empty?

The problem is in the theme you’re using. This has been reported before: https://core.trac.wordpress.org/ticket/21430 Seems that Elegant Themes don’t like their widgets to not have titles. So they have a function called “et_widget_force_title” which changes blank titles into titles with a single space. In case of doubt, always try disabling plugins and switching to the … Read more

What’s the right way to share data between widgets?

Presumably, these are widgets that you are writing. If so, basic PHP class methods should do it– set a static class property to hold your “overlap” data. class Foo extends WP_Widget { static $overlap; /*constructs etc*/ function __construct($id = ‘twidg’, $descr=”Test Widget”, $opts = array()) { $widget_opts = array(); parent::__construct($id,$descr,$widget_opts); /*do stuff*/ } function widget() … Read more

Correct code to use php in text widget

I think this is enough, inside the widget-function of your Widget-class. ob_start(); eval( ‘?>’ . $html ); $html = ob_get_contents(); ob_end_clean(); Alternative use a maintenant plugin for this job, like PHP Code Widget

Random sidebar/widget background

Place the following code above the DOCTYPE in header.php: <?php $bg = array(‘bg-01.jpg’, ‘bg-02.jpg’, ‘bg-03.jpg’, ‘bg-04.jpg’, ‘bg-05.jpg’, ‘bg-06.jpg’, ‘bg-07.jpg’ ); // array of filenames $i = rand(0, count($bg)-1); // generate random number size of the array $selectedBg = “$bg[$i]”; // set variable equal to which random filename was chosen ?> Now place this code within … Read more

Widgets not showing on sidebars

You’ve registered your sidebars with the ids of sidebar-left and sidebar-right but you are are attempting to display them using only left and right. You need to use the correct ids. That is: dynamic_sidebar( ‘sidebar-left’ ); You need that id with both dynamic_sidebar() and is_active_sidebar() if ( is_active_sidebar( ‘sidebar-left’ ) ) { dynamic_sidebar( ‘sidebar-left’ ); … Read more

the_widget() inside functions.php

The function the_widget() does not return any output it only echos it. If you want to save its output to a variable you need to use output buffering. ob_start(); the_widget(‘My_Custom_Widget’); $w = ob_get_contents(); ob_end_clean();