Conditions | 1 |
Paths | 1 |
Total Lines | 103 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
7 | function example_my_user_address( $wp_fields ) { |
||
8 | |||
9 | // Object type: User |
||
10 | $object_type = 'user'; |
||
11 | |||
12 | // Object name: n/a |
||
13 | $object_name = null; |
||
14 | |||
15 | // Form: User Edit Profile |
||
16 | $form_id = 'user-edit'; |
||
17 | |||
18 | ////////////////////// |
||
19 | // Section: Address // |
||
20 | ////////////////////// |
||
21 | |||
22 | $section_id = 'address'; |
||
23 | $section_args = array( |
||
24 | 'title' => __( 'Address', 'my-text-domain' ), // @todo Update text domain |
||
25 | 'form' => $form_id, |
||
26 | ); |
||
27 | |||
28 | $wp_fields->add_section( $object_type, $section_id, $object_name, $section_args ); |
||
29 | |||
30 | // Address Line 1 |
||
31 | $field_id = 'address_1'; |
||
32 | $field_args = array( |
||
33 | 'control' => array( |
||
34 | 'type' => 'text', |
||
35 | 'section' => $section_id, |
||
36 | 'label' => __( 'Address 1', 'my-text-domain' ), // @todo Update text domain |
||
37 | ), |
||
38 | ); |
||
39 | |||
40 | $wp_fields->add_field( $object_type, $field_id, $object_name, $field_args ); |
||
41 | |||
42 | // Address Line 2 |
||
43 | $field_id = 'address_2'; |
||
44 | $field_args = array( |
||
45 | 'control' => array( |
||
46 | 'type' => 'text', |
||
47 | 'section' => $section_id, |
||
48 | 'label' => __( 'Address 2', 'my-text-domain' ), // @todo Update text domain |
||
49 | ), |
||
50 | ); |
||
51 | |||
52 | $wp_fields->add_field( $object_type, $field_id, $object_name, $field_args ); |
||
53 | |||
54 | // City |
||
55 | $field_id = 'address_city'; |
||
56 | $field_args = array( |
||
57 | 'control' => array( |
||
58 | 'type' => 'text', |
||
59 | 'section' => $section_id, |
||
60 | 'label' => __( 'City', 'my-text-domain' ), // @todo Update text domain |
||
61 | ), |
||
62 | ); |
||
63 | |||
64 | $wp_fields->add_field( $object_type, $field_id, $object_name, $field_args ); |
||
65 | |||
66 | // State / Region |
||
67 | $field_id = 'address_state'; |
||
68 | $field_args = array( |
||
69 | 'control' => array( |
||
70 | 'type' => 'text', |
||
71 | 'section' => $section_id, |
||
72 | 'label' => __( 'State / Region', 'my-text-domain' ), // @todo Update text domain |
||
73 | // You could use 'select' type instead and then |
||
74 | // pass in all states in 'choices' option with array( 'TX' => 'Texas' ) |
||
75 | ), |
||
76 | ); |
||
77 | |||
78 | $wp_fields->add_field( $object_type, $field_id, $object_name, $field_args ); |
||
79 | |||
80 | // Zip / Postal Code |
||
81 | $field_id = 'address_zip'; |
||
82 | $field_args = array( |
||
83 | 'control' => array( |
||
84 | 'type' => 'text', |
||
85 | 'section' => $section_id, |
||
86 | 'label' => __( 'Zip / Postal Code', 'my-text-domain' ), // @todo Update text domain |
||
87 | ), |
||
88 | ); |
||
89 | |||
90 | $wp_fields->add_field( $object_type, $field_id, $object_name, $field_args ); |
||
91 | |||
92 | // Zip / Postal Code |
||
93 | $field_id = 'address_country'; |
||
94 | $field_args = array( |
||
95 | 'control' => array( |
||
96 | 'type' => 'select', |
||
97 | 'section' => $section_id, |
||
98 | 'label' => __( 'Country', 'my-text-domain' ), // @todo Update text domain |
||
99 | 'choices' => array( |
||
100 | 'US' => 'United States', |
||
101 | 'CA' => 'Canada', |
||
102 | // Add more here as needed, or use 'text' type instead for freeform |
||
103 | ), |
||
104 | ), |
||
105 | ); |
||
106 | |||
107 | $wp_fields->add_field( $object_type, $field_id, $object_name, $field_args ); |
||
108 | |||
109 | } |
||
110 | |||
111 | add_action( 'fields_register', 'example_my_user_address' ); |