Advertisement
Guest User

Untitled

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