Skip to content

Instantly share code, notes, and snippets.

@tripflex
Created January 3, 2017 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tripflex/fd767e5706d1f8fc6d5eba22ec72c6ec to your computer and use it in GitHub Desktop.
Save tripflex/fd767e5706d1f8fc6d5eba22ec72c6ec to your computer and use it in GitHub Desktop.
Save custom values from text field and add/insert as new taxonomy value with WP Job Manager (untested)
<?php
// Return data in correct format to be used with text field
add_action( 'submit_job_form_fields_get_job_data', 'smyles_get_custom_user_add_tax_data', 10, 2 );
function smyles_get_custom_user_add_tax_data( $data, $job ) {
// Update job_metakey with correct metakey, and job_metakey_tax with the taxonomy name for that metakey
$data[ 'job' ][ 'job_metakey' ]['value'] = implode( ', ', wp_get_object_terms( $job->ID, 'job_metakey_tax', array( 'fields' => 'names' ) ) );
return $data;
}
add_action( 'job_manager_update_job_data', array( $this, 'smyles_custom_user_add_tax_save' ), 10, 2 );
function smyles_custom_user_add_tax_save( $job_id, $values ) {
$tag_values = $values['job']['job_metakey'];
$metakey_taxonomy = 'job_metakey_tax';
if ( is_array( $tag_values ) ) {
$tags = array_map( 'absint', $tag_values );
} else {
$raw_tags = array_filter( array_map( 'sanitize_text_field', explode( ',', $tag_values ) ) );
// Loop tags we want to set and put them into an array
$tags = array();
foreach ( $raw_tags as $tag ) {
// Assume less than or equal to 3 characters is abreviation, capitalize it
if ( strlen( $tag ) <= 3 ) {
$tag = strtoupper( $tag );
} else {
$tag = strtolower( $tag );
}
$tags[] = $tag;
}
}
if ( ! empty( $tags ) ) {
wp_set_object_terms( $job_id, $tags, $metakey_taxonomy, false );
}
}
// Validate max tags on submission
add_filter( 'submit_job_form_validate_fields', array( $this, 'smyles_custom_user_add_tax_validate' ), 10, 3 );
function smyles_custom_user_add_tax_validate( $passed, $fields, $values ) {
$max = 3;
$tags = is_array( $values['job']['job_metakey'] ) ? $values['job']['job_metakey'] : array_filter( explode( ',', $values['job']['job_metakey'] ) );
if ( $max && sizeof( $tags ) > $max ){
return new WP_Error( 'validation-error', sprintf( __( 'Please enter no more than %d tags.'), $max ) );
}
return $passed;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment