This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
programmieren:wordpress:rest_api_einrichten [2019/08/20 22:32] – jgehrke | programmieren:wordpress:rest_api_einrichten [2024/07/12 20:10] (current) – jgehrke | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Wordpress Rest-API Endpunkt einrichten ====== | ====== Wordpress Rest-API Endpunkt einrichten ====== | ||
+ | |||
+ | **AKTUALISIERTE VERSION MIT STATIC CLASS »» ** [[programmieren: | ||
+ | |||
Mit diesem Script kann ein neuer Rest-API Endpunkt eingerichtet werden, der dann über die Website URL aufrufbar ist. In diesem Beispiel wird ein //News Endpunkt// erstellt, alles nur fiktiv, aber es zeigt wie man es macht. | Mit diesem Script kann ein neuer Rest-API Endpunkt eingerichtet werden, der dann über die Website URL aufrufbar ist. In diesem Beispiel wird ein //News Endpunkt// erstellt, alles nur fiktiv, aber es zeigt wie man es macht. | ||
Line 6: | Line 9: | ||
<?php | <?php | ||
/* | /* | ||
- | Eigener Endpoint | + | Eigener Endpoint damit eine komplett |
- | JSON Ausgabe erzeugt werden kann | + | |
Doku : https:// | Doku : https:// | ||
Line 14: | Line 16: | ||
/* | /* | ||
REST URL : | REST URL : | ||
- | / | + | http:// |
*/ | */ | ||
add_action( ' | add_action( ' | ||
function register_news_endpoint(){ | function register_news_endpoint(){ | ||
- | $namespace = 'mein-blog/ | + | // http:// |
- | $route | + | // ohne Slash am Anfang und Ende |
- | $args | + | $namespace = 'mein-namespace/ |
- | ' | + | /* type => string aus klein Großbuchsten, |
- | ' | + | /* ID => integer Zahl */ |
- | ]; | + | $route |
+ | $args = [ | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ]; | ||
- | register_rest_route( $namespace, $route, $args ); | + | register_rest_route( $namespace, $route, $args ); |
} | } | ||
Line 34: | Line 41: | ||
* | * | ||
*/ | */ | ||
- | function | + | function |
/* | /* | ||
- | WP codiert | + | WP codiert |
- | Damit das JSON nicht doppelt encoded | + | Alles was davor ausgegeben |
- | muss es einmal rückgaing gemacht werden. | + | |
*/ | */ | ||
- | $current_page | + | $type = $request['type' |
+ | $ID = $request[' | ||
- | $thema_slug | + | $return_array |
+ | 'type' | ||
+ | 'ID' | ||
+ | ]; | ||
- | // Daten immer als Array anliefern, da alles was hier raus kommt | + | return $return_array; /* <- wird automatisch in JSON umgewandelt */ |
- | // noch mal json_encoded wird - da ist (array) das sicherste | + | |
- | return | + | |
} | } | ||
</ | </ | ||
+ | |||
+ | ===== Alternative Schreibweise als Klasse/ | ||
+ | |||
+ | Es ist sinnvoll die Registrierung der REST-API Route in Wordpress in eine eigene Klasse zu packen. Man verhindert Namenskonflikte, | ||
+ | |||
+ | <code php> | ||
+ | /** | ||
+ | * | ||
+ | */ | ||
+ | class CreateTrackingPoint | ||
+ | { | ||
+ | |||
+ | function __construct() | ||
+ | { | ||
+ | add_action( ' | ||
+ | } | ||
+ | |||
+ | public function register_endpoint() | ||
+ | { | ||
+ | // / | ||
+ | $namespace = ' | ||
+ | $route | ||
+ | $args | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ]; | ||
+ | |||
+ | register_rest_route( $namespace, $route, $args ); | ||
+ | } | ||
+ | |||
+ | public function render_endpoint( $request ) { | ||
+ | |||
+ | $user_id | ||
+ | | ||
+ | |||
+ | $return_array = [ | ||
+ | ' | ||
+ | ' | ||
+ | ]; | ||
+ | |||
+ | |||
+ | |||
+ | return $return_array; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ |