• dstmalo

    (@dstmalo)


    Hello All,

    I am perplexed by the instructions listed on the API manual page here. The page ostensibly describes updating Navigation blocks (as opposed to navigation menus). However I think someone copied here the instructions for creating new posts. The URLs, however, are correct, and I have been able to view my navigation blocks using PostMan. I’m just perplexed on how to update my blocks via the API.

    I would love some simple instructions for modifying my navigation blocks, similar to the documentation here.

    Don

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Anton Vlasenko

    (@antonvlasenko)

    Hello,

    Querying the?/wp/v2/navigation?endpoint will invoke the?WP_REST_Posts_Controller?REST controller under the hood since navigation blocks are just posts with the?wp_navigation?post type. I think the documentation is correct, as it describes how to update posts (navigation blocks === posts). Therefore, the documentation reflects how the data is stored in the database.

    Could the documentation be improved? Possibly. However, I believe it is factually correct.

    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

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.