functions.php
<?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-zd.]{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;
}
No comments yet.