0

For the love of God can anyone help me?

My theme I had built a year or so ago, I added custom meta boxes/fields. I have created numerous custom taxonomies. But I didn't want the custom taxonomies to be placed in traditional check boxes. I want them in SELECT dropdown menus as options. Everything worked fine until I updated from version 4.9 to 5.2(latest update).

Now, when I try to add a new listing/post, it does not save the data and throws multiple PHP warnings.

First I will display the warnings and below that will be my code.

**************** EDIT *****************

php error

Here is a link to my Github which shows the files I am working with: https://github.com/pcross1986/car-dealership

UPDATE - 6-28-2019 SCREENSHOT with errors

Screenshot of new errors

UPDATE - 6-28-2019 When trying to add a new post I also get this error

enter image description here

UPDATE 6.29.2019

It looks like I am very close. I can now go into a current post and it looks like the data is now saving after editing and updating the listing.

The problem now is, when I go to create a NEW listing, it gives me this error. Screenshot is below:

enter image description here

  • 2
    That's far too much code to review for one question, and I can't format it properly for you without exceeding the allowed number of characters. Can you reduce it in some way? The code for registering the post type shouldn't be relevant when dealing with meta boxes, for example. – Jacob Peattie Jun 27 at 5:21
  • I apologize for the mess..... I have uploaded the files to my Github so is much easier to read. I have also updated the screenshot above. github.com/pcross1986/car-dealership – Preston Jun 27 at 18:22
0

Update 29.6.19

I think the array_map() error is actually caused by how the selects are named. They have names like tax_input[fuel-type], which probably results in them getting passed to the default taxonomy saving function. As only single value is passed for each select and I assume the tax save handler expects an array, the array_map() warning is raised.

This would also explain why some of the values weren't saved. The values weren't in $_POST[$valid_taxonomy], but $_POST['tax_input'][$valid_taxonomy] (meta_boxes.php:416). I missed this when I wrote my loop examples.

You could experiment with either changing the select names to name="tax_slug" or by changing the names to array format name="tax_input[fuel-type][]". foreach ( $valid_taxonomies as $valid_taxonomy ) should start working with the former option and would be unnecessary with the latter.

Try replacing meta_boxes.php lines 59-60 with wp_nonce_field( 'save_car_details_meta', 'nonce_car_details'); and lines 361-363 with check_admin_referer( 'save_car_details_meta', 'nonce_car_details' );. Do similiar thing for the testimonials metabox and its saving function.


New answer

I had a quick glance on your code on GitHub and I think something like this should make it work.

Replace lines 372 - 518 with the code below. Have a helper array with valid keys that you compare the $_POST against. Loop meta and term data separately and save them accordingly - meta data as post meta, set term data as post terms.

if ( 'revision' === $post->post_type ) {
  return;
};

$valid_meta_keys = array(
  '_vin',
  '_mileage',
  '_intcol',
  '_tot',
  '_nod',
  '_trim',
  '_price',
);
foreach ( $valid_meta_keys as $valid_meta_key ) {
  $current_value = get_post_meta( $post->ID, $valid_meta_key, FALSE );
  $new_value = isset( $_POST[$valid_meta_key] ) ? $_POST[$valid_meta_key] : '';

  if ( ! $new_value && ! $current_value ) {
    delete_post_meta($post->ID, $valid_meta_key);
    continue;
  } else if ( ! $new_value ) {
    continue;
  }

  // If $value is an array, make it a CSV (unlikely)
  if ( is_array( $new_value ) ) {
    $new_value = implode(',', $new_value);    
  }

  // Returns meta_id if the meta doesn't exist, otherwise returns true on success and false on failure. It also returns false if the value submitted is the same as the value that is already in the database. 
  update_post_meta($post->ID, $valid_meta_key, sanitize_text_field( $new_value ) );
}

$valid_taxonomies = array(
  'makes',
  'model_year',
  'body_type',
  'colors',
  'drivetrain',
  'transmissions',
  'fuel-type',
  'engine',  
);
foreach ( $valid_taxonomies as $valid_taxonomy ) {
  $new_value = isset( $_POST[$valid_taxonomy] ) ? intval( $_POST[$valid_taxonomy] ) : NULL;
  // Replace previous terms or clear with NULL $new_value
  wp_set_object_terms( $post->ID, $new_value, $valid_taxonomy, false );
}

Replace lines 597 - 610 with the code below. No need for a loop here as there's only one meta key.

$customer_name = isset( $_POST['_customer'] ) ? $_POST['_customer']: '';
if ( get_post_meta( $post->ID, '_customer', FALSE ) ) {
  update_post_meta($post->ID, '_customer', sanitize_text_field( $customer_name ) );
} else {
  delete_post_meta($post->ID, '_customer');
}

First answer

As Jacob Peattie pointed out in his comment, your question has quite a lot of code, which makes it rather difficult to review.

But based on the screenshots you've provided, you should dig into the theme files noted on the PHP warnings and add some guard clauses to the related lines. Here are some general examples,

Undefined index

// key is set in array and the value is not empty
if ( ! empty( $array['some_key'] ) ) {
  // some code
}

or

// key is set in array, but value can be empty
if ( isset( $array['some_key'] ) ) {
  // some code
}

argument should be an array or invalid argument supplied for foreach

// make sure the variable is array
if ( is_array( $something ) ) {
  // foreach loops
  // array_maps
}

undefined variable

// variable exists and is different than NULL
if ( isset( $variable ) ) {
  // some code
}

or

// variable exists and value equals FALSE
if ( ! empty( $variable ) ) {
  // some code
}

Do add fallbacks for ifs with elses, if needed. E.g.

  • add key to array with empty or some default value
  • set variable
  • cast variable to array
  • empty includes and isset check already. :) – fuxia Jun 27 at 9:43
  • @fuxia Yes, that is true. I improved the formatting of my answer to make it more clear that the examples are different cases. – Antti Koskinen Jun 27 at 12:26
  • I apologize for the mess, I have updated the screenshot above. And here is my Github where the files are that I am working with. Should be much easier to read now. github.com/pcross1986/car-dealership – Preston Jun 27 at 18:23
  • @Preston, thank you for moving the code to GitHub. I had a look at the metabox code and added new code examples to my answer. The warnings were caused by not having any isset() or empty() checks on the code. Also there was a mixture of post and term meta functions on the metabox saving function, which caused the undefined variable errors. In my answe I've separated the post meta saving and term setting into two different loops. – Antti Koskinen Jun 28 at 8:25
  • It looks like you are getting somewhere. After saving the new code, most of those errors went away, however it is still not saving data, and throws some errors now. I have updated a new screenshot above for the new errors showing after updating a listing/post. – Preston Jun 28 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.