Last active
April 13, 2018 15:05
-
-
Save dnaber-de/4690246 to your computer and use it in GitHub Desktop.
WordPress plugin to show the current used template file name in the admin bar.
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 | |
/** | |
* Plugin Name: dna Template Name to Admin Bar | |
* Plugin URI: https://gist.github.com/4690246 | |
* Description: Shows the current used theme template file in the admin bar. (Requires PHP 5.3) | |
* Author: David Naber | |
* Author URI: http://dnaber.de/ | |
* Version: 2013.11.09 | |
* License: MIT | |
* License URI: http://opensource.org/licenses/mit-license.php | |
*/ | |
namespace dna\tpl_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' ), 999 ); | |
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 a item 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' => 'dna-tpl-name', | |
'title' => $this->template_basename, | |
'href' => '#', | |
'meta' => array( | |
'title' => $this->template | |
) | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment