Adding fields to admin quick edit menu
-
I made a function for adding extra fields to the quick edit menu, decided to share it here to save others the trouble of having to write their own from scratch:
function genomics_add_custom_quick_edit_field($postType,$fieldArray = array()) { foreach($fieldArray as $fieldRow) { $fieldName = $fieldRow['name']; $fieldLabel = $fieldRow['label']; $fieldType = $fieldRow['type']; // CREATE HTML FIELDS add_filter('quick_edit_custom_box', function($column_name, $post_type) use($fieldName,$fieldLabel,$fieldType) { if ($column_name != $fieldName) return; $screen = get_current_screen(); $post_type = $screen->post_type; $nonce_name = 'qe_' . $fieldName . '_noncename'; $field_id = 'qe_' . $fieldName; echo '<fieldset class="inline-edit-col-left"> <div class="inline-edit-col"> <span class="title">' . $fieldLabel . '</span> <input id="' . $nonce_name . '" type="hidden" name="' . $nonce_name . '" value="" />'; if ($fieldType == 'text') { echo '<input id="' . $field_id . '" type="text" name="' . $fieldName . '" value=""/>'; } elseif ($fieldType == 'select') { $genesPod = pods('gene',array()); $optionsList = array(); while($genesPod->fetch()) { $geneID = $genesPod->field('ID'); $geneName = $genesPod->field('title'); $optionsList[] = '<option value="'.$geneID.'">'.$geneName.'</option>'; } $optionsString = implode("\n",$optionsList); echo '<select id="' . $field_id . '" name="' . $fieldName . '" value=""/> '.$optionsString.' </select>'; } echo ' </div> </fieldset> '; } , 10, 2); // ADD FILTER BY CATEGORY DROPDOWN MENU add_filter('save_post', function($post_id) use($postType,$fieldName) { // verify if this is an auto save routine. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id; // Check permissions if ( $postType == $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id ) ) return $post_id; } else { if ( !current_user_can( 'edit_post', $post_id ) ) return $post_id; } // Authentication passed now we save the data if (isset($_POST[$fieldName]) && ($post->post_type != 'revision')) { $my_fieldvalue = esc_attr($_POST[$fieldName]); if ($my_fieldvalue) update_post_meta( $post_id, $fieldName, $my_fieldvalue); else delete_post_meta( $post_id, $fieldName); } return $my_fieldvalue; }); add_filter('admin_footer', function() use($postType,$fieldName,$fieldType) { global $current_screen; //if (($current_screen->post_type != 'mytype')) return; $field_id = 'qe_' . $fieldName; echo '<script type="text/javascript"> console.log('.field_id.'); function set_' . $fieldName . '_value(fieldValue, nonce) { // refresh the quick menu properly inlineEditPost.revert(); console.log(fieldValue);'; if ($fieldType == 'text') { echo 'jQuery("#' . $field_id . '").val(fieldValue);'; } elseif ($fieldType == 'select') { echo ' var selectMenu = jQuery("#'.$field_id.'"); var selectedField = jQuery("#'.$field_id.' option").filter("[value="+fieldValue+"]"); var selectedOption = jQuery("<option>", { value: 1, text: "Gene", selected: true }); selectMenu.append(selectedOption); //selectedField.prop("selected",false).prop("selected",true); '; } echo '} </script>'; }); } // END FOREACH add_filter('post_row_actions', function($actions, $post) use($postType,$fieldArray) { $fieldName = $fieldArray[0]['name']; global $current_screen; //return $actions; $nonce = wp_create_nonce( 'nonce_' . $fieldName . '_'.$post->ID); $onclickString = ''; foreach($fieldArray as $fieldRow) { $fieldName = $fieldRow['name']; if ($fieldRow['type'] == 'select') { $myfielvalue = get_post_meta( $post->ID, $fieldName, TRUE)['ID']; //$myfielvalue = $fieldRow['ID']; } else { $myfielvalue = get_post_meta( $post->ID, $fieldName, TRUE); } $onclickString .= "set_{$fieldName}_value('{$myfielvalue}'); "; } $actions['inline hide-if-no-js'] = '<a href="#" class="editinline" title="'; $actions['inline hide-if-no-js'] .= esc_attr( __( 'Edit this item inline' ) ) . '"'; $actions['inline hide-if-no-js'] .= " onclick=\"{$onclickString}\" >"; $actions['inline hide-if-no-js'] .= __( 'Quick Edit' ); $actions['inline hide-if-no-js'] .= '</a>'; return $actions; }, 10, 2); }
So to call the function, you need to supply it an array containing all the fields you wanna add, so like this:
$fieldsArray = array( array( 'name' => 'importance', 'label' => 'Importance', 'type' => 'text', 'length' => 25 ), array( 'name' => 'genotypes', 'label' => 'Genotypes', 'type' => 'text', 'length' => 25 ) ); add_custom_quick_edit_fields('CUSTOM_POST',$fieldsArray);
- The topic ‘Adding fields to admin quick edit menu’ is closed to new replies.