0

In the form I'm creating I have a multi-select that doesn't want to update. The debug log point me to this line of code but I'm just a noob WordPress-user so I have no idea what I'm supposed to do with it.

I've tried removing the form and using a new one and just using the plugin along with the standard Twenty Fifteen theme though nothing seems to work.

$stripslashes = $args['submitted'][ $key ];

PHP Notice: Undefined index: Kurser-K.H in /home3/thestva6/public_html/wp-content/plugins/ultimate-member/includes/core/um-actions-profile.php on line 271 PHP Notice: Undefined index: _um_row_1 in /home3/thestva6/public_html/wp-content/plugins/ultimate-member/includes/core/um-actions-profile.php on line 271

treyBake
  • 6,440
  • 6
  • 26
  • 57
  • Aside - the error doesn't match that line of code in the question; are you sure that's line 71 in *um-actions-profile.php* ? – CD001 Aug 01 '19 at 14:03

1 Answers1

0

You should check if index exists before trying to access it.

Use isset() function for checking - isset( $args['submitted'][ $key ] )

$stripslashes = isset( $args['submitted'][ $key ] ) ? $args['submitted'][ $key ] : '';

This will make sure that key exists in multidimensional array and if it does not exists it will return empty string. This will prevent notice

Aleksej
  • 469
  • 3
  • 7
  • There is no need to check for the first and then the second key, just use `isset($args['submitted'][ $key ])` and it will work as desired. – Felippe Duarte Aug 01 '19 at 14:08
  • If you are speaking of that exact line of code, then no that doesn't seem to exist. Am I supposed to paste that somewhere? – Elias_the_Geek Aug 01 '19 at 14:09
  • @FelippeDuarte Yes but just in case submitted key is not present in $args array, double checking won;t hurt – Aleksej Aug 01 '19 at 14:10
  • Yes it will "hurt" because it's uneccessary. – Felippe Duarte Aug 01 '19 at 14:11
  • @Elias_the_Geek I am speaking of undefined index notice, as reference if error like that happens, you should check if key is present in the array before trying to access it. Instead of directly accessing $args['submitted'][ $key ]; – Aleksej Aug 01 '19 at 14:12
  • So I should replace $args['submitted'][ $key ]; with isset($args['submitted'][ $key ])? – Elias_the_Geek Aug 01 '19 at 14:13
  • @FelippeDuarte Yes I agree, when you are sure the first key is present, it is unnecessary. I was just presenting him with the safe approach to multidimensional array without risk of breaking the app – Aleksej Aug 01 '19 at 14:16
  • @Aleksej test by yourself. There is no need to double check. PHP will not give any errors, notice, anything if the first key is not present. – Felippe Duarte Aug 01 '19 at 14:17
  • @FelippeDuarte Correct. That will suppress the notice. But for saving problem, maybe you can show us the code you are using for the form, so we can see more details. My guess is the name attribute might be the issue – Aleksej Aug 01 '19 at 14:17
  • @FelippeDuarte Yes indeed. I recall getting problems once I did not double check for keys. But I just tested it again, and you are right. I will correct the answer, thanks! – Aleksej Aug 01 '19 at 14:20
  • @Elias_the_Geek edit your post with the code – Felippe Duarte Aug 01 '19 at 14:23
  • I'll try again: `//validation of correct values from options in wp-admin $stripslashes = $args['submitted'][ $key ]; if ( is_string( $stripslashes ) ) { $stripslashes = stripslashes( $stripslashes ); } if ( in_array( $array['type'], array( 'select' ) ) && ! empty( $array['options'] ) && ! empty( $stripslashes ) && ! in_array( $stripslashes, array_map( 'trim', $array['options'] ) ) && ! $has_custom_source ) {continue;}` – Elias_the_Geek Aug 01 '19 at 14:29
  • @Elias_the_Geek if the key is not present, then your if conditions is failing at this point - ! empty( $stripslashes ). $key you are trying to access is not present in $args['submitted']. The origin of the problem is at the point where this array is getting values from – Aleksej Aug 01 '19 at 14:29
  • Well, this is the first time the code uses $key: `foreach ( $fields as $key => $array )` Could this be of use? – Elias_the_Geek Aug 01 '19 at 14:34
  • @Elias_the_Geek hm, if you are certain that $key is the parameter you should use for accessing the array. The format returned in your notice is very badly formatted for the array index. Maybe you can var_dump($array) and var_dump($args['submitted']); and check if your proper key is perhaps hidden deeper in the $array – Aleksej Aug 01 '19 at 14:38
  • Well I messed around a bit and the error undefined indexes have stopped popping up but now these pop-up instead: PHP Notice: Constant WP_CRON_LOCK_TIMEOUT already defined in /home3/thestva6/public_html/wp-config.php on line 92 PHP Notice: Constant AUTOSAVE_INTERVAL already defined in /home3/thestva6/public_html/wp-config.php on line 93 Constant WP_POST_REVISIONS already defined in /home3/thestva6/public_html/wp-config.php on line 94 Constant EMPTY_TRASH_DAYS already defined in /home3/thestva6/public_html/wp-config.php on line 95 – Elias_the_Geek Aug 01 '19 at 14:48
  • @Elias_the_Geek Were you trying to redefine these constants ? (WP_CRON_LOCK_TIMEOUT , etc). If you are not sure you can use the search through files option in your code editor and search for WP_CRON_LOCK_TIMEOUT , and other constants to see where are they defined besides wp-config. Default constant should be defined within any other file, or at least if ( ! defined('Constant_Name') ) should be checked – Aleksej Aug 01 '19 at 14:55