Last active
August 21, 2020 23:21
-
-
Save tripflex/5a28072ea5c929d246f1f58550edeff5 to your computer and use it in GitHub Desktop.
Restrict max files uploaded based on user selected package for WP Job Manager Field Editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'submit_job_form_fields', 'smyles_custom_max_images_per_package', 101 ); | |
function smyles_custom_max_images_per_package( $fields ) { | |
$max_packages = 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 | |
'main_image' => 1, | |
// Another example meta key | |
'job_file_upload' => 1, | |
), | |
// So below, the product ID is 22222 | |
'22222' => array( | |
'main_image' => 2, | |
'job_file_upload' => 2, | |
), | |
'33333' => array( | |
'main_image' => 3, | |
'job_file_upload' => 3, | |
), | |
'44444' => array( | |
'main_image' => 4, | |
'job_file_upload' => 4, | |
), | |
); | |
$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, $max_packages ) ) { | |
// Example "job_file_upload" max uploads | |
// Uncomment code below to enable for example meta key | |
//if ( array_key_exists( 'job', $fields ) && array_key_exists( 'job_file_upload', $fields['job'] ) ) { | |
// $fields['job']['job_file_upload']['max_uploads'] = $max_packages[ $job_package ]['job_file_upload']; | |
//} | |
// main_image max uploads (the Listable main_image is under the company fields, not job fields) | |
if ( array_key_exists( 'company', $fields ) && array_key_exists( 'main_image', $fields['company'] ) ) { | |
$fields['company']['main_image']['max_uploads'] = $max_packages[ $job_package ]['main_image']; | |
} | |
} | |
return $fields; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'submit_job_form_fields', 'smyles_custom_max_images_per_package', 101 ); | |
function smyles_custom_max_images_per_package( $fields ) { | |
$max_packages = 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 | |
'main_image' => 1, | |
// Another example meta key | |
'job_file_upload' => 1, | |
), | |
// So below, the product ID is 22222 | |
'22222' => array( | |
'main_image' => 2, | |
'job_file_upload' => 2, | |
), | |
'33333' => array( | |
'main_image' => 3, | |
'job_file_upload' => 3, | |
), | |
'44444' => array( | |
'main_image' => 4, | |
'job_file_upload' => 4, | |
), | |
); | |
$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, $max_packages ) ) { | |
foreach ( (array) $max_packages[ $job_package ] as $meta_key => $max_value ) { | |
if( isset( $fields['job'], $fields['job'][ $meta_key ] ) ){ | |
$fields['job'][$meta_key]['max_uploads'] = $max_value; | |
} | |
// Also check in company fields for the meta key field | |
if ( isset( $fields['company'], $fields['company'][ $meta_key ] ) ) { | |
$fields['company'][ $meta_key ]['max_uploads'] = $max_value; | |
} | |
} | |
} | |
return $fields; | |
} |
Here's an example of doing this just for gallery_images
field:
https://gist.github.com/tripflex/c3cabb264ae00867a71dbb461dfd6303
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The second example file
functionsv2.php
does the same exact thing, except that one will check for the field injob
andcompany
fields and set the max uploads value (the first onefunctions.php
you specifically tell it which files to check)