Skip to content

jenstornell/php-shortcode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Shortcode

Version 1.0

Features

  • Just a single file
  • No dependencies
  • Very easy to use
  • Shortcodes supports single, double and literal quotes

Summary

The code below is a fully working example.

include __DIR__ . '/shortcode.php';

shortcode::add('button', function($args) {
  return '<a href="' . $args->url . '">' . $args->title . '</a>';
});

echo shortcode::filter('<p>[button title="About" url="about"]</p>');

Setup

Include the shortcode.php file. Just make sure the path is correct.

include __DIR__ . '/shortcode.php';

HTML

A shortcode starts with [ and ends with ]. The whole shortcode will then be replaced with the output from your custom method.

Without arguments

[button]

Arguments and double quotes

Single quotes can be used inside the double quotes.

[button title="It's a button" url="about"]

Arguments and single quotes

Double quotes can be used inside single quotes.

[button title='My "button"' url='about']

Arguments and template literals

Both single and double quotes can be used inside template literals.

[button title=`It's my "button"` url='about']

Custom method

With a custom method, you will replace the shortcode with your custom method output.

  • name - The name of the shortcode [button title="button"]
  • method - The output of the method will replace the shortcode.
  • args (optional) - Arguments sent from the shortcode.
  • types (optional) - The type of quotes that was used for each argument.

Without arguments

shortcode::add('button', function() {
  return 'Hello!';
});

With arguments

shortcode::add('button', function($args) {
  return '<a href="' . $args->url . '">' . $args->title . '</a>';
});

With types

Your shortcode quotes may vary from time to time.

  • [quote content='"I am a quote"']
  • [quote content="'I am a quote'"]

If you are not always using the same type of quotes, you may need to use the second optional parameter $quotes.

shortcode::add('quote', function($args, $quotes) {
  return sprintf('
    <div data-content=%s%s%s></div>',
    $quotes->content,
    $args->content,
    $quotes->content,
  );
});

Filter html

All shortcodes will be replaced with the output from your custom method.

$html = '<p>[button]</p>';
echo shortcode::filter($html);

Unset shortcode(s)

When using shortcode::add() or $shortcode->add(), your custom method will be added to a global variable for later usage.

With unset() and unsetAll(), you can remove one or remove all of them.

unset

Remove a single custom method from memory by shortcode name.

shortcode::unset('button');

unsetAll

Remove all custom methods from memory.

shortcode::unsetAll();

Alternative usage - Instantiated method

With all methods you can use an instantiated class instead if you prefer that approach.

$shortcode = new PHPShortcode();
echo $shortcode->filter('[button]');

A note about quotes

To prevent breaking HTML, it can sometimes be good to convert quotes to html characters.

$some_argument = htmlspecialchars($args->some_argument, ENT_QUOTES);

Todo

  • Support for usage without quotes, mainly for integers.

Limitations

  • You can't place content within a start and closing shortcode like [hello]world[/hello]... yet.
  • You can't use line breaks inside shortcodes. However, it's fine to print out line breaks from the custom shortcode methods.

Bug report

Can you break it? If you break it in an unexpected way, please report it as a bug to us. Provide a code example.

Inspiration

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published