Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Functions and Widget Files for WCSD12 Action and Filters
<?php
/*
Plugin Name: Basic Awesome Widget
Plugin URI: http://pixeljar.net
Description: Very simple sidebar widget that just displays awesome
Author: Jeffrey Zinn
Version: 1.0
Author URI: http://jzinn.us/
*/
/**
* Adds Basic_Awesome_Widget widget.
*/
class Basic_Awesome_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
public function __construct() {
parent::__construct(
'basic_awesome_widget', // Base ID
'Basic Awesome Widget', // Name
array( 'description' => __( 'Basic Awesome Widget', 'text_domain' ), ) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
echo '<h2>awesome</h2>';
echo $after_widget;
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Basic Awesome Widget', 'text_domain' );
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
} // class Basic_Awesome_Widget
add_action( 'widgets_init', create_function( '', 'register_widget( "basic_awesome_widget" );' ) );
<?php
/*
Plugin Name: Deluxe Awesome Widget
Plugin URI: http://pixeljar.net
Description: Very simple sidebar widget to demonstrate actions and filters
Author: Jeffrey Zinn
Version: 1.0
Author URI: http://jzinn.us/
*/
/**
* Adds Deluxe_Awesome_Widget widget.
*/
class Deluxe_Awesome_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
public function __construct() {
parent::__construct(
'deluxe_awesome_widget', // Base ID
'Deluxe Awesome Widget', // Name
array( 'description' => __( 'Deluxe Awesome Widget', 'text_domain' ), ) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
do_action( 'before_awesome_widget' );
echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' );
do_action( 'after_awesome_widget' );
echo $after_widget;
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Deluxe Awesome Widget', 'text_domain' );
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
} // class Deluxe_Awesome_Widget
add_action( 'widgets_init', create_function( '', 'register_widget( "deluxe_awesome_widget" );' ) );
<?php
/**
* ACTIONS
*/
//add_action( 'before_awesome_widget', 'puppy' );
//add_action( 'before_awesome_widget', 'puppy', 2 );
function puppy() {
$src = 'http://placedog.com/g/188/150';
echo '<img src="' . $src . '" />';
}
//add_action( 'before_awesome_widget', 'warning' );
//add_action( 'before_awesome_widget', 'warning', 1 );
function warning() {
echo '<p>Warning: Puppies!</p>';
}
//remove_action( 'before_awesome_widget', 'warning', 1 );
/**
* FILTERS
*/
//add_filter( 'filter_awesome_text', 'filter_awesome' );
//add_filter( 'filter_awesome_text', 'filter_awesome', 2 );
function filter_awesome( $text ) {
$text .= '<h4>puppies</h4>';
return $text;
}
//add_filter( 'filter_awesome_text', 'second_filter' );
//add_filter( 'filter_awesome_text', 'second_filter', 1 );
function second_filter( $text ) {
$text = '<h2>dogs!</h2>';
return $text;
}
//remove_filter( 'filter_awesome_text', 'second_filter', 1 );
//add_filter( 'filter_ridiculous_text', 'ridiculous_filter', 10, 3 );
function ridiculous_filter( $text, $var1, $var2 ) {
$var2 = "not awesome";
$new = sprintf( '<%s>%s</%s>', $var1, $var2, $var1 );
return $new;
}
<?php
/*
Plugin Name: Ridiculous Awesome Widget
Plugin URI: http://pixeljar.net
Description: Very simple sidebar widget to demonstrate actions and filters
Author: Jeffrey Zinn
Version: 1.0
Author URI: http://jzinn.us/
*/
/**
* Adds Ridiculous_Awesome_Widget widget.
*/
class Ridiculous_Awesome_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
public function __construct() {
parent::__construct(
'ridiculous_awesome_widget', // Base ID
'Ridiculous Awesome Widget', // Name
array( 'description' => __( 'Ridiculous Awesome Widget', 'text_domain' ), ) // Args
);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
do_action( 'before_ridiculous_widget' );
$wrap = ( is_home() ) ? 'h2' : 'h3';
$text = 'awesome';
$pattern = '<%s>%s</%s>';
$awesome = sprintf( $pattern, $wrap, $text, $wrap );
echo apply_filters( 'filter_ridiculous_text', $awesome, $wrap, $text );
do_action( 'after_ridiculous_widget' );
echo $after_widget;
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Ridiculous Awesome Widget', 'text_domain' );
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
} // class Ridiculous_Awesome_Widget
add_action( 'widgets_init', create_function( '', 'register_widget( "ridiculous_awesome_widget" );' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.