Widget Example using dynamic params based off settings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Example Widget | |
* | |
*/ | |
class ck_example_widget extends WP_Widget { | |
/** Constructor */ | |
function __construct() { | |
parent::__construct( 'ck_example_widget', Example Widget, array( 'description' => 'My Example Widget' ) ); | |
add_filter( 'dynamic_sidebar_params', array( $this, 'widget_class' ), 10, 1 ); | |
} | |
/** @see WP_Widget::widget */ | |
function widget( $args, $instance ) { | |
$args['id'] = ( isset( $args['id'] ) ) ? $args['id'] : 'ck_example_widget'; | |
$instance['title'] = ( isset( $instance['title'] ) ) ? $instance['title'] : ''; | |
$title = apply_filters( 'widget_title', $instance['title'], $instance, $args['id'] ); | |
echo $args['before_widget']; | |
if ( $title ) { | |
echo $args['before_title'] . $title . $args['after_title']; | |
} | |
// Your widget content | |
echo $args['after_widget']; | |
} | |
/** @see WP_Widget::update */ | |
function update( $new_instance, $old_instance ) { | |
$instance = $old_instance; | |
$instance['title'] = strip_tags( $new_instance['title'] ); | |
$instance['add_wrapper_class'] = isset( $new_instance['add_wrapper_class'] ); | |
return $instance; | |
} | |
/** @see WP_Widget::form */ | |
function form( $instance ) { | |
$defaults = array( | |
'title' => '', | |
'add_wrapper_class' => false, | |
); | |
$instance = wp_parse_args( (array) $instance, $defaults ); ?> | |
<p> | |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> | |
Widget Title | |
</label> | |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo $instance['title']; ?>"/> | |
</p> | |
<p> | |
<input <?php checked( $instance['add_wrapper_class'], true ); ?> id="<?php echo esc_attr( $this->get_field_id( 'add_wrapper_class' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'add_wrapper_class' ) ); ?>" type="checkbox" /> | |
<label for="<?php echo esc_attr( $this->get_field_id( 'add_wrapper_class' ) ); ?>"> | |
Apply CSS Class to Wrapper? | |
</label> | |
</p> | |
<?php | |
} | |
/** | |
* Check the widget options before it's wrapper loads to add classes | |
* @param $params | |
* | |
* @return array | |
*/ | |
public function widget_class( $params ) { | |
if ( strpos( $params[0]['widget_id'], 'ck_example_widget' ) !== false ) { | |
$instance_id = $params[1]['number']; | |
$all_settings = $this->get_settings(); | |
$instance_settings = $all_settings[ $instance_id ]; | |
if ( ! empty( $instance_settings['add_wrapper_class'] ) ) { | |
/** | |
* Here, you can do some logic to determine what $class should be equal to. | |
* | |
* For example, on Easy Digital Downloads we look for the option and check if the cart | |
* is empty or not using something like: | |
* $class = ! empty( edd_get_cart_contents() ) ? 'cart-not-empty' : 'cart-empty'; | |
*/ | |
$class = 'my-awesome-css-class'; | |
$params[0]['before_widget'] = preg_replace( '/class="(.*?)"/', 'class="$1 ' . $class . '"', $params[0]['before_widget'] ); | |
} | |
} | |
return $params; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment