Last active
January 29, 2016 14:35
-
-
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.
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 | |
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 Name:</td><td>%s</td></tr>\n", | |
$file_data['name'] | |
); | |
$dump .= sprintf( | |
"<tr><td>Plugin Version:</td><td>%s</td></tr\n", | |
$file_data['version'] | |
); | |
$dump .= sprintf( | |
"<tr><td>Plugin 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 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