wp-content » themes » functions.php » Building a simple WordPress shortcode to output Website information

Building a simple WordPress shortcode to output Website information

Here is a simple WordPress plugin that uses the [site] shortcode to display the website name:

<?php
function info_shortcode_handler( $atts ) {
 $html .= get_bloginfo( 'name' );

 // Return the HTML
  return $html;
}

add_shortcode( 'site', 'site_shortcode_handler' );

It uses the get_bloginfo() function to retrieve website information, and do_shortcode() function to display the information in the WordPress post or page.

We can use get_bloginfo() function to also display additional information such as URL, WordPress version, etc.

Here is another shortcode [info] that will display basic website information:

<?php
/*
Plugin Name: WordPress Site Info Display
Description: Displays publicly available information about a WordPress website.
*/

function info_shortcode_handler( $atts ) {
  // Build the HTML for the data
  $html = '<div>';
  $html .= '<h3>WordPress Site Info</h3>';
  $html .= '<ul>';
  $html .= '<li>Name: ' . get_bloginfo( 'name' ) . '</li>';
  $html .= '<li>Version: ' . get_bloginfo( 'version' ) . '</li>';
  $html .= '<li>URL: ' . get_bloginfo( 'url' ) . '</li>';
  $html .= '</ul>';
  $html .= '<h3>Server Info</h3>';
  $html .= '<ul>';
  $html .= '<li>Software: ' . $_SERVER['SERVER_SOFTWARE'] . '</li>';
  $html .= '<li>IP Address: ' . $_SERVER['SERVER_ADDR'] . '</li>';
  $html .= '<li>Current Script: ' . $_SERVER['SCRIPT_NAME'] . '</li>';
  $html .= '</ul>';
  $html .= '</div>';

  // Return the HTML
  return $html;
}

add_shortcode( 'info', 'info_shortcode_handler' );

Shortcode

image 2 - Building a simple WordPress shortcode to output Website information

Output:

image 3 - Building a simple WordPress shortcode to output Website information

We can further expand the shortcode to publically display simillar information as the Site Health page does:

<?php
/*
Plugin Name: WordPress Site Health Info Display
Description: Displays information about the WordPress website and server, similar to the Site Health page.
*/

function info_shortcode_handler( $atts ) {
  // Build the HTML for the data
  $html = '<div>';
  $html .= '<h3>WordPress Site Health Info</h3>';
  $html .= '<h4>WordPress Site Info</h4>';
  $html .= '<ul>';
  $html .= '<li>Name: ' . get_bloginfo( 'name' ) . '</li>';
  $html .= '<li>Version: ' . get_bloginfo( 'version' ) . '</li>';
  $html .= '<li>URL: ' . get_bloginfo( 'url' ) . '</li>';
  $html .= '</ul>';
  $html .= '<h4>Server Info</h4>';
  $html .= '<ul>';
  $html .= '<li>Software: ' . $_SERVER['SERVER_SOFTWARE'] . '</li>';
  $html .= '<li>IP Address: ' . $_SERVER['SERVER_ADDR'] . '</li>';
  $html .= '<li>Current Script: ' . $_SERVER['SCRIPT_NAME'] . '</li>';
  $html .= '</ul>';
  $html .= '<h4>WordPress Configuration</h4>';
  $html .= '<ul>';
  $html .= '<li>Multisite: ' . ( is_multisite() ? 'Yes' : 'No' ) . '</li>';
  $html .= '<li>Memory Limit: ' . WP_MEMORY_LIMIT . '</li>';
  $html .= '<li>Debug Mode: ' . ( WP_DEBUG ? 'Yes' : 'No' ) . '</li>';
  $html .= '</ul>';
  $html .= '<h4>WordPress Directories</h4>';
  $html .= '<ul>';
  $html .= '<li>ABSPATH: ' . ABSPATH . '</li>';
  $html .= '<li>WP_CONTENT_DIR: ' . WP_CONTENT_DIR . '</li>';
  $html .= '<li>WP_PLUGIN_DIR: ' . WP_PLUGIN_DIR . '</li>';
  $html .= '<li>WP_LANG_DIR: ' . WP_LANG_DIR . '</li>';
  $html .= '</ul>';
  $html .= '<h4>WordPress URLs</h4>';
  $html .= '<ul>';
  $html .= '<li>WP_CONTENT_URL: ' . WP_CONTENT_URL . '</li>';
  $html .= '<li>WP_PLUGIN_URL: ' . WP_PLUGIN_URL . '</li>';
  $html .= '<li>WP_LANG_URL: ' . WP_LANG_URL . '</li>';
  $html .= '</ul>';
  $html .= '<h4>WordPress Environment</h4>';
  $html .= '<ul>';
  $html .= '<li>Type: ' . wp_get_environment_type() . '</li>';
  $html .= '</ul>';
  $html .= '</div>';

  // Return the HTML
  return $html;
}

add_shortcode( 'info', 'info_shortcode_handler' );
image 4 760x1024 - Building a simple WordPress shortcode to output Website information

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.

Recommended