Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter dstmalo

    (@dstmalo)

    After looking through my messages table using phpMyAdmin I was able to locate the post record that contains my links. After inspecting the syntax of the post’s content, I was able to deduce the information that I needed.

    For those facing a similar need in the future, I’ve put together this Python script. This script modifies the navigation block via the WordPress API.


    import http.client
    import json

    # Create variables for the site you wish to appear
    # in your navigation block.
    link_label = "The Onion"
    link_desc = "America's Finest News Source"
    link_url = "www.theonion.com"

    # Here I am building the code for the navigation link.
    content = '<!-- wp:navigation-link {"label":"' + link_label + '","description":"' \
    + link_desc + \
    '","opensInNewTab":true,"url":"' + link_url + '", "kind":"custom"} /-->'

    # This translates the content variable above to JSON.
    payload = json.dumps({
    "content": content,
    })

    conn = http.client.HTTPSConnection("<your domain>")

    headers = {
    'Content-Type':'application/json',
    'Authorization': '<your code here>'
    }

    conn.request("POST", "/wp-json/wp/v2/navigation/<id>", payload, headers)
    # <id> is the ID of your navigation "post" ^^^^

    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))

    Perhaps the most valuable part of this code is building the string that I assign the to variable ‘content.’

    Note that this code replaces any links that are already in the Navigation block, and it only creates one link. I leave it as an exercise to the reader to replace or add strings.

    May you be blessed by the god of your choosing.

    Don

    Thread Starter dstmalo

    (@dstmalo)

    This solved my problem. Thank you.

    I notice that the

    <!-- wp:html -->

    tag “escapes” the rest of the message. Are there other escape sequences like this? Is there are list of wp:xxxx tags somewhere?

Viewing 2 replies - 1 through 2 (of 2 total)