Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created June 5, 2014 12:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/ab83b2bc517f9a555e52 to your computer and use it in GitHub Desktop.
Save tommcfarlin/ab83b2bc517f9a555e52 to your computer and use it in GitHub Desktop.
[WordPress] Display a native WordPress error message using add_settings_error.
<?php
add_action( 'save_post', 'acme_save_post' );
function acme_save_post() {
// Verify the user has permission to save. Code consolidated for readability.
if ( acme_user_can_save( $post_id, 'acme_save_meta_data_nonce' ) ) {
// Make sure the meta box data isn't empty
if ( isset( $_POST['acme-input-field'] ) && 0 === strlen( trim( $_POST['acme-input-field'] ) ) ) {
// Define the settings error to display
add_settings_error(
'invalid-email',
'',
'You must define a value in the meta box.',
'error'
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment