Skip to content

Instantly share code, notes, and snippets.

@norcross
Forked from joelgoodman/widget.php
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norcross/df38fb7798b2e2a0746f to your computer and use it in GitHub Desktop.
Save norcross/df38fb7798b2e2a0746f to your computer and use it in GitHub Desktop.
<?php
/**
* ITS Related Links
*
* The WordPress Widget Boilerplate is an organized, maintainable boilerplate for building widgets using WordPress best practices.
*
*/
class ITS_Contact_Box extends WP_Widget {
/**
* The variable name is used as the text domain when internationalizing strings
* of text. Its value should match the Text Domain file header in the main
* widget file.
*
* @since 1.0.0
* @var string
*/
protected $widget_slug = 'its-contact-box';
/*--------------------------------------------------*/
/* Constructor
/*--------------------------------------------------*/
/**
* Specifies the classname and description, instantiates the widget,
* loads localization files, and includes necessary stylesheets and JavaScript.
*/
public function __construct() {
// load plugin text domain
// TODO rename this function from 'widget' to something else or
// remove completely. only used for localization.
// add_action( 'init', array( $this, 'widget' ) );
// TODO: update description
parent::__construct(
$this->get_widget_slug(),
__( 'ITS Contact Box', $this->get_widget_slug() ),
array(
'classname' => $this->get_widget_slug().'-container',
'description' => __( 'Displays the ITS Contact box. Add details on the Options panel.', $this->get_widget_slug() )
)
);
// Refreshing the widget's cached output with each new post
add_action( 'save_post', array( $this, 'flush_widget_cache' ) );
add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) );
add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) );
} // end constructor
/**
* Return the widget slug.
*
* @since 1.0.0
*
* @return Plugin slug variable.
*/
public function get_widget_slug() {
return $this->widget_slug;
}
/*--------------------------------------------------*/
/* Widget API Functions
/*--------------------------------------------------*/
/**
* Outputs the content of the widget.
*
* @param array args The array of form elements
* @param array instance The current instance of the widget
*/
public function widget( $args, $instance ) {
// go on with your widget logic, put everything into a string and …
extract( $args, EXTR_SKIP );
$title = apply_filters( 'widget_title', $instance['title'] );
echo $before_widget;
if ( !empty($title) ) {
echo $before_title . $title . $after_title;
}
$contact_output = '<p><span class="its-phone">'. get_field("phone_number", "option") .'</span><br />
<a href="mailto:'.get_field("help_email", "option").'" class="its-email">'.get_field("help_email", "option").'</a></p>';
echo $contact_output;
echo $after_widget;
} // end widget
public function flush_widget_cache()
{
wp_cache_delete( $this->get_widget_slug(), 'widget' );
}
/**
* Processes the widget's options to be saved.
*
* @param array new_instance The new instance of values to be generated via the update.
* @param array old_instance The previous instance of values before the update.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
// TODO: Here is where you update your widget's old values with the new, incoming values
$instance['title'] = strip_tags( stripslashes( $new_instance['title'] ) );
return $instance;
} // end widget
/**
* Generates the administration form for the widget.
*
* @param array instance The array of keys and values for the widget.
*/
public function form( $instance ) {
$defaults = array('title' => 'Contact ITS');
$instance = wp_parse_args(
(array) $instance
);
// TODO: Store the values of the widget in their own variable
$title = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
// Display the admin form
include( plugin_dir_path(__FILE__) . 'views/admin.php' );
} // end form
} // end class
add_action( 'widgets_init', create_function( '', 'register_widget("ITS_Contact_Box");' ) );
?>
@norcross
Copy link
Author

a few initial issues:

line 34: the init function called 'widget' is clashing with the normal widget class functions. it's used for localization, so either rename it (and add the appropriate function) or remove it completely

line 125: you were calling the save / escape function in the 'form' as as well as the 'update' function. only needed in the 'update' function in that manner.

@joelgoodman
Copy link

Ah, yeah. I get that. Thanks for the look and help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment