Created
August 30, 2016 20:11
-
-
Save wpcrmsystem/5204a592461c14af31d9079fb337033a to your computer and use it in GitHub Desktop.
WP-CRM System Custom Fields and Meta Boxes
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 | |
/* Add a new meta box with your own custom field. | |
------------------------------------------------*/ | |
// Add a custom meta box to Contact edit pages. | |
// For more information, see https://developer.wordpress.org/reference/functions/add_meta_box/ | |
function extend_wpcrm_system() { | |
add_meta_box( 'wpcrm_custom_meta', __( 'Custom Meta', 'wp-crm-system-extend' ), 'wpcrmCustomMeta', 'wpcrm-contact', 'side', 'low' ); | |
} | |
add_action( 'wpcrm_system_custom_meta_boxes', 'extend_wpcrm_system' ); | |
// Add content to the new meta box | |
function wpcrmCustomMeta() { | |
global $post; | |
$custom_field = get_post_meta( $post->ID, '_wpcrm_test_custom_field', true ); ?> | |
<label for="_wpcrm_test_custom_field"><strong><?php _e('My Custom Field','wp-crm-systemtest-extend'); ?></strong></label><br /> | |
<input type="number" name="_wpcrm_test_custom_field" value="<?php echo $custom_field; ?>" /><br /> | |
<?php } | |
// Validate field input and update post_meta table with user's input | |
function wpcrm_save_test_data( $post_id ) { | |
$fields = array( '_wpcrm_test_custom_field' ); // if more than one field is added, add field name to this array | |
foreach ( $fields as $field ) { | |
if ( array_key_exists( $field, $_POST ) ) { | |
$value = $_POST[$field]; | |
if ( $field == '_wpcrm_test_custom_field' ) { | |
$safeinput = preg_replace("/[^0-9\.]/", "", $value); // this field is a number field, so only allow numeric input to be saved | |
} | |
// if other types of fields (text box, text area, select/radio, etc.) are used you will need to add validation for each type to ensure the correct data type is being saved. | |
update_post_meta( $post_id, $field, $safeinput ); // update the post_meta with a valid and validated value | |
} | |
} | |
} | |
add_action( 'save_post_wpcrm-contact', 'wpcrm_save_test_data', 1, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment