Skip to content

Instantly share code, notes, and snippets.

@bekarice
Last active February 4, 2019 22:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bekarice/63a0196ef010d0e30407 to your computer and use it in GitHub Desktop.
Save bekarice/63a0196ef010d0e30407 to your computer and use it in GitHub Desktop.
Storefront Post Meta Widget: moves post meta into a widget and displays full-width blog posts
<?php
/**
* Plugin Name: Storefront Post Meta Widget
* Plugin URI: https://www.skyverge.com/blog/create-smart-woocommerce-widget/
* Description: Adds a widget to display blog post meta in the Storefront theme
* Author: SkyVerge
* Author URI: https://www.skyverge.com/
* Version: 1.0.0
* Text Domain: storefront-post-meta
*
* Copyright: (c) 2015 SkyVerge, Inc. (info@skyverge.com)
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @author SkyVerge
* @copyright Copyright (c) 2015, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
// Bail if Storefront isn't the active or parent theme
if ( 'storefront' !== wp_get_theme()->template ) {
return;
}
/**
* A widget for displaying post meta in the Storefront theme
*
* @since 1.0.0
* @extends \WP_Widget
*/
class Storefront_Post_Meta_Widget extends WP_Widget {
/**
* Setup the widget options
*
* @since 1.0.0
*/
public function __construct() {
// modify the existing posts to remove the post meta / make them full width
add_action( 'wp', array( $this, 'remove_storefront_postmeta' ) );
add_action( 'wp_print_footer_scripts', array( $this, 'modify_storefront_blog_posts' ) );
// set widget options
$options = array(
'classname' => 'widget_storefront_post_meta', // CSS class name
'description' => __( 'Displays post meta on single blog posts with Storefront.', 'storefront-post-meta' ),
);
// instantiate the widget
parent::__construct( 'Storefront_Post_Meta_Widget', __( 'Storefront Post Meta', 'storefront-post-meta' ), $options );
}
/**
* Render the widget
*
* @since 1.0.0
* @see WP_Widget::widget()
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
// Only show this if we're looking at a blog post
if ( ! is_singular( 'post' ) ) {
return;
}
// get the widget configuration
$title = $instance['title'];
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . wp_kses_post( $title ) . $args['after_title'];
}
// Show the storefront post meta
storefront_post_meta();
echo $args['after_widget'];
}
/**
* Update the widget title & selected product
*
* @since 1.0.0
* @see WP_Widget::update()
* @param array $new_instance new widget settings
* @param array $old_instance old widget settings
* @return array updated widget settings
*/
public function update( $new_instance, $old_instance ) {
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
/**
* Render the admin form for the widget
*
* @since 1.0.0
* @see WP_Widget::form()
* @param array $instance the widget settings
* @return string|void
*/
public function form( $instance ) {
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'storefront-post-meta' ) ?>:</label>
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( isset( $instance['title'] ) ? $instance['title'] : '' ); ?>" />
</p>
<?php
}
/**
* Removes the post meta from its current location at the left of the post
*
* @since 1.0.0
*/
public function remove_storefront_postmeta() {
if ( is_singular( 'post' ) && is_active_widget( false, false, $this->id_base ) ) {
remove_action( 'storefront_single_post', 'storefront_post_meta', 20 );
}
}
/**
* Modifies singular blog post styles since post meta is removed
*
* @since 1.0.0
*/
public function modify_storefront_blog_posts() {
if ( is_singular( 'post' ) && is_active_widget( false, false, $this->id_base ) ) {
echo '<style>
.hentry.type-post .entry-content { width: 100%; }
.hentry { padding-bottom: 2em; margin-bottom: 2em; }
</style>';
}
}
} // end \Storefront_Post_Meta_Widget class
/**
* Registers the new widget to add it to the available widgets
*
* @since 1.0.0
*/
function storefront_post_meta_register_widget() {
register_widget( 'Storefront_Post_Meta_Widget' );
}
add_action( 'widgets_init', 'storefront_post_meta_register_widget' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment