filters - How to elect position of new item output in a dropdown when using add_filter

admin2025-06-03  3

Hello I am using the suggested snippet here to add a new term to a dropdown menu: /

which is:

add_filter( 'job_application_statuses', 'add_new_job_application_status' );

function add_new_job_application_status( $statuses ) {
    $statuses['example'] = _x( 'Example', 'job_application', 'wp-job-manager- 
applications' );
    return $statuses;
}

That works fine but my new single entry 'example' is output at the bottom of the dropdown list, I'd really like it to be in 2nd place. Is there a simple way to do this? Thank you.

Hello I am using the suggested snippet here to add a new term to a dropdown menu: https://wpjobmanager/document/applications-customising-application-statuses/

which is:

add_filter( 'job_application_statuses', 'add_new_job_application_status' );

function add_new_job_application_status( $statuses ) {
    $statuses['example'] = _x( 'Example', 'job_application', 'wp-job-manager- 
applications' );
    return $statuses;
}

That works fine but my new single entry 'example' is output at the bottom of the dropdown list, I'd really like it to be in 2nd place. Is there a simple way to do this? Thank you.

Share Improve this question edited Feb 18, 2019 at 15:24 Tom J Nowell 61.2k7 gold badges79 silver badges150 bronze badges asked Feb 18, 2019 at 15:18 Ed AEd A 154 bronze badges 2
  • I don't believe this is a WP question, but a general PHP question about arrays, see stackoverflow/questions/3353745/… – Tom J Nowell Commented Feb 18, 2019 at 15:25
  • Thanks Tom, I wasn't aware if there might be nuances to executing this in a Wordpress functions.php file for this WP Job Manager plugin Applications add-on. Sunny's solution below works well though. – Ed A Commented Feb 19, 2019 at 8:08
Add a comment  | 

1 Answer 1

Reset to default 0

You can achieve this by using the array_splice() function in conjunction with array_merge(). Here is an example:

function add_new_job_application_status( $statuses ) {
  return array_merge(
    array_splice( $statuses, 0, 1 ),
    array( 'example' => _x( 'Example', 'job_application', 'wp-job-manager- 
applications' ) ),
    array_splice( $statuses, 1, -1 ) 
  );
}
add_filter( 'job_application_statuses', 'add_new_job_application_status' );

This function takes the first item in the $statuses array, your custom example field, the rest of the items in the array and merges it together. You need to do it this way as splice doesn't support inserting a multi-dimensional array at a specific point in an array. Hope that helps.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748894167a314606.html

最新回复(0)