Skip to content

Instantly share code, notes, and snippets.

@ejntaylor
Last active December 28, 2015 21:49
Show Gist options
  • Select an option

  • Save ejntaylor/7567084 to your computer and use it in GitHub Desktop.

Select an option

Save ejntaylor/7567084 to your computer and use it in GitHub Desktop.
Shortcode to insert PHP & Create a Widget
// shortcode - features
// shortcode implementation
function sc_features($atts) {
// turn on output buffering to capture script output
ob_start();
// include file (contents will get saved in output buffer)
get_template_part('includes/features');
// save and return the content that has been output
$content = ob_get_clean();
return $content;
}
//register the Shortcode handler
add_shortcode('features', 'sc_features');
// widget - include
class NameOfWidget extends WP_Widget
{
function NameOfWidget()
{
$widget_ops = array('classname' => 'NameOfWidget', 'description' => 'Displays a ....' );
$this->WP_Widget('NameOfWidget', 'Title goes here', $widget_ops);
}
function form($instance)
{
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = $instance['title'];
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
<?php
}
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
return $instance;
}
function widget($args, $instance)
{
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
if (!empty($title))
echo $before_title . $title . $after_title;;
// widget code here
get_template_part( 'includes/widget/name' );
echo $after_widget;
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("NameOfWidget");') );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment