Skip to content

Instantly share code, notes, and snippets.

@stas
Created October 5, 2012 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stas/3839694 to your computer and use it in GitHub Desktop.
Save stas/3839694 to your computer and use it in GitHub Desktop.
Hook git repo version into WordPress <head>.
<?php
/*
Plugin Name: Git Version
Plugin URI: http://wordpress.org/extend/plugins/git-version/
Description: Add a git version meta to HEAD
Version: 0.1
Author: sushkov
Author URI: http://wordpress.org/extend/plugins/git-version/
*/
?>
<?php
/**
* Hook current Git version data into <head>
*/
class GitVersion {
/**
* Static constructor
*/
function init() {
add_action( 'wp_head', array( __CLASS__, 'read_git_head' ) );
}
/**
* Reads git HEAD if available
*/
function read_git_head() {
$head = ABSPATH . '.git/HEAD';
$ref = null;
if ( file_exists( $head ) ) {
$git_ref = ABSPATH . str_replace( 'ref: ', '.git/', file_get_contents( $head ) );
$ref = file_get_contents( trim( $git_ref ) );
}
if ( $ref = trim( $ref ) ) {
printf( '<meta name="version" content="%s" />' . "\n", substr( $ref, 0, 6 ) );
}
}
}
GitVersion::init();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment