Skip to content

Instantly share code, notes, and snippets.

@tripflex
Created September 12, 2016 21:41
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/6ec0cb449a8a1817312709098ab5a981 to your computer and use it in GitHub Desktop.
Save tripflex/6ec0cb449a8a1817312709098ab5a981 to your computer and use it in GitHub Desktop.
WP Job Manager Field Editor output Facebook and Instagram fields with @USERNAME label
<?php
/**
* When setting up the custom field, set it as a Text Box field type, click on the
* "Advanced" tab on the left, and enter the pattern below in the "Pattern" input box.
*
* This assumes that your meta key for Facebook is "company_facebook" and for instagram, is
* "company_instagram" ... if it's not, change every instance you see below to match.
*
* The patterns below will prevent the user from entering the URL, and only allow usernames
*
* Twitter Username Pattern (backwards compatible with old usernames): ^[A-Za-z0-9_]{1,32}$
* Facebook Username Pattern: ^[a-z\d\.]{5,}$
*
* General only allow alpha numeric: [a-zA-Z0-9]+
* General username with 2-20 characters: ^[a-zA-Z][a-zA-Z0-9-_\.]{1,20}$
*/
add_filter( 'field_editor_output_as_value_company_facebook', 'output_company_facebook_only_show_username', 10, 4 );
add_filter( 'field_editor_output_as_value_company_instagram', 'output_company_instagram_only_show_username', 10, 4 );
add_filter( 'field_editor_output_as_args_company_facebook', 'output_only_show_username_args', 10, 4 );
add_filter( 'field_editor_output_as_args_company_instagram', 'output_only_show_username_args', 10, 4 );
/**
* Remove URL and only output Username as caption
*
* Because our filter for the value is before the arguments, we need to remove
* the URL from the value, and then set the caption as that value (with URL removed).
*
* @param $args
* @param $value
* @param $listing_id
*
* @return array
*/
function output_only_show_username_args( $args, $value, $listing_id ){
if( ! is_array( $args ) ) return $args;
$username = str_replace( 'http://instagram.com/', '', $value );
$username = str_replace( 'http://facebook.com/', '', $username );
$args[ 'output_caption' ] = "@{$username}";
return $args;
}
/**
* Add Facebook URL to field value
*
*
* @param $value
* @param $slug
* @param $listing_id
* @param $args
*
* @return string
*/
function output_company_facebook_only_show_username( $value, $slug, $listing_id, $args ){
if( ! empty( $value ) ){
$value = 'http://facebook.com/' . $value;
}
return $value;
}
/**
* Add Instagram URL to field value
*
*
* @param $value
* @param $slug
* @param $listing_id
* @param $args
*
* @return string
*/
function output_company_instagram_only_show_username( $value, $slug, $listing_id, $args ){
if( ! empty( $value ) ){
$value = 'http://instagram.com/' . $value;
}
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment