WordPress-API mit Access ansprechen: So hole ich Posts, User und Formulare per VBA ab

Du willst aus Access direkt auf Deine WordPress-Daten zugreifen.
Nicht per ODBC oder Export – sondern sauber ĂŒber die REST-API.
Das geht. Und zwar mit purem VBA.

Ich zeig Dir, wie Du Posts, Benutzer oder Formulare abrufst – und was Du dabei beachten solltest.

Voraussetzungen

  • WordPress REST-API ist aktiv (Standard seit WP 4.7)
  • Endpunkte erreichbar z. B. unter https://deine-domain.de/wp-json/wp/v2/posts
  • Bei geschĂŒtzten Endpunkten brauchst Du Authentifizierung (z. B. per JWT oder App Passwords)

Access: Die HTTP-Anfrage in VBA

Public Function HoleVonWP(apiUrl As String, Optional authHeader As String = "") As String
    Dim http As Object
    Set http = CreateObject("MSXML2.XMLHTTP")

    http.Open "GET", apiUrl, False
    http.setRequestHeader "Content-Type", "application/json"
    
    If authHeader <> "" Then
        http.setRequestHeader "Authorization", authHeader
    End If

    http.Send

    If http.Status = 200 Then
        HoleVonWP = http.responseText
    Else
        HoleVonWP = "Fehler " & http.Status & ": " & http.statusText
    End If
End Function

Beispiel: Posts abrufen

Sub HolePosts()
    Dim json As String
    json = HoleVonWP("https://deine-domain.de/wp-json/wp/v2/posts?per_page=5")
    Debug.Print json
End Sub

Tipp: Nutze einen JSON-Parser fĂŒr Access wie VBA-JSON von Tim Hall

Dann kannst Du den Inhalt direkt aufrufen:

Dim j As Object
Set j = JsonConverter.ParseJson(json)

Dim eintrag As Variant
For Each eintrag In j
    Debug.Print eintrag("id"), eintrag("title")("rendered")
Next

Beispiel: Benutzer abrufen (nur mit Auth)

Sub HoleUser()
    Dim json As String
    Dim token As String
    token = "Bearer eyJhbGciOi..." ' JWT oder Application Password Header

    json = HoleVonWP("https://deine-domain.de/wp-json/wp/v2/users", token)
    Debug.Print json
End Sub

Tipp: Application Passwords erzeugst Du im WordPress-Backend unter Deinem Benutzerkonto.
Dann als Header:

authHeader = "Basic " & Base64Encode("user:app-password")

Base64 Encode in VBA

Public Function Base64Encode(text As String) As String
    Dim arrData() As Byte
    arrData = StrConv(text, vbFromUnicode)

    Dim objXML As Object, objNode As Object
    Set objXML = CreateObject("MSXML2.DOMDocument")
    Set objNode = objXML.createElement("b64")

    objNode.DataType = "bin.base64"
    objNode.nodeTypedValue = arrData
    Base64Encode = objNode.Text
End Function

Formulardaten abrufen (z. B. aus Contact Form 7 oder WPForms)

Viele Formplugins bieten eigene REST-Endpunkte – z. B. ĂŒber ein Addon.
Oder Du nutzt Flamingo, wenn Du Contact Form 7 verwendest.

Beispiel:

json = HoleVonWP("https://deine-domain.de/wp-json/flamingo/v1/inbound-messages", token)

Ohne Plugin: Speichere Formularinhalte in einen Custom Post Type (z. B. form_entry)
Dann kannst Du sie ĂŒber die normale Posts-API abfragen:

json = HoleVonWP("https://deine-domain.de/wp-json/wp/v2/form_entry?per_page=10")

Zugriff limitieren und filtern

Du kannst alle Standardfilter der WP-REST-API nutzen:

  • ?author=1
  • ?status=publish
  • ?after=2024-01-01T00:00:00
  • ?search=test
  • ?orderby=modified

Beispiel:

json = HoleVonWP("https://deine-domain.de/wp-json/wp/v2/posts?after=2024-01-01T00:00:00&per_page=20")

Daten in Access-Tabelle schreiben

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("tbl_WPPosts", dbOpenDynaset)

For Each eintrag In j
    rs.AddNew
    rs!WP_ID = eintrag("id")
    rs!Titel = eintrag("title")("rendered")
    rs!Inhalt = eintrag("content")("rendered")
    rs.Update
Next

Mein restliches Fazit

REST-API heißt: WordPress ist nicht mehr nur OberflĂ€che.
Du kannst Dir Daten holen, wann und wie Du willst – direkt aus Access.
Und das Beste: Du brauchst kein zusÀtzliches Tool, keine teuren Addons.

Nur ein bisschen sauberes VBA.
Und ein Schaf, das weiß, wie man JSON frisst.

Kategorien: