1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

The function get_plugin_data

The function get_plugin_data can be used to grab information in the comment block of a plugin bootstrap file if the user browser points to the administration area of WordPress.
Categories:
  1. big-data.jpg

    The top of all bootstrap files for WordPress plugins begin with an important comment block. Below is a sample of a multiline comment block which describes the plugin and provides name, URI, Author, Author URI, and Version.

    Create a file named fabulous.php and place it in the wp-content/plugins directory of WordPress.

    PHP:
    1. <?php
    2.  
    3. /**
    4. * Plugin Name: Fabulous
    5. * Plugin URI: www.tuxreports.com
    6. * Description: A test Plugin
    7. * Author: LPH
    8. * Author URI: www.tuxreports.com
    9. * Version: 1.0.0
    10. */
    It turns out the function get_plugin_data can be used to grab the information in the comments of a bootstrap file if logged into the administration area of WordPress. For example, below is a sample of code to pull the array of information within the $data variable.

    PHP:
    1. function admin_notice() {
    2.  
    3.   if ( is_admin() ) {
    4.     $data = get_plugin_data(__FILE__);
    5.     var_dump($data);
    6.   }
    7. }
    8.  
    9. add_action( 'admin_notices', 'admin_notice' );
    Code (Text):
    1. array (size=11)
    2.   'Name' => string 'Fabulous' (length=8)
    3.   'PluginURI' => string 'http://www.tuxreports.com' (length=25)
    4.   'Version' => string '1.0.0' (length=5)
    5.   'Description' => string 'A test Plugin <cite>By <a href="http://www.tuxreports.com">LPH</a>.</cite>' (length=74)
    6.   'Author' => string '<a href="http://www.tuxreports.com">LPH</a>' (length=43)
    7.   'AuthorURI' => string 'http://www.tuxreports.com' (length=25)
    8.   'TextDomain' => string '' (length=0)
    9.   'DomainPath' => string '' (length=0)
    10.   'Network' => boolean false
    11.   'Title' => string '<a href="http://www.tuxreports.com">Fabulous</a>' (length=48)
    12.   'AuthorName' => string 'LPH' (length=3)
    This means $data['Version'] would return the string '1.0.0' -- something which might be very useful.

Comments

To make a comment simply sign up and become a member!