I'm getting page content via calling /wp-json/v2/page/ And there is no one field with CSS styles. How could I add css styles into json response ? Maybe exist some plugin, which can add custom field ?.. something like this https://wordpress.org/plugins/wp-api-yoast-meta/ will be nice

  • 1
    Why would there be CSS in an API response? Seems like there's a misunderstanding of what the API is and what it's for here... – Jacob Peattie Jun 21 at 11:42
  • so , there is no way to get css content? only html ? – Алексей Самойлич Jun 21 at 11:47
  • 1
    No. That's not how any of these things are supposed to work. CSS is for presentation and is the responsibility of the site doing the output. If certain styles are required for the output to be rendered properly then you should either find that CSS and just give it to them to add to their site, or structure the data differently so that it uses distinct fields rather than lots of custom markup. – Jacob Peattie Jun 21 at 12:00

CSS of what?

CSS can be relevant only in the context of the full page, and as long as you do not request data for the full page (which you can easily do by just fetching the page and parsing it for the relevant style rules) you lack context. There is simply no such thing as a CSS of a post. Posts can appear in many contexts and in each of them have different styling.

So it is not so much an issue with a technical way to do that, as more of the fact that the requirement itself do not make much sense in context of an API that is supposed to bring data on a single post without any hint of a context.

Mark's answer is correct about having no CSS in the API output. However, if you need to get the URL of your CSS file for any reason, you can add a custom rest route and output it's URI.

Here's a simple code that goes into your theme's functions.php file:

// Register a new rest route
add_action( 'rest_api_init', 'my_rest_routes' );

function my_rest_routes() {

    register_rest_route(
        'alexi/v1',
        '/output_css_uri/',
        [
            'methods'  => 'GET',
            'callback' => 'output_css_callback',
        ]
    );
}


// Callback function to output the css URI
function output_css_callback() {
    return get_stylesheet_uri();
}

Now, you can get the stylesheet's URI dynamically by accessing the below address:

http://example.com/wp-json/alexi/v1/output_css_uri

However there's not much point in outputting the main stylesheet's URI, since you probably already know where it's located, but it can be used to output any URI, including custom JS and CSS files.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.