There is a loop in the function I want to modify:
foreach ( $form_fields as $key => $val ) {
if ( 'menu_order' == $key ) {
if ( $gallery )
$order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ). "' /></div>";
else
$order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
unset( $form_fields['menu_order'] );
break;
}
}
This loop is looping through $form_fields array. if “menu_order” is the key, then, do something and break. Now I want to add one more key to do things as the above. So, I modified it as following:
foreach ( $form_fields as $key => $val ) {
if ( 'menu_order' == $key ) {
if ( $gallery )
$order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ). "' /></div>";
else
$order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
unset( $form_fields['menu_order'] );
}
if ( 'post_status' == $key ) {
if ( $gallery )
$status = "<div class='post_status'> <input class='post_status_input' type='text' id='attachments[$attachment_id][post_status]' name='attachments[$attachment_id][post_status]' value='" . esc_attr( $val['value'] ). "' /></div>";
else
$status = "<input type='hidden' name='attachments[$attachment_id][post_status]' value='" . esc_attr( $val['value'] ) . "' />";
unset( $form_fields['post_status'] );
break;
}
}
Please note that I removed the break in original code, add the break at the end of the loop. I think this should make the loop going till it finished my newly added block.
But this code doesn’t print the post_status input box as expected. Where did I go wrong in the loop?
Here’s a simple fix: Get rid of the loop. Since you know the keys, you don’t need it, just do this to access the array elements directly:
Do the same for the
'post_status'field.