Skip to content

Instantly share code, notes, and snippets.

@johnpbloch
Created November 21, 2011 20:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnpbloch/1383772 to your computer and use it in GitHub Desktop.
Save johnpbloch/1383772 to your computer and use it in GitHub Desktop.
Short Circuit a post save in WordPress and give the user a notice saying why.
<?php
namespace JPB;
function wp_insert_post_data( $data, $postarr ){
if($data['foo'] !== 'bar' || $data['baz'] !== 'bat'){
wp_redirect( add_query_arg( array( 'post' => $data['ID'], 'action' => 'edit', 'message' => 11 ), admin_url( 'post.php' ) ) );
exit;
}
return $data;
}
add_filter( 'wp_insert_post_data', '\\JPB\\wp_insert_post_data', 10, 2 );
function messages( $messages ){
$messages[11] = 'You did not fill my two fields out!';
return $messages;
}
add_filter( 'post_updated_messages', '\\JPB\\messages' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment