{"id":4663,"date":"2020-06-14T19:33:53","date_gmt":"2020-06-15T02:33:53","guid":{"rendered":"https:\/\/codedcommerce.com\/?post_type=product&#038;p=4663"},"modified":"2023-10-08T20:14:03","modified_gmt":"2023-10-09T03:14:03","slug":"user-profile-add-custom-fields","status":"publish","type":"woo-code","link":"https:\/\/codedcommerce.com\/woo\/user-profile-add-custom-fields\/","title":{"rendered":"User profile: add custom fields"},"content":{"rendered":"\n<pre class=\"wp-block-code language-php\"><code>\/\/ Display Hooks For User Edit\nadd_action( 'show_user_profile', function( $user ) {\n\n\tdo_action( 'ccom_profile_fields', $user );\n\n} );\n\nadd_action( 'edit_user_profile', function( $user ) {\n\n\tdo_action( 'ccom_profile_fields', $user );\n\n} );\n\n\/\/ Display Logic For User Edit\nadd_action( 'ccom_profile_fields', function( $user ) {\n\n\t\/\/ Get Field Value\n\t$ccom_custom_field = get_user_meta(\n\t\t$user->ID, 'ccom_custom_field', true\n\t);\n\n\t\/\/ Show Field\n\tprintf(\n\t\t'\n\t\t\t&lt;table class=\"form-table\">\n\t\t\t\t&lt;tr>\n\t\t\t\t\t&lt;th>&lt;label for=\"ccom_custom_field\">%s&lt;\/label>&lt;\/th>\n\t\t\t\t\t&lt;td>\n\t\t\t\t\t\t&lt;input type=\"text\" id=\"ccom_custom_field\" name=\"ccom_custom_field\" value=\"%s\" \/>\n\t\t\t\t\t&lt;\/td>\n\t\t\t\t&lt;\/tr>\n\t\t\t&lt;\/table>\n\t\t',\n\t\t'Custom Field',\n\t\t$ccom_custom_field\n\t);\n\n} );\n\n\/\/ My Account Edit Profile Form\nadd_action( 'woocommerce_edit_account_form', function() {\n\n\t\/\/ Get User Object\n\t$user = wp_get_current_user();\n\n\t?>\n\n\t&lt;p class=\"woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide\">\n\t\t&lt;label for=\"favorite_color\">Custom Field&lt;\/label>\n\t\t&lt;input type=\"text\"\n\t\t\tclass=\"woocommerce-Input woocommerce-Input--text input-text\"\n\t\t\tname=\"ccom_custom_field\" id=\"ccom_custom_field\"\n\t\t\tvalue=\"&lt;?php echo esc_attr( $user->ccom_custom_field ); ?>\" \/>\n\t&lt;\/p>\n\n\t&lt;?php\n\n} );\n\n\/\/ My Account Edit Profile Save\nadd_action( 'woocommerce_save_account_details', function( $user_id ) {\n\n\t\/\/ Maybe Update Field\n\tif( isset( $_POST&#91;'ccom_custom_field'] ) ) {\n\t\tupdate_user_meta(\n\t\t\t$user_id,\n\t\t\t'ccom_custom_field',\n\t\t\tesc_attr( $_POST&#91;'ccom_custom_field'] )\n\t\t);\n\t}\n\n} );\n\n\/\/ Profile Update Hooks\nadd_action( 'personal_options_update', function( $user_id ) {\n\n\tdo_action( 'ccom_profile_update', $user_id );\n\n} );\n\nadd_action( 'edit_user_profile_update', function( $user_id ) {\n\n\tdo_action( 'ccom_profile_update', $user_id );\n\n} );\n\n\/\/ Profile Update Logic\nadd_action( 'ccom_profile_update', function( $user_id ) {\n\n\t\/\/ Security Check\n\tif ( ! current_user_can( 'edit_user', $user_id ) ) {\n\t\treturn false;\n\t}\n\n\t\/\/ Maybe Update Field\n\tif( isset( $_POST&#91;'ccom_custom_field'] ) ) {\n\t\tupdate_user_meta(\n\t\t\t$user_id,\n\t\t\t'ccom_custom_field',\n\t\t\tesc_attr( $_POST&#91;'ccom_custom_field'] )\n\t\t);\n\t}\n\n} );<\/code><\/pre>\n","protected":false},"template":"","class_list":["post-4663","woo-code","type-woo-code","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/woo-code\/4663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/woo-code"}],"about":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/types\/woo-code"}],"wp:attachment":[{"href":"https:\/\/codedcommerce.com\/wp-json\/wp\/v2\/media?parent=4663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}