Created
June 16, 2014 13:27
-
-
Save joelgoodman/707555766a9716f34b88 to your computer and use it in GitHub Desktop.
WordPress Widget for ACF Fields
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 | |
/** | |
* 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 | |
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 = strip_tags( stripslashes( $new_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");' ) ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Anything added between lines 80 - 84 gets inserted properly into the WordPress widgetized sidebar, but also prints out, from what I can tell, before the
<html>
.