In the example they give you would replace book with cookielawinfo. You can either add the code snippet to your themeâs functions.php file, or create a custom plugin containing the code snippet.
Good luck, and do let us know how you get on or if we can help your further.
if ( 'cookielawinfo' === $post_type ) {
$args['show_in_rest'] = true;
// Optionally customize the rest_base or rest_controller_class
$args['rest_base'] = 'cookielawinfo';
$args['rest_controller_class'] = 'WP_REST_Posts_Controller';
}
return $args;
}
But the thing is still not working in frontity theme (it is working normally in wordpress theme, though). Do I have to do anything else besides what I already did?
If you enter https://your.url/wp-json/wp/v2/cookielawinfo what do you get? Do you see the content from the REST API?
Ideally you shouldnât simple copy/paste code. You should make sure that your function names are unique, so I recommend that you change the function name from my_post_type_args to something more descriptive such as add_cookielawinfo_cpt_to_rest.
Also you donât really need the two lines after the comment in the function. The first line, i.e. $args['show_in_rest'] = true; adds the post type to the REST API with the endpoint being the same as the CPT name.
Where is your WordPress installation? Do you have a localhost installation of WordPress that youâre connecting to? If so then you should use the URL of the local installation, or upload the new functions.php file to your live WordPress site if that is the one that youâre connecting Frontity to.
The message youâre getting back indicates that the cookielawinfo CPT hasnât been added to the REST API on the WordPress installation that youâre connecting to.
Hi, yes, I have a local installation of WordPress with the url of http://localhost/schools/wp/ and I am connecting to its JSON in frontity.settings.js just normally (http://localhost/schools/wp/wp-json). I get the content and all, just this GDPR is not showing up in frontity.
(Like I said, In WordPress installation in functions.php file, I have the beforementioned code.)
On the other hand, if I enter this (http://localhost/schools/wp/wp-json/wp/cookielawinfo) in the address bar, I get the same response as before: No route was found matching the URL and request method` message.
To be honest, I donât really get why this â/v2/â needs to be added in the URL anyway.
The v2 is part of the WP REST API endpoint. It should be there.
When you donât have it you get the message that no matching route was found - which is correct.
When you do have it you get an empty array. This indicates that the route was found, but there is no data so itâs returning an empty array. Ensure that the cookielawinfo CPT has some content in the WordPress admin pages.
I see. Thanks for that explanation. But how so that in frontity.settings.js, where you connect to WP REST API, you donât include â/v2/â (i.e., http://localhost/schools/wp/wp-json)? (Sorry for unrelated questions.)
Secondly, what do you mean exactly with âEnsure that the cookielawinfo CPT has some content in the WordPress admin pagesâ?
Frontity handles the endpoints after the wp-json part. In fact, in recent versions of Frontity, you only need to put your domain name. Recent versions use state.source.url rather than state.source.api. If youâre using a version of Frontity that has state.source.api then you need to define both your domain and the wp-json suffix. See here for more details.
I mean that there should be some posts that are of the type cookielawinfo.
Do you have the GDPR Cookie Consent plugin installed in WordPress? That plugin provides the cookielawinfo CPT. However it doesnât add the CPT to the REST API, which is what the code snippet you included above does - it adds the cookielawinfo CPT to the WordPress REST API.
Yes, I have it installed. And yes, I also understand that this code, I included above, needs to be added as well, and it is added. But the GDPR still doesnât show up in Frontity theme. It only shows up in WordPress.
I still donât get what you mean with âpostsâ in this cookielawinfo CPT, though. Do you mean if I have added any custom posts except the ones that are already included in this GDPR Cookie Consent plugin (like shown in the picture below?).