Skip to content

Instantly share code, notes, and snippets.

@tripflex
Created August 8, 2019 19:04
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/e031d7dd2d958bce6fc0b3b3272d7a72 to your computer and use it in GitHub Desktop.
Save tripflex/e031d7dd2d958bce6fc0b3b3272d7a72 to your computer and use it in GitHub Desktop.
Prepend URL scheme (http/https) to value saved when submitting a listing in WP Job Manager (if missing in field value filled out by user)
<?php
add_filter( 'job_manager_get_posted_fields', 'smyles_set_url_scheme_if_not_exists', 10, 2 );
function smyles_set_url_scheme_if_not_exists( $values, $fields ){
// Enter any Job, Company, or Resume fields you want to make sure have http or https in the field value before being saved
$url_fields = array( 'job_field_with_url', 'resume_field_with_url' );
// Set default scheme to http://
$scheme = 'http://';
foreach( (array) $values as $group_key => $meta_keys ){
foreach( (array) $meta_keys as $meta_key => $meta_value ){
// If not one of our URL fields, or has no value, continue to next
if ( ! in_array( $meta_key, $url_fields ) || empty( $meta_value ) ) {
continue;
}
// If already has valid scheme, continue to next
if( parse_url( $meta_value, PHP_URL_SCHEME ) !== null ){
continue;
}
// Make sure to remove // from front of value if set (means value looks like //somedomain.com/something)
$value = substr( $meta_value, 0, 2 ) === '//' ? substr( $meta_value, 2 ) : $meta_value;
// Prepend scheme to front of field value
$values[ $group_key ][ $meta_key ] = $scheme . $value;
}
}
return $values;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment