5

javascriptを使用せずにカスタムメタボックスフィールドを検証する方法はありますか?検証されない場合は、投稿がデータベースに保存されないようにします。

4

3 に答える 3

3

'save_post'アクションは公開および更新後に実行されるため、ハック的な代替手段なしにカスタムキーを検証する方法は実際にはありません。

ただし、Viralが提案した方法で「save_post」を使用することで、必要な機能を模倣できると思いますが、検証エラー時に保存プロセスを中断またはキャンセルするのではなく、投稿を完全に削除できます。

add_action('save_post', 'validate_meta');
function validate_meta($post_id)
{
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
      return $post_id;
    /*USE THIS ONLY IF YOU ARE UTILIZING NONCE FIELDS IN A CUSTOM META BOX
    if ( !wp_verify_nonce( $_POST['metabox_nonce'], basename(__FILE__) ) )
      return $post_id;*/
    /*Use plugin_basename(__FILE__) if this is an actual plugin, rather than
    a part of your theme*/

    if ( 'page' == $_POST['post_type'] ) 
    {
        if ( !current_user_can( 'edit_page', $post_id ) )
            return $post_id;
    }
    else
    {
        if ( !current_user_can( 'edit_post', $post_id ) )
            return $post_id;
    }

    /*VALIDATE YOUR METADATA HERE HOWEVER YOU LIKE
    if(is_valid($_POST['metadata']))
        $validated = true;
    else
        $validated = false;
    */

    if(!$validated)
        wp_delete_post($post_id, true);
    else
        return $post_id;
}

このアプローチで注意する必要があるのは、公開と更新の両方で実行されることだけです。新しく公開された投稿に対してのみ投稿が削除され、更新された投稿が以前のバージョンにロールバックされ、無効なリビジョンが削除されるように、チェックを追加することを検討してください。

于 2012-08-13T14:50:16.697 に答える
1

フィルターはwp_insert_post_dataあなたが探しているものです。このような何かがうまくいくはずです:

add_filter( 'wp_insert_post_data', 'my_validation_function' );

function my_validation_function( $data ) {
    // Don't want to do this on autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $data;
    if ( $data['some_key'] != 'some_value' ||
         $_POST['some_meta_key'] != 'some_meta_value' ) {
        $data['post_status'] = 'draft'; // or whatever status to revert to
        add_filter( 'redirect_post_location', 'remove_message'); // remove the publish success message
    }
    return $data;
}

function remove_message( $location ) {
    return remove_query_arg( 'message', $location);
}
于 2014-04-22T18:53:33.820 に答える
0

WP Codex @ http://codex.wordpress.org/Function_Reference/add_meta_boxから直接、フックを呼び出してsave_post、データを検証/保存するために実行される関数を指定します。

/* Do something with the data entered */
add_action('save_post', 'myplugin_save_postdata');

次に、その関数を定義すると、投稿 ID が自動的に渡されます。さらに、 $_POST 配列にアクセスして、メタボックスの値を取得できます。

/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
      return $post_id;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times

  if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) ) )
      return $post_id;


  // Check permissions
  if ( 'page' == $_POST['post_type'] ) 
  {
    if ( !current_user_can( 'edit_page', $post_id ) )
        return $post_id;
  }
  else
  {
    if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;
  }

  // OK, we're authenticated: we need to find and save the data

  $mydata = $_POST['myplugin_new_field'];

  // Do something with $mydata 
  // probably using add_post_meta(), update_post_meta(), or 
  // a custom table (see Further Reading section below)

   return $mydata;
}

有効なデータに対するすべてのルーチンは、この関数内で実行されます。最後に、次のような方法でデータを保存する可能性があります。 update_post_meta('meta_key', 'meta_value');

于 2012-08-08T05:21:09.167 に答える