Skip to content

Instantly share code, notes, and snippets.

@reatlat
Last active April 12, 2019 15:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reatlat/17989f05ace63ac32859f1b629e6d6a7 to your computer and use it in GitHub Desktop.
Save reatlat/17989f05ace63ac32859f1b629e6d6a7 to your computer and use it in GitHub Desktop.
Example of shortcode plugin

How to install

  1. Upload reatlat-shortcode-plugin.php file to the /wp-content/plugins/ directory
  2. Activate the plugin through the ‘Plugins’ menu in WordPress
  3. Admire your new brand shortcode plugin 😎

How to use

  1. Paste the shortcode [bartag foo="Some new Foo variable"] to content editor.
  2. Be sure, the shotcode plugin activated.
  3. Enjoy result.
<?php
/*
Plugin Name: reatlat Shortcode plugin
Plugin URI: https://reatlat.net
Description: A Wordpress example shortcode plugin file.
Version: 1.0.0
Author: Alex Zappa
Author URI: https://reatlat.net
License: GPL
License URI: http://www.gnu.org/licenses/gpl-3.0.txt
Copyright: Alex Zappa
Text Domain: reatlat-shortcode-plugin
*/
define('REATLAT_SHORTCODE_PLUGIN_ROOT', plugin_dir_url(__FILE__));
// check if out function exist, not needed if we override some some function
if (function_exists('initReatlatShortcodePluginInit')) {
function initReatlatShortcodePluginInit()
{
function bartag_func($atts)
{
$atts = shortcode_atts(array(
// set default $atts if user use just simple shortcode [bartag]
'foo' => 'no foo',
'baz' => 'default baz'
), $atts, 'bartag');
ob_start();
?>
<div class="wrapper">
<div class="wprapper__inner">
<p>Foo => <?php echo $atts['foo']; ?></p>
<p>Baz => <?php echo $atts['baz']; ?></p>
</div>
</div>
<?php
$content = ob_get_contents();
ob_end_clean();
return $content;
}
add_shortcode('bartag', 'bartag_func');
}
initReatlatShortcodePluginInit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment