Thanks for reporting this, I will investigate further and see if there is anything that can be done from the plugin side of things.
I just looked into this, but not sure if you are asking about the plugin, “Contact Form 7”, or some other plugin..? Please let me know what you are referring to when you say, “The script for Contact form no longer works…” Thank you, @hupe13
Thread Starter
hupe13
(@hupe13)
Oh, this was a long time ago. I used your plugin twice, once with Contact Form 7 and once with my application. Suddenly both stopped working, I don’t know why. There are different hosters. So I deleted your plugin and used another one. And I meant this script.
Now I have looked at it again and can confirm that it works with Contact Form 7 on both sites, but not with my application without the above changes.
The $_SERVER['REQUEST_URI'] for my REST API is /rest/api/path/?querystring. I changed your code again, now looks better:
if (isset($_SERVER['REQUEST_URI']) ) {
$this_url = wp_parse_url( $_SERVER['REQUEST_URI'] );
if (!empty($server_var)) {
if (is_array($server_var)) {
foreach($server_var as $var) {
if ( $this_url['path'] === $var ) {
return true;
}
}
}
} else {
if ( $this_url['path'] === $server_var) return true;
}
}
So it works for my application too.
Thread Starter
hupe13
(@hupe13)
I can’t edit the code, else must be one line above:
if (isset($_SERVER['REQUEST_URI']) ) {
$this_url = wp_parse_url( $_SERVER['REQUEST_URI'] );
if (!empty($server_var)) {
if (is_array($server_var)) {
foreach($server_var as $var) {
if ( $this_url['path'] === $var ) {
return true;
}
}
} else {
if ( $this_url['path'] === $server_var) return true;
}
}
}
return false;
Okay thank you for sharing your code. It sounds like everything is wrapped up for this thread, so will go ahead and mark it as resolved. Also just fyi, I retested Contact Form 7 with Disable REST API and sending email worked out of the box, with no custom code or fixes required. The tests were done with CF7 version v6.0.5 on three different WP themes. So apparently CF7 resolved the not working issue when REST API is disabled.
Thread Starter
hupe13
(@hupe13)
The problem with CF7 does not exists anymore.
Should I open a new thread with the problem if $_SERVER['REQUEST_URI'] contains a query string? As it is now, I can’t use your plugin for this.
This thread is fine, but I am not understanding the issue.. does it relate to WordPress in general, or some other plugin, or some custom code..?
Thread Starter
hupe13
(@hupe13)
It is my testing plugin. It generates urls for the REST API in the form https://my-domain.tld/path/wp-json/leafext-tileproxy/v1/tiles/?tile=https://tile.openstreetmap.org/12/1324/1478.png
My customized script for opening the REST API worked. Maybe my hoster changed something or WordPress, I don’t know. Then it stopped working. You are checking in your plugin the$_SERVER['REQUEST_URI']. It contains the query string and thats why the condition $_SERVER['REQUEST_URI'] === $var is never true.
Okay thanks. I see that you have posted several code snippets, and it is confusing as they are all similar. Is there one specific snippet that shows the changes that you are recommending?
Thread Starter
hupe13
(@hupe13)
See your code: https://plugins.trac.wordpress.org/browser/disable-wp-rest-api/trunk/disable-wp-rest-api.php#L87
Change this to my last one: https://wordpress.org/support/topic/contact-form-not-working-2-3/#post-18373434
My code works for both CF7 and my app. But both use GET, but I don’t know about POST.
I had until now tested the scripts for opening the REST API individually. If you have more than one REST API to open, change this to:
function disable_wp_rest_api_server_var_custom($var) {
$rest_api_path = wp_parse_url(get_rest_url())["path"];
$cf7 = array(
$rest_api_path . 'contact-form-7/v1/contact-forms/9781/refill',
$rest_api_path . 'contact-form-7/v1/contact-forms/9781/refill/',
$rest_api_path . 'contact-form-7/v1/contact-forms/9781/feedback',
$rest_api_path . 'contact-form-7/v1/contact-forms/9781/feedback/',
$rest_api_path . 'contact-form-7/v1/contact-forms/9781/feedback/schema',
$rest_api_path . 'contact-form-7/v1/contact-forms/9781/feedback/schema/'
);
if ( is_array($var) ) {
$var = array_merge($var,$cf7);
} else {
$var = $cf7;
}
return $var;
}
add_filter('disable_wp_rest_api_server_var', 'disable_wp_rest_api_server_var_custom');
Thanks I will look closer at this for the next update, and maybe implement if it’s something that will benefit users in general. Thank you for sharing your code, much appreciated.
Thread Starter
hupe13
(@hupe13)
I looked up where cf7’s number (9781) comes from, but I don’t understand this. If you know that, you could do it too.