Adding a Client Custom Field
Adding the Phone Number Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // don't add this line since it's already in your functions.php file | |
/** | |
* Add the client field to the meta box in the admin | |
* @param array $fields | |
*/ | |
function add_additional_client_field( $fields = array() ) { | |
$client_id = get_the_ID(); | |
if ( SI_Client::POST_TYPE !== get_post_type( $client_id ) ) { | |
return $fields; | |
} | |
$client = SI_Client::get_instance( $client_id ); | |
$fields['phone'] = array( | |
'weight' => 120, | |
'label' => __( 'Phone', 'sprout-invoices' ), | |
'type' => 'text', | |
'required' => false, | |
'default' => ( $client ) ? $client->get_phone() : '', | |
); | |
return $fields; | |
} | |
add_filter( 'si_client_form_fields', 'add_additional_client_field' ); | |
/** | |
* Save the field as a client meta | |
* @param array $data | |
* @param array $post | |
* @return $data | |
*/ | |
function save_client_field( $data = array(), $post = array() ) { | |
if ( $post['post_type'] !== SI_Client::POST_TYPE ) { | |
return $data; | |
} | |
$phone = ''; | |
if ( isset( $_POST['sa_metabox_phone'] ) && $_POST['sa_metabox_phone'] !== '' ) { | |
$phone = $_POST['sa_metabox_phone']; | |
} | |
$client = SI_Client::get_instance( $post['ID'] ); | |
if ( ! is_a( $client, 'SI_Client' ) ) { | |
return $data; | |
} | |
$client->set_phone( $phone ); | |
return $data; | |
} | |
add_filter( 'wp_insert_post_data', 'save_client_field', 100, 2 ); | |
/** | |
* Add the phone to the template | |
* @return string html | |
*/ | |
function si_maybe_add_phone_to_docs() { | |
$client_id = 0; | |
$doc_id = get_the_id(); | |
if ( get_post_type( $doc_id ) == SI_Invoice::POST_TYPE ) { | |
$client_id = si_get_invoice_client_id(); | |
} | |
if ( get_post_type( $doc_id ) == SI_Estimate::POST_TYPE ) { | |
$client_id = si_get_estimate_client_id(); | |
} | |
if ( $client_id ) { | |
$client = SI_Client::get_instance( $client_id ); | |
printf( __( '<dl class="client_addy"><dt><span class="dt_heading">Phone</span></dt><dd>%s</dd></dl>', 'sprout-invoices' ), $client->get_phone() ); | |
} | |
} | |
add_action( 'si_document_vcards', 'si_maybe_add_phone_to_docs' ); |