Support Forum
Hi there,
We've recently updated Metabox and our extension, unfortunaltey i can't tell which version we had previously installed. Since this update to todays latest version of Metabox and all the extension we're using, we are facing the following issue which wasn't one for more than 1,5 years.
We have "post" fields to choose posts/pages rom any post types and as field_type we are using "selected_advanced". This ends in https://example.com/wp-admin/admin-ajax.php 400 (Bad Request). If i change to field_type "select" all is working correctly.
Here's a code snippet, hope this helps:
'fields' => [
[
'id' => 'login_page_id',
'name' => _x( 'Login page', 'admin', 'demo' ),
'desc' => _x( 'Choose your default login page. This will change the login page URL of WordPress to point to the chosen page.', 'admin', 'demo' ),
'type' => 'post',
'post_type' => 'any',
'field_type' => 'select_advanced',
'placeholder' => _x( 'Default WordPress login page', 'admin', 'demo' )
],
Please advise, the "select" option is hard to use in environments with hundreds of posts/pages.
Best, John
Hi John,
We updated Meta Box last year to add Ajax support for object fields, to improve the performance on sites with thousands of posts.
Can you try reinstalling Meta Box? And clear the browser cache before trying again? I think there might be something like missing files or caching of old JS files.
Hi Ahn,
Thank you for looking into this! I really appreciate it!
I reinstalled Meta Box and cleared the browser cache. I also created a very new WP site, still no joy.
The ajax call ends as a 400 (badrequest).
I don't think it is a caching issue. It also is not a general ajax issue, since other ajax calls working as expected.
I've checked with others in my team, they experiencing the same issue. In the select box it displays "The results couldn't be loaded", once i change the "field_type" from "select_advanced" to "select" all is working, but i've mentioned that earlier in our conversation.
Not sure if this might help you to sort this out, but we're using this with your "settings page extension" implemented through.
Please advise.
Thank you!
Best,
John
Hi John,
Can you share the full code of the meta box and settings page to test? I can't replicate the bug on my end :(.
Hi Ahn,
we've figured it out.
In our code we don't add our metabox code to your filter array on admin pages where it shouldn't be displayed.
It seems like that your AJAX call triggers the filter execution and because we're not adding our code on admin-ajax.php it breaks somehow your ajax call.
We've workaround it and all is working for us.
Thanks for assistance, we're really appreciate your effort!
I have the exact same problem with a "post field" always giving no result, in inspector post request gets 400 bad request.
What was your workaround?
I'm adding the field with metabox builder. Any help would be appreciated.
Our issue was the we've checked in the filter callback if we're on the page in the WP backend where the metabox we've defined should be displayed or not.
If not, we've returned the array we've received through the metabox filter hook as is, without adding our metabox and fields. The intention was to add our metabox and fields only on the settings pages where we need it.
This works fine, but some field types like select_advanced doing some ajax calls to retrieve the selectable options, in our case back then because we've used the field without the options parameter, instead we used it to offer posts to select. In that case, metabox does an ajax call to retrieve and populate the select_advanced field options with the available posts. I assume the post_field does an ajax call as well and works similar.
Since we kind of registered the metabox only on that specific settings page, the metabox and it's fields weren't defined when doing the ajax call.
We've fixed it by removing our check if we're on the page on which we need the metabox.
Here's an example which might help:
private function __construct() {
add_filter( 'mb_settings_pages', [ $this, 'settings_page'], 0, 1 );
add_filter( 'rwmb_meta_boxes', [ $this, 'settings'], 0, 1 );
}
public function settings_page( $settings_pages ) {
// add our settings pages
return $settings_pages
}
public function settings( $meta_boxes ) {
/*
* This broke it when having a field type which is doing ajax calls. Fields which aren't doing ajax calls are
not affected.
*/
//=======================================
if ( ! empty( $_GET['page'] ) ) {
if ( 'my-settings-page' <> $_GET['page'] ) {
return $meta_boxes;
}
}
//=======================================
$meta_boxes[] = [
'id' => 'my_settings',
'title' => 'Login Settings',
'settings_pages' => 'my-settings-page',
'tab' => 'login_settings',
'fields' => [
[
'id' => 'login_page_id',
'name' => _x( 'Login page', 'admin', 'demo' ),
'desc' => _x( 'Choose your default login page.', 'admin', 'demo' ),
'type' => 'post',
'post_type' => 'any',
'field_type' => 'select_advanced',
'placeholder' => _x( 'Default WordPress login page', 'admin', 'demo' )
]
]
];
return $meta_boxes;
}
Hope that helps!