Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created April 16, 2019 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/b264ef7e0af29b42651bf45ba50c64b2 to your computer and use it in GitHub Desktop.
Save tommcfarlin/b264ef7e0af29b42651bf45ba50c64b2 to your computer and use it in GitHub Desktop.
[WordPress] Using Custom Filters with Shortcodes
<?php
namespace Acme;
add_shortcode('a_simple_example', __NAMESPACE__ . '\\aSimpleExample');
/**
* Replaces [a_simple_example] short code with the text "This is the result of an example shortcode."
* whenever the shortcode is placed in a post or page.
*/
function aSimpleExample()
{
return 'This is the result of an example shortcode.';
}
<?php
namespace Acme;
add_filter('custom_shortcode_example', __NAMESPACE__ . '\\aSimpleShortcodeFilter');
/**
* Replaces [a simple example] short code with the text "This is the result of an example shortcode."
* whenever the shortcode is placed in a post or page.
*
* @param string $input the input to filter.
*
* @return string the result of the filter running on the input
*/
function aSimpleShortcodeFilter(string $input): string
{
return str_ireplace(
'an example shortcode.',
'a filtered shortcode',
$input
);
}
<?php
namespace Acme;
add_shortcode('a_simple_example', __NAMESPACE__ . '\\aSimpleExample');
/**
* Replaces [a_simple_example] short code with the text "This is the result of an example shortcode."
* whenever the shortcode is placed in a post or page.
*/
function aSimpleExample()
{
return apply_filters('custom_shortcode_example', 'This is the result of an example shortcode.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment