9.1. Einen Shortcode erstellen

Ein Shortcode wird mit der Funktion add_shortcode() angelegt und ist fortan mit dem Namen $tag z.B. im Editor verwendbar.

<?php
function add_shortcode( $tag, $func ) {
	...
}
?>

Es gilt:

You’re not allowed to see this content. Please log in first.

Aufgerufen wird der Shortcode dann z.B. im Inhaltsbereich eines Beitrags. Im Editor fügt man dazu den Shortcode in eckigen Klammern ein:

[$tag]

Wird der Shortcode von WordPress entdeckt, so wird die Funktion $func ausgeführt und dessen Endresultat zurückgegeben.

Beispiel ohne Parameter

You’re not allowed to see this content. Please log in first.

Beispiel mit Parameter

You’re not allowed to see this content. Please log in first.

Hinweis

Es spielt keine Rolle, ob Sie den Shortcode so:

  • [mm_current_time date_format="d.m.Y"] (ohne schließenden Tag) oder so:
  • [mm_current_time date_format=d.m.Y] (ohne Anführungsstriche) oder so:
  • [mm_current_time date_format=d.m.Y][/mm_current_time date_format=d.m.Y] (mit schließendem Tag) oder so:
  • [mm_current_time date_format=d.m.Y /]

angeben. Alle Formate werden ordnungsgemäß erkannt.

Hinweis 2

Wollen Sie einen Shortcode ausgeben ohne dass er in entsprechende HTML-Codes umgewandelt werden soll? Schreiben Sie ihn einfach in doppelten eckigen Klammern, z.B. [[mm_current_time]].

Hinweis 3

Sie können Shortcodes auch ineinander verschachteln. Erfahren Sie mehr darüber im Kapitel 9.8. (Beispiel 3).