How to get site’s name from WP API

I’m trying to get WordPress website title using javascript and WP API plugin

I didn’t find any example on how to get the site’s name but I found the variable name under the entities section in the developer guide

Read More
function _updateTitle(documentTitle) {
    document.querySelector('title').innerHTML = 
        documentTitle + ' | '+ $http.get('wp-json/name');
}

The output string of $http.get('wp-json/name') is [object Object]

Output

Does anyone know how to use fix this?

Related posts

2 comments

  1. You didn’t get enough context. What’s $http? What happens when you go to wp-json/name directly in your browser? Here’s what I see:

    [{
      "code":"json_no_route",
      "message":"No route was found matching the URL and request method"
    }]
    

    Here’s a simple example to get you the title:

    var siteName;
    $.getJSON( "/wp-json", function( data ) {
      siteName = data.name;
    });
    
  2. See more elegant solution here https://wordpress.stackexchange.com/a/314767/94636
    response will not contain extra data like:

    authentication: []
    namespaces: ["oembed/1.0", "akismet/v1", "acf/v3", "wp/v2"]
    routes: {/: {namespace: "", methods: ["GET"],…},…}
    timezone_string: ""
    ...
    _links: {help: [{href: "http://v2.wp-api.org/"}]}

Comments are closed.