A
A
Alexey Nikolaev2015-01-12 16:12:53
CMS
Alexey Nikolaev, 2015-01-12 16:12:53

Is it possible to get a list of widgets in a Wordpress panel?

Hello.
The bottom line is this: the panel has a widget with php code. From the system point of view, it is always enabled and always displayed, but sometimes the php code returns false, and the widget is displayed empty. I'm looking for a way to loop through all the widgets in a panel, and thus discard those that are generated empty. Is there such a way?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Evgenievich, 2015-01-12
@Heian

So if they are generated empty, then why do you need to discard if they are already empty? Or I didn't understand the question? In general, to override the output, you need to inject your own function instead of dynamic_sidebar() and override some WP_Widget methods.
An example of replacing dynamic_sidebar() :

function advanced_dynamic_sidebar($index = 1) {
  global $wp_registered_sidebars, $wp_registered_widgets;
        $objects = array();
  if ( is_int($index) ) {
    $index = "sidebar-$index";
  } else {
    $index = sanitize_title($index);
    foreach ( (array) $wp_registered_sidebars as $key => $value ) {
      if ( sanitize_title($value['name']) == $index ) {
        $index = $key;
        break;
      }
    }
  }

  $sidebars_widgets = wp_get_sidebars_widgets();
  if ( empty( $sidebars_widgets ) )
    return false;

  if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
    return false;

  $sidebar = $wp_registered_sidebars[$index];

  $did_one = false;
  foreach ( (array) $sidebars_widgets[$index] as $id ) {

    if ( !isset($wp_registered_widgets[$id]) ) continue;

    $params = array_merge(
      array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
      (array) $wp_registered_widgets[$id]['params']
    );

    // Substitute HTML id and class attributes into before_widget
    $classname_ = '';
    foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
      if ( is_string($cn) )
        $classname_ .= '_' . $cn;
      elseif ( is_object($cn) )
        $classname_ .= '_' . get_class($cn);
    }
    $classname_ = ltrim($classname_, '_');
    $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);

    $params = apply_filters( 'dynamic_sidebar_params', $params );

    $callback = $wp_registered_widgets[$id]['callback'];

    do_action( 'dynamic_sidebar', $wp_registered_widgets[$id] );

    if ( is_callable($callback) ) {
      $did_one = true;
                        $objects[] = call_user_func_array($callback, $params);
    }
  }
  return $objects;
}

Method to be overridden in WP_Widget :
function display_callback( $args, $widget_args = 1 ) {
    if ( is_numeric($widget_args) )
      $widget_args = array( 'number' => $widget_args );

    $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    $this->_set( $widget_args['number'] );
    $instance = $this->get_settings();
                
    if ( array_key_exists( $this->number, $instance ) ) {
      $instance = $instance[$this->number];
      // filters the widget's settings, return false to stop displaying the widget
      $instance = apply_filters('widget_display_callback', $instance, $this, $args);
      if ( false !== $instance )                            
                            return $this->widget($args, $instance);
    }
  }

And, accordingly, you need to catch the widget output that occurs in the widget () method.
There may be an easier way, but I don't know it)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question