How to create a WordPress shortcode plugin?
It is pretty easy to roll out a WordPress plugin that adds a shortcode.
Creating a WordPress shortcode plugin
Here are the basic steps:
- Create a new file called MyPlugin.php
- Add this code:
<?php /* Plugin Name: <Your Plugin Name> Version: 1.0 Plugin URI: tba Description: Author: <your name> Author URI: <your web site> */ function handleShortcode( $atts, $content ) { return "Hello, World!"; } $test = add_shortcode( 'my-shortcode', 'handleShortcode' ); ?> - Upload (or copy) MyPlugin.php to the /wp-content/plugins/ directory in your WordPress install.
Using a WordPress shortcode plugin
- Start a new Post
- type in the following:
[my-shortcode]
- Click Preview.
Your post should have replaced your shortcode with “Hello, Word!”.
A better WordPress shortcode plugin template
While the above is all you need, a more scalable solution might involve using classes. Here is a template that uses classes.
<?php
/*
Plugin Name: <Your Plugin Name>
Version: 1.0
Plugin URI: tba
Description:
Author: <your name>
Author URI: <your web site>
*/
// A class to manage your plugin
class MyPlugin {
public function MyPlugin( $shortCodeHandler ) {
$result = add_shortcode( 'my-shortcode', array( $shortCodeHandler, 'handleShortcode' ) );
}
}
// A class to handle your shortcode
class ShortCodeHandler {
public function handleShortcode( $atts, $content ) {
return "Hello, World";
}
}
$shortCodeHandler = new ShortCodeHandler();
$plugin = new MyPlugin( $shortCodeHandler );
?>


www.hermagazinehotsprings.com
How to create a WordPress shortcode plugin? | Rhyous