Skip to content

Instantly share code, notes, and snippets.

@nextab
Last active August 20, 2021 10:29
Show Gist options
  • Save nextab/1c764bf5f501e339c5d7340981bc5402 to your computer and use it in GitHub Desktop.
Save nextab/1c764bf5f501e339c5d7340981bc5402 to your computer and use it in GitHub Desktop.
Wir liefern einige einfache Vorlagen für Shortcodes in WordPress.
/* Einfaches Beispiel, für einen simplen Shortcode, der das Jahr (4-stellig) ausgibt.
Anwendung:
[jahr]
*/
function jahr_function( $atts ) {
return date('Y');
}
add_shortcode( 'jahr', 'jahr_function' );
/* Etwas komplexerer Shortcode [highlight], der ein "div" um den $content (steht zwischen dem öffnenden und dem schließenden Shortcode-Tag) packt und diesem eine Klasse zuweist, die übergeben werden kann und mit dem Standardwert "highlight" versehen ist.
Anwendung:
[highlight]Dies ist mein hervorgehobener Text[/highlight]
*/
function highlight_shortcode( $atts, $content = null ) {
$a = shortcode_atts( [
'class' => 'highlight',
], $atts );
return '<div class="' . esc_attr($a["class"]) . '">' . $content . '</div>';
}
add_shortcode( 'highlight', 'highlight_shortcode' );
/* Tiefere Infos zu Shortcodes:
- https://www.sitepoint.com/custom-shortcodes-for-wordpress/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment