Skip to content

Instantly share code, notes, and snippets.

@tripflex
Last active September 25, 2017 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tripflex/ea7da7a9da34a1d66ee8b006c78797c9 to your computer and use it in GitHub Desktop.
Save tripflex/ea7da7a9da34a1d66ee8b006c78797c9 to your computer and use it in GitHub Desktop.
WP Job Manager Field Editor add max selections support for Taxonomy Checklist field (only required for 1.7.5 or older, 1.7.6+ has this already integrated) MAKE SURE YOU READ ALL COMMENTS
<?php
// THIS IS JUST A SMALL UPDATE YOU NEED TO MAKE IN A FILE:
// JUST READ COMMENTS BELOW AND MAKE MINOR UPDATE YOURSELF
// EDIT THE FILE LOCATED AT /wp-content/plugins/wp-job-manager-field-editor/classes/integration.php
// and on LINE 328 inside the 'locate_template' function you will find this block of code:
switch ( $template_name ) {
case 'form-fields/term-checklist-field.php':
wp_enqueue_script( 'jmfe-term-checklist-field' );
break;
default:
if ( file_exists( WPJM_FIELD_EDITOR_PLUGIN_DIR . '/templates/' . $template_name ) ) {
/**
* Support customized theme template overrides
*
* Theme template structure should match /templates/THEMENAME/
*/
$theme_name = self::get_theme_name( TRUE, FALSE );
if ( is_string( $theme_name ) && ! empty( $theme_name ) && file_exists( WPJM_FIELD_EDITOR_PLUGIN_DIR . "/templates/{$theme_name}/" . $template_name ) ) {
$template = WPJM_FIELD_EDITOR_PLUGIN_DIR . "/templates/{$theme_name}/" . $template_name;
} else {
$template = WPJM_FIELD_EDITOR_PLUGIN_DIR . '/templates/' . $template_name;
}
}
break;
}
// ONCE YOU FIND THAT BLOCK OF CODE ABOVE (THE ORIGINAL BLOCK OF CODE)
// UPDATE THAT BLOCK OF CODE TO MATCH THIS BLOCK OF CODE BELOW:
switch ( $template_name ) {
// Allow for adding custom switch later on if needed, right now only default
default:
if( $template_name === 'form-fields/term-checklist-field.php' ){
wp_enqueue_script( 'jmfe-term-checklist-field' );
}
if ( file_exists( WPJM_FIELD_EDITOR_PLUGIN_DIR . '/templates/' . $template_name ) ) {
/**
* Support customized theme template overrides
*
* Theme template structure should match /templates/THEMENAME/
*/
$theme_name = self::get_theme_name( TRUE, FALSE );
if ( is_string( $theme_name ) && ! empty( $theme_name ) && file_exists( WPJM_FIELD_EDITOR_PLUGIN_DIR . "/templates/{$theme_name}/" . $template_name ) ) {
$template = WPJM_FIELD_EDITOR_PLUGIN_DIR . "/templates/{$theme_name}/" . $template_name;
} else {
$template = WPJM_FIELD_EDITOR_PLUGIN_DIR . '/templates/' . $template_name;
}
}
break;
}
<?php
// THIS IS JUST A SMALL UPDATE YOU NEED TO MAKE IN A FILE (DO NOT COPY ENTIRE CONTENTS OF THIS BLOCK LIKE OTHERS):
// JUST READ COMMENTS BELOW AND MAKE MINOR UPDATE YOURSELF
// EDIT THE FILE LOCATED AT /wp-content/plugins/wp-job-manager-field-editor/classes/admin/js.php
// and on LINE 611 find this block of code:
"term-checklist" => array(
"show" => array('taxonomy', 'default'),
"hide" => array('placeholder'),
"required" => array('taxonomy')
),
// UPDATE THAT FILE ADDING THE 'max_selected' field to show, the block of code should look like this after the update:
"term-checklist" => array(
"show" => array('taxonomy', 'default', 'max_selected'),
"hide" => array('placeholder'),
"required" => array('taxonomy')
),
<?php
// COPY THE ENTIRE CONTENTS OF THIS CODE BLOCK/FILE TO:
// wp-content/plugins/wp-job-manager-field-editor/templates/term-checklist-field.php
// YOU MUST CREATE A NEW FILE WITH THE EXACT FILENAME OF 'term-checklist-field.php' AND PLACE IT INSIDE
// wp-content/plugins/wp-job-manager-field-editor/templates/ DIRECTORY
/**
* Shows `checkbox` form fields in a list from a list on job listing forms.
*
* This template can be overridden by copying it to yourtheme/job_manager/form-fields/term-checklist-field.php.
*
* @see https://wpjobmanager.com/document/template-overrides/
* @author Automattic/Myles McNamara
* @package WP Job Manager Field Editor
* @category Template
* @version @@version
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
// Default should be empty string, only when max selected is defined should it have a custom string
$max_selected_class = '';
// Check for custom configurations (that require separate jQuery initalization)
if ( array_key_exists( 'max_selected', $field ) && ! empty( $field['max_selected'] ) ) {
$max_selected = $field['max_selected'];
$esc_key = esc_attr( $key );
// Set to specific class to allow max selected jQuery handling
$max_selected_class = 'jmfe-term-checklist-max-checked';
}
?>
<ul class="job-manager-term-checklist job-manager-term-checklist-<?php echo $key; ?> <?php echo $max_selected_class; ?>" data-meta-key="<?php echo $esc_key ?>" data-max-selected="<?php echo $max_selected; ?>">
<?php
require_once( ABSPATH . '/wp-admin/includes/template.php' );
if ( empty( $field['default'] ) ) {
$field['default'] = '';
}
$args = array(
'descendants_and_self' => 0,
'selected_cats' => isset( $field['value'] ) ? $field['value'] : ( is_array( $field['default'] ) ? $field['default'] : array( $field['default'] ) ),
'popular_cats' => false,
'taxonomy' => $field['taxonomy'],
'checked_ontop' => false
);
// Max selections handling for args
if( isset( $max_selected ) && ! empty( $max_selected ) ){
// If value has more than allowed selected, set selected_cats to max amount of values
if ( is_array( $args['selected_cats'] ) && ! empty( $args['selected_cats'] ) ) {
$args['selected_cats'] = array_slice( $args['selected_cats'], 0, $max_selected );
}
// Set description showing max selections (if custom one not specified)
if ( empty( $field['description'] ) ) {
$field['description'] = sprintf( __( 'Maximum selections: %s' ), $max_selected );
}
}
// $field['post_id'] needs to be passed via the args so we can get the existing terms
ob_start();
wp_terms_checklist( 0, $args );
$checklist = ob_get_clean();
echo str_replace( "disabled='disabled'", '', $checklist );
?>
</ul>
<?php if ( ! empty( $field['description'] ) ) : ?><small class="description"><?php echo $field['description']; ?></small><?php endif; ?>
// UPDATE THE EXISTING FILE term-checklist.min.js LOCATED AT
// wp-content/plugins/wp-job-manager-field-editor/assets/js/term-checklist.min.js
// WITH THE ENTIRE CONTENTS OF THIS CODE BLOCK/FILE
jQuery( function ( $ ) {
$( '.job-manager-term-checklist input' ).prop( 'disabled', false );
$( '.jmfe-term-checklist-max-checked' ).each( function ( index ) {
var meta_key = $( this ).data( 'meta-key' );
var max_selected = $( this ).data( 'max-selected' );
if ( meta_key && max_selected ) {
window[ meta_key + '_checked' ] = [];
window[ '$' + meta_key + '_check' ] = $( '.job-manager-term-checklist-' + meta_key + ' input[type=checkbox]' ).change( function () {
if ( this.value == - 1 && this.checked ) {
window[ '$' + meta_key + '_check' ].not( this ).prop( 'disabled', true ).prop( 'checked', false );
window[ meta_key + '_checked' ] = [];
} else {
window[ '$' + meta_key + '_check' ].prop( 'disabled', false );
window[ meta_key + '_checked' ].push( this );
window[ meta_key + '_checked' ] = $( window[ meta_key + '_checked' ] );
window[ meta_key + '_checked' ].prop( 'checked', false ).slice( - max_selected ).prop( 'checked', true );
}
} );
}
} );
} );
@tripflex
Copy link
Author

Most of the updates are just small code changes in file, the ONLY file you need to create is the term-checklist-field.php all others should already exist.

The only file that will be updated with everything in the code block, is the term-checklist-min.js file ... all other files/updates are minor code block changes, read comments for all details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment