Use the Search Parameter in the Table Search Routine
Finally, the specified search parameter is used to control the actual search functionality within the display function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | /* * If the search parameter is defined and not blank, and * if the search value does not contain any invalid characters (used by the preg_match routine) and * if the search column parameter is set (which should always be set) * then select only the table data entries that match the search and search column parameters */ if ( ( isset( $_REQUEST[ ‘s’ ] ) ) && ( ” !== $_REQUEST[ ‘s’ ] ) && ( ! preg_match( ‘/({})/’, $_REQUEST[ ‘s’ ] ) ) && ( isset( $_REQUEST[ ‘sc’ ] ) ) ) { $array = array(); $columns = explode( ‘,’, $_REQUEST[ ‘sc’ ] ); /* * Search each entry in $data which contains the table contents * Each entry is an array structure, with the primary key being the timestamp of the occurrence */ foreach ( $data as $item ) { /* * Search all column keys as defined from the dropdown box by default */ foreach ( $columns as $key ) { /* * The time entry is stored as an integer but displayed as text * so it is converted to a time text for searching */ if ( ‘time’ === $key ) { if ( preg_match( ( ‘{‘ . $_REQUEST[ ‘s’ ] . ’}’ ), gmdate( ‘Y/m/d H:i:s’, $item[ $key ] ) ) ) { $array[ $item[ ‘time’ ] ] = $item; break; } } /* * Otherwise search the actual item entry for the given search criteria */ else { if ( preg_match( ( ‘{‘ . $_REQUEST[ ‘s’ ] . ’}’ ), $item[ $key ] ) ) { $array[ $item[ ‘time’ ] ] = $item; break; } } } } /* * Store the temporary array into the actual data array */ $data = $array; } |
This code segment is based on the sample WP_Table code, with the addition of the internal search loop for all the search column keys, and so replaces that segment in the overall ‘prepare_items()’ function in the class.
The short URL of the present article is: http://www.terryobrien.me/Oodqa
Personal Links