-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwp-job-manager-fields.php
187 lines (167 loc) · 5.63 KB
/
wp-job-manager-fields.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* Plugin Name: Custom fields for WP Job Manager
* Plugin URI: https://github.com/astoundify/wp-job-manager-fields
* Description: An example plugin for adding custom fields to the WP Job Manager submission form.
* Author: Astoundify
* Author URI: http://astoundify.com
* Version: 1.0
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
class Astoundify_Job_Manager_Fields {
/**
* @var $instance
*/
private static $instance;
/**
* Make sure only one instance is only running.
*
* @since Custom fields for WP Job Manager 1.0
*
* @param void
* @return object $instance The one true class instance.
*/
public static function instance() {
if ( ! isset ( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Start things up.
*
* @since Custom fields for WP Job Manager 1.0
*
* @param void
* @return void
*/
public function __construct() {
$this->setup_globals();
$this->setup_actions();
}
/**
* Set some smart defaults to class variables.
*
* @since Custom fields for WP Job Manager 1.0
*
* @param void
* @return void
*/
private function setup_globals() {
$this->file = __FILE__;
$this->basename = plugin_basename( $this->file );
$this->plugin_dir = plugin_dir_path( $this->file );
$this->plugin_url = plugin_dir_url ( $this->file );
}
/**
* Hooks and filters.
*
* We need to hook into a couple of things:
* 1. Output fields on frontend, and save.
* 2. Output fields on backend, and save (done automatically).
*
* @since Custom fields for WP Job Manager 1.0
*
* @param void
* @return void
*/
private function setup_actions() {
/**
* Filter the default fields that ship with WP Job Manager.
* The `form_fields` method is what we use to add our own custom fields.
*/
add_filter( 'submit_job_form_fields', array( $this, 'form_fields' ) );
/**
* When WP Job Manager is saving all of the default field data, we need to also
* save our custom fields. The `update_job_data` callback is what does this.
*/
add_action( 'job_manager_update_job_data', array( $this, 'update_job_data' ), 10, 2 );
/**
* Filter the default fields that are output in the WP admin when viewing a job listing.
* The `job_listing_data_fields` adds the same fields to the backend that we added to the front.
*
* We do not need to add an additional callback for saving the data, as this is done automatically.
*/
add_filter( 'job_manager_job_listing_data_fields', array( $this, 'job_listing_data_fields' ) );
}
/**
* Add fields to the submission form.
*
* Currently the fields must fall between two sections: "job" or "company". Until
* WP Job Manager filters the data that passes to the registration template, these are the
* only two sections we can manipulate.
*
* You may use a custom field type, but you will then need to filter the `job_manager_locate_template`
* to search in `/templates/form-fields/$type-field.php` in your theme or plugin.
*
* @since Custom fields for WP Job Manager 1.0
*
* @param array $fields The existing fields
* @return array $fields The modified fields
*/
function form_fields( $fields ) {
$fields[ 'company' ][ 'company_office_morale' ] = array(
'label' => 'Office Morale', // The label for the field
'type' => 'text', // file, job-description (tinymce), select, text
'placeholder' => 'Happy, etc', // Placeholder value
'required' => true, // If the field is required to submit the form
'priority' => 3 // Where should the field appear based on the others
);
/**
* Repeat this for any additional fields.
*/
return $fields;
}
/**
* When the form is submitted, update the data.
*
* All data is stored in the $values variable that is in the same
* format as the fields array.
*
* @since Custom fields for WP Job Manager 1.0
*
* @param int $job_id The ID of the job being submitted.
* @param array $values The values of each field.
* @return void
*/
function update_job_data( $job_id, $values ) {
/** Get the value of our "morale" field. */
$morale = isset ( $values[ 'company' ][ 'company_office_morale' ] ) ? sanitize_text_field( $values[ 'company' ][ 'company_office_morale' ] ) : null;
/** By using an underscore in the meta key name, we can prevent this from being shown in the Custom Fields metabox. */
if ( $morale )
update_post_meta( $job_id, '_company_office_morale', $morale );
/**
* Repeat this process for any additional fields. Always escape your data.
*/
}
/**
* Add fields to the admin write panel.
*
* There is a slight disconnect between the frontend and backend at the moment.
* The frontend allows for select boxes, but there is no way to output those in
* the admin panel at the moment.
*
* @since Custom fields for WP Job Manager 1.0
*
* @param array $fields The existing fields
* @return array $fields The modified fields
*/
function job_listing_data_fields( $fields ) {
/**
* Add the field we added to the frontend, using the meta key as the name of the
* field. We do not need to separate these fields into "job" or "company" as they
* are all output in the same spot.
*/
$fields[ '_company_office_morale' ] = array(
'label' => 'Company Morale', // The field label
'placeholder' => 'Happy, etc', // The default value when adding via backend.
'type' => 'text' // text, textarea, checkbox, file
);
/**
* Repeat this for any additional fields.
*/
return $fields;
}
}
add_action( 'init', array( 'Astoundify_Job_Manager_Fields', 'instance' ) );