Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active January 29, 2016 14:35
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 glueckpress/b720341600e13d31d4f5 to your computer and use it in GitHub Desktop.
Save glueckpress/b720341600e13d31d4f5 to your computer and use it in GitHub Desktop.
[WordPress] Sample plugin demonstrating how to retrieve data like plugin name, description, version etc. from the plugin file header and use retrieved data in the front-end.
<?php
defined( 'ABSPATH' ) or die( 'Poop.' );
/**
* Plugin Name: __Retrieve Plugin Headers (Sample Plugin)
* Description: Retrieves data like this description, or version from the plugin file header.
* Version: 1.2.3
* Plugin URI: https://gist.github.com/glueckpress/b720341600e13d31d4f5
* Author: Caspar Hübinger
* Author URI: https://profiles.wordpress.org/glueckpress/
* License: GPL3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* Retrieve data from file header.
* @return void
*/
function sample_plugin__retrieve_file_headers() {
// List of headers to retrieve.
$headers = array(
// my key => file header
'name' => 'Plugin Name',
'desc' => 'Description',
'version' => 'Version',
'plugin_uri' => 'Plugin URI',
'author' => 'Author',
'author_uri' => 'Author URI',
);
// Beacause get_plugin_data() will only work in wp-admin.
$file_data = get_file_data( __FILE__, $headers );
// Sample output for the front-end.
$dump = '<h4>Retrieving Plugin File Headers</h4>';
$dump .= '<table border="0" cellpadding="10" cellspacing="0"><tbody>';
$dump .= sprintf(
"<tr><td>Plugin&#160;Name:</td><td>%s</td></tr>\n",
$file_data['name']
);
$dump .= sprintf(
"<tr><td>Plugin&#160;Version:</td><td>%s</td></tr\n",
$file_data['version']
);
$dump .= sprintf(
"<tr><td>Plugin&#160;URI:</td><td><a href='%1$s'>%1$s</a></td></tr\n",
$file_data['plugin_uri']
);
$dump .= sprintf(
"<tr><td>Author:</td><td>%s</td></tr\n",
$file_data['author']
);
$dump .= sprintf(
"<tr><td>Author&#160;URI:</td><td><a href='%1$s'>%1$s</a></td></tr\n",
$file_data['author_uri']
);
$dump .= sprintf(
"<tr><td>Description:</td><td>%s</td></tr\n",
$file_data['desc']
);
$dump .= '</tbody></table>';
if ( ! is_admin() )
wp_die( $dump, "Sample Output via {$file_data['name']}" );
}
add_action( 'init', 'sample_plugin__retrieve_file_headers' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment