-
-
Save tommcfarlin/b264ef7e0af29b42651bf45ba50c64b2 to your computer and use it in GitHub Desktop.
[WordPress] Using Custom Filters with Shortcodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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.'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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