{ code: ‘db_insert_error’, message: ‘The post could not be inserted into the database.’, data: { status: 500 }, additional_data: [ ‘WordPress database error: The following field could not be processed: post_content. The value sent may be too long or contain invalid data.’ ] }
]]>I have some Python code that sends a request to a custom REST API endpoint I added. I have a plugin that listens to the request and adds a new user by doing:
$random_password = wp_generate_password(8, false);
$user_data = array(
'user_login' => $user_email,
'user_pass' => $random_password,
'user_email' => $user_email,
'role' => 'subscriber',
'first_name' => $first_name,
'last_name' => $last_name,
);
$user_id = wp_insert_user($user_data);
where the fields are passed from the REST API HTTP request.
For some reason the plugin works as expected if I send the request from my local machine, but it doesn’t work when the request is sent from my AWS Lambda, giving a "User insertion failed: Not enough data to create this user."
error.
When I print out the request content, the one sent from my local machine looks identical to the one sent from my AWS Lambda.
When I print out $user_data
in debug.log
, the content and type of the array is identical.
This is the code I use to print out the array:
echo "Type of \$user_data: " . print_r(gettype($user_data)) . " -- ";
if (empty($user_data)) {
echo "\$user_data is empty." . " -- ";
} else {
echo "\$user_data is not empty." . " -- ";
echo "Contents of \$user_data: " . " -- ";
print_r($user_data);
}
if (is_wp_error($user_id))
{
$error_message = $user_id->get_error_message();
print_r("User insertion failed: $error_message");
$result = false;
}
else
{
$result = $user_id;
}
and this is the error I get:
"arrayType of $user_data: 1 -- $user_data is not empty. -- Contents of $user_data: -- Array\n(\n [user_login] => [email protected]\n [user_pass] => aa0fDLcG\n [user_email] => [email protected]\n [role] => subscriber\n [first_name] => John\n [last_name] => Doe\n)\nUser insertion failed: Not enough data to create this user.{\"code\":\"insert_error\",\"message\":\"Error in inserting user with the user_email [email protected]\"
As it can be seen the array is not empty but I still get the Not enough data to create this user
error.
Unfortunately with my current host I don’t have access to wp-include
, so I can’t debug wp_insert_user()
directly from user.php
.
Thank you!
]]>When I submit the form that calls fields directly, it works fine. However, the form that uses do_settings_sections() and callbacks results in a “400 Bad Request” error when submitted.
Why isn’t the do_settings_sections form submitting correctly?
Here’s a simplified version of my code:
function snap_save_settings() {
error_log('triggered snap_save_settings, Success!');
}
add_action('admin_post_snap_save_settings', 'snap_save_settings');
class Snap_Settings extends Snap_Settings_Page {
protected function initialize_settings() {
// General Settings Section
$this->add_section('snap_main_settings', '', [$this, 'main_settings_section_callback'], 'snap-main-settings');
$this->add_field('snap_main_settings', 'snap_linking_mode', '', [$this, 'linking_mode_callback'], 'snap-main-settings');
}
//callback
public function main_settings_section_callback() {
}
public function linking_mode_callback() {
// Output a basic text input form field for testing
echo '<label for="snap_test_input">Test Input:</label>';
echo '<input type="text" id="snap_test_input" name="snap_test_input" value="">';
}
// Display Settings
public function display_settings_page() {
?>
<div class="wrap">
<!-- Form using do_settings_sections() -->
<form method="post" action="<?php echo admin_url('admin-post.php'); ?>">
<label>This form is rendered using do_settings_sections and does not work</label>
<input type="hidden" name="action" value="snap_save_settings">
<?php
settings_fields('snap_settings_group');
do_settings_sections('snap-main-settings');
submit_button();
?>
</form>
<br>
<br>
<!-- Form with fields rendered directly -->
<form method="post" action="<?php echo admin_url('admin-post.php'); ?>">
<label>This form is rendered directly and it works</label>
<input type="hidden" name="action" value="snap_save_settings">
<input type="text" name="snap_test_input" value="Test Value">
<input type="submit" value="Save Changes">
</form>
</div>
<?php
}
}
new Snap_Settings();
Here is the settings base:
abstract class Snap_Settings_Page {
private $sections = [];
abstract protected function initialize_settings();
public function __construct() {
add_action('admin_menu', [$this, 'add_menu_page']);
add_action('admin_init', [$this, 'register_settings']);
}
public function add_menu_page() {
add_options_page(
'Snap Settings', // Page title
'Snap', // Menu title
'manage_options', // Capability
'snap-settings', // Menu slug
[$this, 'display_settings_page'] // Callback function
);
}
public function register_settings() {
$this->initialize_settings();
foreach ($this->sections as $section_id => $section) {
add_settings_section(
$section_id,
$section['title'],
$section['callback'],
$section['page_slug']
);
foreach ($section['fields'] as $field_id => $field) {
add_settings_field(
$field_id,
$field['title'],
$field['callback'],
$field['page_slug'], // Use the page_slug from the field
$section_id
);
register_setting('snap_settings_group', $field_id);
}
}
}
protected function add_section($id, $title, $callback, $page_slug = 'snap-settings') {
$this->sections[$id] = [
'title' => $title,
'callback' => $callback,
'fields' => [],
'page_slug' => $page_slug // Store the page_slug with the section
];
}
protected function add_field($section_id, $field_id, $title, $callback, $page_slug = 'snap-settings') {
if (!isset($this->sections[$section_id])) {
// Optionally handle the error if the section does not exist
return;
}
$this->sections[$section_id]['fields'][$field_id] = [
'title' => $title,
'callback' => $callback,
'page_slug' => $page_slug
];
}
abstract public function display_settings_page();
}
]]>The site also says I am running on a staging server when created a new site.
When I try to access the api, I receive
{
“error”: “unauthorized”,
“message”: “API calls to this blog have been disabled.”
}
I have tried following:
1. checked the Xml-RPC file, it is accessible and gives: “XML-RPC server accepts POST requests only.”
2. deactivated all plugins
3. disconnected Jetpack
4. deactivated Jetpack and reactivated
However, I still receive the error:
{
“error”: “unauthorized”,
“message”: “API calls to this blog have been disabled.”
}
What could be the issue? Any support would be very appreciated.
Thanks,
Yuri
I install WordPress on my subdirectory with Nginx and proxy pass.
But after clicking on Add plugins or add themes, it shows An unexpected error occurred. Something may be wrong with www.remarpro.com or this server’s configuration. If you continue to have problems, please try the support forums.
https://ibb.co/WtJmSMW
how can I fix this?
Regards
]]>Screenshot (adding featured image before saving post): https://ibb.co/8g4hvF7
Screen shot (After saving post): https://ibb.co/w4n3Y8h
This issue is happening since I migrated my WordPress website from one hosting domain to another. There may be any issue within the database. Please let me know how to fix it.
]]>The same code I have used for another website and is working.
Thanks
Udit
Can anybody help me with this? I know it’s something like this but can’t figure out the exact way to do it.
Here is my route:
add_action('rest_api_init', function() {
register_rest_route('woo/v2', 'woocommerce/order_summary_by_date/(?P<start_date>/<end_date>)', array(
'methods' => 'GET',
'callback' => 'woocommerce_orders_by_dates'
));
});
This is the function to access:
function woocommerce_orders_by_dates($start_date, $end_date) {
$start_date = $start_date['start_date'];
$end_date = $end_date['start_date'];
return $start_date . $end_date;
}
URL is: https://example.com/wp-json/woo/v2/woocommerce/order_summary_by_date/?start_date=2021-09-01&end_date=2021-09-30
Any help highly appreciated.
]]>I used this method to create a featured media url field called imageLink. it appears in /posts/id but wont get me the link when i access it from my app
]]>