wp-content » themes » functions.php » phpinfo() WordPress Shortcode

phpinfo() WordPress Shortcode

Here is an example of a simple WordPress plugin that uses the [phpinfo] shortcode to display PHP information similar to what the phpinfo() function does:

Plugin Name: PHP Info Shortcode
Description: Displays PHP information using the [phpinfo] shortcode.

function phpinfo_shortcode_handler( $atts ) {
  // Build the HTML for the data
  $html = '<div>';
  $html .= '<h3>PHP Information</h3>';
  $html .= '<table>';
  $html .= '<tr>';
  $html .= '<th>Name</th>';
  $html .= '<th>Value</th>';
  $html .= '</tr>';
  foreach ( get_defined_constants( true )['Core'] as $name => $value ) {
    $html .= '<tr>';
    $html .= '<td>' . $name . '</td>';
    $html .= '<td>' . $value . '</td>';
    $html .= '</tr>';
  $html .= '</table>';
  $html .= '</div>';

  // Return the HTML
  return $html;

add_shortcode( 'phpinfo', 'phpinfo_shortcode_handler' );

This plugin defines a shortcode called [phpinfo] that displays a table of PHP constants and their values.

image 5 - phpinfo() WordPress Shortcode

When you include the [phpinfo] shortcode in a post or page, the plugin will execute the phpinfo_shortcode_handler() function to generate the HTML for the table of information.

chrome wp4fw4752T - phpinfo() WordPress Shortcode
phpinfo() WordPress Shortcode

To further customize the plugin you can add or remove constants from the table, or display different types of PHP information using other functions such as phpversion() or php_uname().

Was this post helpful?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.