Cannot edit functions.php and any of the website pages anymore on localhost

30 views Asked by At

I've just installed Wordpress 6.4.3 & MAMP and managed to edit functions.php and saved my changes.

Twenty Twenty-Four: Theme Functions (functions.php)

When I tried to make more changes to the file, Wordpress started displaying the following error message:

Something went wrong. Your change may not have been saved. Please try again. There is also a chance that you may need to manually fix and upload the file over FTP.

When I try to edit my website pages, I started getting the following error message now:

Updating failed. The response is not a valid JSON response.

This is what I've added to functions.php successfully and cannot edit it anymore

if ($_SERVER["REQUEST_METHOD"] === "POST") {
  $name = sanitize_text_field($_POST["name"]);
  $email = sanitize_email($_POST["email"]);
  $message = sanitize_textarea_field($_POST["description"]);

  // Add code to save the form data to the database
  global $wpdb;
  $table_name = $wpdb->prefix . 'request_form';
  $data = array(
    'name' => $name,
    'email' => $email,
    'description' => $message,
    'submission_time' => current_time('mysql')
  );
  $insert_result = $wpdb->insert($table_name, $data);

  if ($insert_result === false) {
    $response = array(
      'success' => false,
      'message' => 'Error saving the form data.',
    );
  } else {
    $response = array(
      'success' => true,
      'message' => 'Form data saved successfully.'
    );
  }

  // Return the JSON response
  header('Content-Type: application/json');
  echo json_encode($response);
  exit;
}

What I have done:

  • I've checked permissions of the file and gave read and write access to everyone on my localhost.

  • Restarted the server

  • Moved wordpress to a new folder

  • When I changed http to https for wordpress Address (URL) and Site Address (URL) under Settings > General and saved the changes, I was redirected to the following address http://localhost:8888/wp-admin/options.php and can see the following:


    Notice: Undefined index: name in /Applications/MAMP/htdocs/wp- content/themes/twentytwentyfour/functions.php on line 209

    Notice: Undefined index: email in /Applications/MAMP/htdocs/wp- content/themes/twentytwentyfour/functions.php on line 210

    Notice: Undefined index: description in /Applications/MAMP/htdocs/wp- content/themes/twentytwentyfour/functions.php on line 211
    WordPress database error: [Unknown column 'submission_time' in 'field list']
    INSERT INTO `wp_request_form` (`name`, `email`, `description`, `submission_time`) VALUES ('', '', '', '2024-03-30 07:10:51')

    {"success":false,"message":"Error saving the form data."}
1

There are 1 answers

0
Emer On

To bypass the error message, I've accessed functions.php file in wordpress folder and commented out the last section of my code which is to return JSON response.

After saving the file, I accessed the file on Wordpress and managed to continue editing it!!!!