Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programmieren:wordpress:rest_api_einrichten [2020/03/09 19:11] – external edit 127.0.0.1programmieren:wordpress:rest_api_einrichten [2022/12/17 12:28] (current) – external edit 127.0.0.1
Line 6: Line 6:
 <?php <?php
 /* /*
- Eigener Endpoint für News-Posts, damit eine komplett eigene + Eigener Endpoint damit eine komplett eigeneJSON Ausgabe erzeugt werden kann
- JSON Ausgabe erzeugt werden kann+
  
  Doku : https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/  Doku : https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
Line 14: Line 13:
  /*  /*
   REST URL :   REST URL :
- /wp-json/mein-blog/v1/news/PAGE_NUMBER+ http://localhost:8888/wp-json/mein-namespace/v1/items/<TYPE:string>/<LEVEL:integer>
  
   */   */
 add_action( 'rest_api_init', 'register_news_endpoint' ); add_action( 'rest_api_init', 'register_news_endpoint' );
 function register_news_endpoint(){ function register_news_endpoint(){
- $namespace = 'mein-blog/v1'; + // http://localhost:8888/wp-json/mein-namespace/v1/items/<TYPE:string>/<ID:integer> 
- $route     = '/news/(?P<current_page>\d+)'; + // ohne Slash am Anfang und Ende 
- $args      = [ + $namespace = 'mein-namespace/v1'; 
- 'methods'  => 'GET', + /* type => string aus klein Großbuchsten, Zahlen & -, sowie _ */ 
- 'callback' => 'render_rest_news_endpoint', + /* ID => integer Zahl */ 
- ];+ $route     = 'items/(?P<type>[a-zA-Z0-9-_]+)/(?P<id>\d+)/'; 
 + $args      = [ 
 + 'methods'  => 'GET', 
 + 'callback' => 'render_rest_item_endpoint', 
 + 'permission_callback' => '__return_true', 
 + ];
  
- register_rest_route( $namespace, $route, $args );+ register_rest_route( $namespace, $route, $args );
 } }
  
Line 34: Line 38:
  *  *
  */  */
-function render_rest_news_endpoint( $data ) {+function render_rest_item_endpoint( $request ) {
  /*  /*
- WP codiert hier alles als JSON selbst nach+ WP codiert selbst alles im "Return" als JSON. 
- Damit das JSON nicht doppelt encoded wird + Alles was davor ausgegeben wird, taucht 1:1 so im Browser auf
- muss es einmal rückgaing gemacht werden.+
  */  */
  
- $current_page = $data['current_page'];+ $type = $request['type']; 
 + $ID = $request['id'];
  
- $thema_slug ( isset( $_GET['thema'] ) ) ? $_GET['thema': false;+ $return_array = [ 
 + 'type=> $type, 
 + 'ID     => $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 json_decode( get_news_json( $thema_slug, ['paged' => $current_page ] ) );+
 } }
  
 </code> </code>
 +
 +===== Alternative Schreibweise als Klasse/Objekt =====
 +
 +Es ist sinnvoll die Registrierung der REST-API Route in Wordpress in eine eigene Klasse zu packen. Man verhindert Namenskonflikte, schafft mehr Übersichtlichkeit und hat insgesamt bessere Code-Qualität.
 +
 +<code php>
 +/**
 + *
 + */
 +class CreateTrackingPoint
 +{
 +
 + function __construct()
 + {
 + add_action( 'rest_api_init', [ $this, 'register_endpoint'] );
 + }
 +
 + public function register_endpoint()
 + {
 + // /wp-json/moewe-tracking/v1/tracking_create/<USER_ID:integer>/<EVENT_TYPE:string>
 + $namespace = 'moewe-tracking/v1'; // ohne Slash am Anfang und Ende
 + $route     = 'tracking_create/(?P<user_id>\d+)/(?P<event_type>[a-zA-Z0-9-_]+)/';
 + $args      = [
 + 'methods'             => 'GET',
 + 'callback'            => [ $this, 'render_endpoint' ],
 + 'permission_callback' => '__return_true',
 + ];
 +
 + register_rest_route( $namespace, $route, $args );
 + }
 +
 + public function render_endpoint( $request ) {
 +
 + $user_id    = $request['user_id'];
 +    $event_type = $request['event_type'];
 +
 + $return_array = [
 + 'user_id'    => $user_id,
 + 'event_type' => $event_type,
 + ];
 +
 +
 +
 + return $return_array; /* <- wird automatisch in JSON umgewandelt */
 + }
 +}
 +</code>
 +

Page Tools