Skip to content

Instantly share code, notes, and snippets.

@tripflex
Last active June 15, 2023 21:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tripflex/042c01c66a2e7f3a13314964369e4841 to your computer and use it in GitHub Desktop.
Save tripflex/042c01c66a2e7f3a13314964369e4841 to your computer and use it in GitHub Desktop.
Set field default value based on selected package when using WP Job Manager Field Editor
<?php
add_filter( 'submit_job_form_fields', 'smyles_custom_default_per_package', 101 );
function smyles_custom_default_per_package( $fields ) {
$packages_config = array(
// The key should be the ID of the product (look at the ID when editing the product in the URL bar)
'11111' => array(
// Array key should be the exact meta key
'job_type' => 'freelance',
),
// So below, the product ID is 22222
'22222' => array(
// Array key should be the exact meta key
'job_type' => 'full-time',
),
);
$product_id = isset( $_REQUEST['wcpl_jmfe_product_id'] ) ? intval( $_REQUEST['wcpl_jmfe_product_id'] ) : '';
$job_package = isset( $_REQUEST['job_package'] ) ? sanitize_text_field( $_REQUEST['job_package'] ) : $product_id;
$job_id = isset( $_REQUEST['job_id'] ) ? intval( $_REQUEST['job_id'] ) : false;
// Product/Package Handling, get job_package from post meta
if ( $job_id && empty( $job_package ) && class_exists( 'WP_Job_Manager_Field_Editor_Package_WC' ) ) {
$job_package = WP_Job_Manager_Field_Editor_Package_WC::get_post_package_id( $job_id );
}
if ( ! empty( $job_package ) && array_key_exists( $job_package, $packages_config ) ) {
foreach ( (array) $packages_config[ $job_package ] as $meta_key => $default ) {
if( isset( $fields['job'], $fields['job'][ $meta_key ] ) ){
$fields['job'][$meta_key]['default'] = $default;
}
// Also check in company fields for the meta key field
if ( isset( $fields['company'], $fields['company'][ $meta_key ] ) ) {
$fields['company'][ $meta_key ]['default'] = $default;
}
}
}
return $fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment