Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ( ! wp_verify_nonce( $_POST['home_custom_metabox_nonce'], 'home_custom_metabox' ) ) {
- return;
- }
- <?php
- // Cria a meta_box
- function home_metabox() {
- // Tipos de post para a metabox
- $screens = array('page');
- foreach ( $screens as $screen ) {
- add_meta_box(
- 'home_metabox', // ID da Meta Box
- 'Opções da página home', // Título
- 'home_metabox_callback', // Função de callback
- $screen, // Local onde ela vai aparecer
- 'normal', // Contexto
- 'high' // Prioridade
- );
- } // foreach
- } // Cria a meta_box
- add_action( 'add_meta_boxes', 'home_metabox', 1 );
- // Essa é a função que vai exibir os dados para o usuário
- function home_metabox_callback( $post ) {
- // Adiciona um campo nonce para verificação posterior
- wp_nonce_field( 'home_metabox_callback', 'home_custom_metabox_nonce' );
- // Configura os campos
- $_texto_botao = get_post_meta( $post->ID, '_texto_botao', true );
- $_link_botao = get_post_meta( $post->ID, '_link_botao', true );
- echo 'Texto do botão:<br>';
- echo '<input type="text" name="_texto_botao" value="'.esc_html( $_texto_botao ).'"/><br>';
- echo 'Link do botão:<br>';
- echo '<input type="text" name="_link_botao" value="'.esc_html( $_link_botao ).'"/>';
- }
- function home_save_custom_metabox_data( $post_id ) {
- // Verifica o campo nonce
- if ( ! isset( $_POST['home_custom_metabox_nonce'] ) ) {
- return;
- }
- // Verifica se o campo nonce é válido
- if ( ! wp_verify_nonce( $_POST['home_custom_metabox_nonce'], 'home_custom_metabox' ) ) {
- return;
- }
- // Se o formulário ainda não foi enviado (estiver fazendo autosave)
- // não faremos nada
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
- return;
- }
- // Verifica as permissões do usuário (mínimo: editar post).
- if ( isset( $_POST['post_type'] ) && 'contato' == $_POST['post_type'] ) {
- if ( ! current_user_can( 'edit_post', $post_id ) ) {
- return;
- }
- }
- /* Perfeito, agora vamos aos campos. */
- $_texto_botao = isset( $_POST['_texto_botao'] ) ? $_POST['_texto_botao'] : null;
- $_texto_botao = sanitize_text_field( $_texto_botao );
- $_link_botao = isset( $_POST['_link_botao'] ) ? $_POST['_link_botao'] : null;
- $_link_botao = sanitize_text_field( $_link_botao );
- // Atualiza os dados no BD
- update_post_meta( $post_id, '_texto_botao', $_texto_botao );
- update_post_meta( $post_id, '_link_botao', $_link_botao );
- }
- add_action( 'save_post', 'home_save_custom_metabox_data' );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement