Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. if ( ! wp_verify_nonce( $_POST['home_custom_metabox_nonce'], 'home_custom_metabox' ) ) {
  2. return;
  3. }
  4.  
  5. <?php
  6. // Cria a meta_box
  7. function home_metabox() {
  8.  
  9. // Tipos de post para a metabox
  10. $screens = array('page');
  11.  
  12. foreach ( $screens as $screen ) {
  13.  
  14. add_meta_box(
  15. 'home_metabox', // ID da Meta Box
  16. 'Opções da página home', // Título
  17. 'home_metabox_callback', // Função de callback
  18. $screen, // Local onde ela vai aparecer
  19. 'normal', // Contexto
  20. 'high' // Prioridade
  21. );
  22.  
  23. } // foreach
  24.  
  25. } // Cria a meta_box
  26. add_action( 'add_meta_boxes', 'home_metabox', 1 );
  27.  
  28. // Essa é a função que vai exibir os dados para o usuário
  29. function home_metabox_callback( $post ) {
  30.  
  31. // Adiciona um campo nonce para verificação posterior
  32. wp_nonce_field( 'home_metabox_callback', 'home_custom_metabox_nonce' );
  33.  
  34. // Configura os campos
  35. $_texto_botao = get_post_meta( $post->ID, '_texto_botao', true );
  36. $_link_botao = get_post_meta( $post->ID, '_link_botao', true );
  37.  
  38. echo 'Texto do botão:<br>';
  39. echo '<input type="text" name="_texto_botao" value="'.esc_html( $_texto_botao ).'"/><br>';
  40.  
  41. echo 'Link do botão:<br>';
  42. echo '<input type="text" name="_link_botao" value="'.esc_html( $_link_botao ).'"/>';
  43. }
  44.  
  45. function home_save_custom_metabox_data( $post_id ) {
  46. // Verifica o campo nonce
  47. if ( ! isset( $_POST['home_custom_metabox_nonce'] ) ) {
  48. return;
  49. }
  50.  
  51. // Verifica se o campo nonce é válido
  52. if ( ! wp_verify_nonce( $_POST['home_custom_metabox_nonce'], 'home_custom_metabox' ) ) {
  53. return;
  54. }
  55.  
  56. // Se o formulário ainda não foi enviado (estiver fazendo autosave)
  57. // não faremos nada
  58. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  59. return;
  60. }
  61.  
  62. // Verifica as permissões do usuário (mínimo: editar post).
  63. if ( isset( $_POST['post_type'] ) && 'contato' == $_POST['post_type'] ) {
  64.  
  65. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  66. return;
  67. }
  68. }
  69.  
  70. /* Perfeito, agora vamos aos campos. */
  71. $_texto_botao = isset( $_POST['_texto_botao'] ) ? $_POST['_texto_botao'] : null;
  72. $_texto_botao = sanitize_text_field( $_texto_botao );
  73.  
  74. $_link_botao = isset( $_POST['_link_botao'] ) ? $_POST['_link_botao'] : null;
  75. $_link_botao = sanitize_text_field( $_link_botao );
  76.  
  77. // Atualiza os dados no BD
  78. update_post_meta( $post_id, '_texto_botao', $_texto_botao );
  79. update_post_meta( $post_id, '_link_botao', $_link_botao );
  80. }
  81. add_action( 'save_post', 'home_save_custom_metabox_data' );
  82.  
  83. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement