Compilazione e salvataggio di un form WordPress

← ritorno a WordPress

Salvataggio elemento

sanificare un numero inserito

        $attachment_id = 0;
        if( array_key_exists( 'attachment_id', $_POST ) && is_numeric( $_POST['attachment_id'] ) ) {
           $r_id = intval( $_POST['attachment_id'] );
           if( filter_var( $r_id, FILTER_VALIDATE_INT ) ) 
              $attachment_id = $r_id;
        }

sanificare un utente

     $user_id = 0;
     if( array_key_exists( 'user_id', $_POST ) ) {
        $sane_user_id = 0;
        if( is_numeric( $_POST['user_id'] ) ) {
          $r_id = intval( $_POST['user_id'] );
          if( filter_var( $r_id, FILTER_VALIDATE_INT ) ) {
            $sane_user_id = $r_id;
          }
        }
        // controllo utente
        if( $sane_user_id > 0 ) {
           $users = get_users();
           foreach( $users as $user )
              if( $sane_user_id == $user->ID ) {
                 $user_id = $user->ID;
                 break;
              }
        }
     }
     
        

sanificare un valore inserito

        $folder = null;
        if( array_key_exists( 'folder', $_POST ) && !empty( $_POST['folder'] ) && wp_unslash( $_POST['folder'] ) !== null ) {
           $folder = sanitize_text_field( $_POST['folder'] );
           if( !empty( $folder ) ) {
              $temps = array();
              foreach( explode( '|', $folder ) as $token ) if( !empty( $token ) ) {
                 $token = trim( $token );
                 if( !empty( $token ) ) $temps[] = $token;
              }
              $folder = !empty( $temps ) ? implode( '|', $temps ) : null;
           }
        }
     // inserimento di data e ora correnti in ofrmato mysql
     $elemento['modified'] = current_time( 'mysql' );
     
     
     
     if( $success ) {
        // aggiorno elemento esistente
        if( $update ) {
           $righe = $wpdb->update( $table, $elemento, array( 'ID' => $elemento['id'] ) );
           if( !empty( $righe ) ) {
              
           } else {
              $error_message = $wpdb->print_error();
              $success = false;
           }
        }
        // inserisco nuovo
        else {
           $righe = $wpdb->insert( $table, $elemento );
           if( !empty( $righe ) ) {
              
           } else {
              $error_message = $wpdb->print_error();
              $success = false;
           }
        }
     }