GDPR Suggestion

Hi Guys,

First of all, thanks for this Amazing Framework, you are really doing GREAT work. :smiley:

my client wants to add a GDPR cookie consent bar, he tried to install this plugin but it didn’t work and it doesn’t support Rest API.

is there any suggestion on how can I make this? I just don’t want to build everything from scratch.

thanks so much

1 Like

Hi @yasserzakaria1993

Thanks for your kind remarks, and welcome to the forum.

That plugin uses a custom post type, called cookielawinfo, that you can add to the WordPress REST-API.

Here is the WordPress developer documentation on adding an existing post type to the REST API: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-rest-api-support-for-custom-content-types/#adding-rest-api-support-to-existing-content-types.

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.

Hi. I added the following code in function.php file in the wordpress theme I use:

add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 );

function my_post_type_args( $args, $post_type ) {

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?

Hi @tix

Welcome to the Frontity community.

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.

Hi, thanks for response and advice with the code. I get the following response, though:

{
“code”: “rest_no_route”,
“message”: “No route was found matching the URL and request method.”,
“data”: {
“status”: 404
}
}

Is this maybe because I am still developing on localhost and the website is not on live server yet?

Hi @tix

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.)

Hi @tix

What do you get if you enter this in the address bar of your browser:

http://localhost/schools/wp/wp-json/wp/v2/cookielawinfo

Do you still get the No route was found matching the URL and request method message, or do you see the CPT content?

If you’re seeing the content in the browser then you will need to add the CPT to frontity.settings.js. See here for more details.

I get an empty array, like this:

[

]

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.

Hi @tix

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”?

Hi @tix

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.

I still don’t know how to do this. Can you give me an example or send me a link or something where this is explained?

If I understand correctly, I should add “Content-Type: cookielawinfo …” code somewhere?

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.

And when you type http://localhost/schools/wp/wp-json/wp/v2/cookielawinfo into the browser address bar you just get an empty array []?

Do you have any posts in the cookielawinfo CPT?

When I type http://SOMETHING/wp/wp-json/wp/v2/cookielawinfo (where WordPress is installed - I moved WordPress to live server now), I get what’s seen in the picture below:

And when I type http://localhost:3000/wp/wp-json/wp/v2/cookielawinfo (where Frontity theme is), I get:

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?).

If that’s what you mean, as you can see in the picture, the answer is: no, I don’t.