Support » Allgemeine Fragen » wie füge ich einen Shortcode hinzu OOP

  • Hallo, ich bin Anfänger und habe Probleme einen Shortcode in meinem PHP Plugin hinzuzufügen.
    Ich würde mich freuen wenn mir jemand helfen könnte.
    LG

    
        function show_map_to_draw()
        {
            add_shortcode('iKuhMapToDraw',$this->GenerateMapToDraw());
        }
    
        function GenerateMapToDraw(){
            $return = "MAP";
            return $return;
        }

    FEHLER:

    Notice: do_shortcode_tag was called incorrectly. Attempting to parse a shortcode without a valid callback: iKuhMapToDraw Please see Debugging in WordPress for more information. (This message was added in version 4.3.0.) in /var/www/html/iKuh/wp-includes/functions.php on line 4773

    [iKuhMapToDraw]
    [iKuhMapToDraw]

Ansicht von 2 Antworten - 1 bis 2 (von insgesamt 2)
  • Mit

    add_shortcode( 'hello', 'pix_sag_hallo' );

    bringst du WordPress bei, dass es einen neuen Shortcode [hello] gibt. Wie WordPress mit diesem Shortcode umgehen soll, steht in einer Funktion pix_sag_hallo(), die anschließend definiert wird:

    function pix_sag_hallo() {
      return '<p>Hallo Welt!</p>';
    }

    Überall dort, wo du jetzt in Beiträgen oder Seiten den Shortcode [hello] verwendest, wird also nun ein Absatz „Hallo Welt!“ eingefügt. Achte darauf, dass die Funktion mit return den Inhalt an WordPress zurückgibt. Mit echo würde der Textstring an einer anderen Stelle ausgegeben.

    Der komplette Code für den Shortcode lautet also

    add_shortcode( 'hello', 'pix_sag_hallo' );
    function pix_sag_hallo() {
      return '<p>Hallo Welt!</p>';
    }

    Eine weitere Verschachtelung in Funktionen oder Klassen ist nicht erforderlich.

    Komplizierter wird es, wenn der Shortcode Parameter enthalten soll, um die Ausgabe zu steuern. Nehmen wir an, du möchtest „Hallo Klasse 8c“ auf der Schulwebseite ausgeben, wobei die Klasse selber austauschbar sein soll, damit auch „Hallo Klasse 6a“ oder „Hallo Klasse 13b“ ausgegeben werden kann. Der Shortcode soll nach dem Muster [hello klasse="8c"] eingefügt werden.
    Zuerst definierst du wieder den Shortcode mit
    add_shortcode( 'hello', 'pix_sag_hallo' );

    Die Funktion ist etwas erweitert:

    function pix_sag_hallo( $atts ) {
      $ausgabe = '<p>Hallo Klasse ' . $atts['klasse'] . '</p>';
      return $ausgabe;
    }

    Wird im Shortcode kein Parameter verwendet ([hello]), wird nun ein Absatz „Hallo Klasse“ ausgegeben, ansonsten wird der Parameter übernommen und [hello klasse="6a"] gibt „Hallo Klasse 6a“ aus.

    Vielleicht möchtest du für den Fall, dass kein Parameter angegeben wird, einen Standard-Wert setzen? Auch das ist möglich:

    function pix_sag_hallo( $atts ) {
      // verwende "klasse=1a", wenn kein Parameter gesetzt wurde
      $werte = shortcode_atts( 
                  array( 'klasse' => '1a' ),
               ), $atts);
      $ausgabe = '<p>Hallo Klasse ' . $werte['klasse'] . '</p>';
      return $ausgabe;
    }

    Mehr Informationen zur Shortcode-API findest du hier:
    https://codex.wordpress.org/Shortcode_API

    Nachtrag: Da es um die Verwendung in einer Class ging:

    class PluginExample {
    	public function __construct() {		
    		add_shortcode( 'hello', array( $this, 'pix_say_hello' ) );
    	}
    	
    	public function pix_say_hello() {		
    		return '<p>Hello World!</p>';
    	}
    }
    
    $pluginExample = new PluginExample();
Ansicht von 2 Antworten - 1 bis 2 (von insgesamt 2)
  • Das Thema „wie füge ich einen Shortcode hinzu OOP“ ist für neue Antworten geschlossen.