Last active
August 10, 2022 15:53
-
-
Save grand-digital/52456ae6a9534a5e8526e21352c5112b to your computer and use it in GitHub Desktop.
Examples for creating a shortcode in WordPress tags: #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 | |
// Examples for creating shortcodes in WordPress | |
/** | |
* EXAMPLE 1 | |
* | |
* Self-closing shortcode without attributes | |
* | |
* Use in WordPress: | |
* [rename_me_to_something_else_selfclosing_without_attributes] | |
*/ | |
function rename_me_to_something_else_selfclosing_without_attributes() { | |
// ------------------ | |
// EXAMPLE CODE | |
// ------------------ | |
$output = '<h1>Shortcode Example</h1>'; | |
$output .= '<p>Hello World!</p>'; | |
return $output; | |
// ------------------ | |
// END EXAMPLE CODE | |
// ------------------ | |
} | |
add_shortcode( 'rename_me_to_something_else_selfclosing_without_attributes', 'rename_me_to_something_else_selfclosing_without_attributes' ); | |
/** | |
* EXAMPLE 2 | |
* | |
* Self-closing shortcode with attributes | |
* | |
* Use in WordPress: | |
* [rename_me_to_something_else_selfclosing_with_attributes foo="just" bar="an" baz="example"] | |
*/ | |
function rename_me_to_something_else_selfclosing_with_attributes( $atts ) { | |
// read attributes | |
$atts = shortcode_atts( | |
array( | |
'foo' => 'these are', | |
'bar' => 'example', | |
'baz' => 'attributes', | |
), | |
$atts, | |
'rename_me_to_something_else_selfclosing_with_attributes' | |
); | |
// ------------------ | |
// EXAMPLE CODE | |
// ------------------ | |
// GET A PREFORMATTED DUMP OF THE SHORTCODE ATTRIBUTES ARRAY: | |
$output = '<h1>Shortcode Attributes</h1>'; | |
ob_start(); | |
echo '<pre>'; | |
print_r( $atts ); | |
echo '</pre>'; | |
$output .= ob_get_clean(); | |
return $output; | |
// ------------------ | |
// END EXAMPLE CODE | |
// ------------------ | |
} | |
add_shortcode( 'rename_me_to_something_else_selfclosing_with_attributes', 'rename_me_to_something_else_selfclosing_with_attributes' ); | |
/** | |
* EXAMPLE 3 | |
* | |
* Enclosing shortcode without attributes | |
* | |
* Use in WordPress: | |
* [rename_me_to_something_else_enclosing_without_attributes] | |
* <p>Example content between enclosing shortcode.</p> | |
* [/rename_me_to_something_else_enclosing_without_attributes] | |
*/ | |
function rename_me_to_something_else_enclosing_without_attributes( $atts , $content = null ) { | |
// ------------------ | |
// EXAMPLE CODE | |
// ------------------ | |
$output = '<h1>Enclosed Content</h1>'; | |
$output .= $content !== null ? $content : 'NULL'; | |
return $output; | |
// ------------------ | |
// END EXAMPLE CODE | |
// ------------------ | |
} | |
add_shortcode( 'rename_me_to_something_else_enclosing_without_attributes', 'rename_me_to_something_else_enclosing_without_attributes' ); | |
/** | |
* EXAMPLE 4 | |
* | |
* Enclosing shortcode with attributes | |
* | |
* Use in WordPress: | |
* [rename_me_to_something_else_enclosing_with_attributes foo="just" bar="an" baz="example"] | |
* <p>Example content between enclosing shortcode.</p> | |
* [/rename_me_to_something_else_enclosing_with_attributes] | |
*/ | |
function rename_me_to_something_else_enclosing_with_attributes( $atts , $content = null ) { | |
// read attributes | |
$atts = shortcode_atts( | |
array( | |
'foo' => 'these are', | |
'bar' => 'example', | |
'baz' => 'attributes', | |
), | |
$atts, | |
'rename_me_to_something_else_enclosing_with_attributes' | |
); | |
// ------------------ | |
// EXAMPLE CODE | |
// ------------------ | |
// GET A PREFORMATTED DUMP OF THE SHORTCODE ATTRIBUTES ARRAY: | |
$output = '<h1>Shortcode Attributes</h1>'; | |
ob_start(); | |
echo '<pre>'; | |
print_r( $atts ); | |
echo '</pre>'; | |
$output .= ob_get_clean(); | |
// GET THE ENCLOSED CODE: | |
$output .= '<h1>Enclosed Content</h1>'; | |
$output .= $content !== null ? $content : 'NULL'; | |
// RETURN EVERYTHING: | |
return $output; | |
// ------------------ | |
// END EXAMPLE CODE | |
// ------------------ | |
} | |
add_shortcode( 'rename_me_to_something_else_enclosing_with_attributes', 'rename_me_to_something_else_enclosing_with_attributes' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment