$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>filters - How to elect position of new item output in a dropdown when using add_filter|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far