Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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
You can’t perform that action at this time.