Skip to content

Instantly share code, notes, and snippets.

@jlroettger
Last active June 8, 2016 00:13
Show Gist options
  • Save jlroettger/1a83064d6cf756c857d5805f880d1fab to your computer and use it in GitHub Desktop.
Save jlroettger/1a83064d6cf756c857d5805f880d1fab to your computer and use it in GitHub Desktop.
roots-template-name-in-admin-bar.php
<?php
/**
* Plugin Name: Roots Template Name in Admin Bar
* Description: Shows the name of the current page's template file in the admin bar
*/
namespace roots_tmplt_to_admin_bar;
if ( ! function_exists( 'add_filter' ) )
return;
add_action( 'plugins_loaded', array( Plugin::get_instance(), 'init_plugin' ) );
class Plugin {
/**
* the plugin instance
*
* @var Plugin
*/
private static $instance = NULL;
/**
* the template full path
*
* @var string
*/
protected $template;
/**
* the template basename
*
* @since 2013.11.09
* @var string
*/
protected $template_basename;
/**
* get the plugin instance
*
* @return Plugin
*/
public static function get_instance() {
if ( NULL === self::$instance )
self::$instance = new self;
return self::$instance;
}
/**
* hook in
*
* @wp-hook plugins_loaded
*/
public function init_plugin() {
if ( is_admin() )
return;
add_filter( 'template_include', array( $this, 'pick_template' ), 98 ); //Changed from 999 to run pre-Roots wrap
add_action( 'wp_before_admin_bar_render', array( $this, 'admin_bar' ) );
}
/**
* fishing the template name out
*
* @wp-hook template_include
* @param string $template
* @return string
*/
public function pick_template( $template ) {
$this->template = $template;
$this->template_basename = basename( $template );
return $template;
}
/**
* add name to the admin bar
*
* @wp-hook wp_before_admin_bar_render
* @global $wp_admin_bar;
* @return
*/
public function admin_bar() {
$GLOBALS[ 'wp_admin_bar' ]->add_menu(
array(
'id' => 'roots-tmplt-name',
'title' => $this->template_basename,
'meta' => array(
'title' => $this->template
)
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment