As the comments on the question indicated, this was basically a pile of garbage (my words, not theirs). There are a number of typos and inverted boolean checks. It was kind of a nightmare.
I managed to get the code working, and I don’t like unanswered questions, so I wanted to work through the bugs from the top down and point out the solution that I ended up with.
Problem Area One
The first issue exists in registering the metaboxes:
// Register the Jobs Metaboxes
function fg_registerJobsMetabox() {
/* Add meta boxes on the 'add_meta_boxes' hook. */
add_action( 'add_meta_boxes', 'fg_addJobMetaboxes' );
/* Save post meta on the 'save_post' hook. */
add_action( 'wp_insert_post_data', 'fg_saveJobsMeta', 10, 2); // <- THIS IS THE ERROR
}
add_action( 'load-post.php', 'fg_registerJobsMetabox' );
add_action( 'load-post-new.php', 'fg_registerJobsMetabox' );
In this case, the add_action()
call is incorrect. When using the wp_insert_post_data
hook, the function parameters of my fg_saveJobsMeta()
function were coming back with incorrect values.
Switching this to add_action( 'save_post', 'fg_saveJobsMeta', 10, 2)
resolved this error
Problem Area Two
The next problem exists in the fg_saveJobsMeta($postID, $post)
function:
function fg_saveJobsMeta($postID, $post) {
/* Get the post type object. */
$postType = get_post_type_object( $post->postType ); // <- THIS IS THE ERROR
/* Check if the current user has permission to edit the post. */
if ( !current_user_can( $postType->cap->edit_post, $postID ) )
return $postID;
[...]
}
the value of $postType
was always being set to null, because $post->postType
is incorrect. This should change to $post->post_type
as post_type
is the field on the actual post object.
Problem Area Three
The next issue is a little further down in the fg_saveJobsMeta()
function:
// Save the Job Description
$metaKey = 'fg_jobDesc';
/* Verify the nonce before proceeding. */
if ( verifyMetaNonce($metaKey) ) // <- THIS IS THE ERROR
return $postID;
/* Get the posted data and sanitize it for use as an HTML class. */
$jobDesc = ( isset( $_POST[$metaKey] ) ? sanitize_text_field( $_POST[$metaKey] ) : '' );
fg_saveMetaData($postID, $metaKey, $jobDesc);
There are two problems here.
verifyMetaNonce($metaKey)
is the incorrect function.
The correct function call is fg_verifyMetaNonce($metaKey)
. The function name is prefixed to reduce the chances of collision with other plugins and/or themes.
- The boolean check is backwards.
If fg_verifyMetaNonce($metaKey)
returns true
(meaning we have a valid nonce value), the function will return the $postID
and exit, but it should save.
The correct call in this case would be if (!fg_verifyMetaNonce($metaKey)
. This will only exit the save process if the nonce value fails validation.
These errors were repeated several times (once for each of the custom fields I identified).
Problem Area Four
In solving the issue, I discovered that I had additional problems in code that I did not post on the original question.
There were a few errors in setting up the actual metabox UI elements (once again, repeated for each custom field). I copied one of the fields below:
// Generate the Job Description Metaboxes
function fg_jobDescMetabox() { // <- ERROR HERE
wp_nonce_field( 'updating_job_meta', 'fg_jobDesc_nonce'); ?> // <- ERROR HERE
<p>
// ERROR HERE
<input class="widefat" type="text" name="fg_jobDesc" id="fg_jobDesc"
value="<?php echo esc_attr( get_post_meta( $post->ID, 'fg_jobDesc', true ) ); ?>" />
</p>
<?php }
The correct code is as follows:
// Generate the Job Description Metaboxes
function fg_jobDescMetabox($object, $box) {
// ^ This function actually receives two arguments.
wp_nonce_field( 'updating_job_meta', 'fg_jobDesc_nonce', true); ?>
// ^ The wp_nonce_field() call needed a third boolean argument
// Without this argument, the nonce value was never set on the request.
<p>
<input class="widefat" type="text" name="fg_jobDesc" id="fg_jobDesc"
value="<?php echo esc_attr( get_post_meta( $object->ID, 'fg_jobDesc', true ) ); ?>" />
// ^ the global `$post` variable is null here, so using it did me no good.
// I need to use the $object parameter passed into the function instead.
</p>
<?php }
As mentioned above, this issue was repeated for each of my custom fields, so I needed to fix it in several places.
I’ve looked through the horribly bugged code and have compared it to my working version several times now. I believe that this covers all of the issues and differences between the two files.