Suporte » Desenvolvendo com WordPress » Problemas com Meta Box

  • Desculpe por não conseguir formatar corretamente os textos… Este é o meu primeiro tópico!! Estou com o seguinte problema!

    Notice: Undefined index: vendameta_noncename in /home/felipifranco/felipifranco.com/wp-content/themes/theron_lite/imoveis.php on line 74
    
    Notice: Undefined index: locacaometa_noncename in /home/felipifranco/felipifranco.com/wp-content/themes/theron_lite/imoveis.php on line 198
    
    Notice: Undefined index: vendameta_noncename in /home/felipifranco/felipifranco.com/wp-content/themes/theron_lite/imoveis.php on line 74
    
    Notice: Undefined index: locacaometa_noncename in /home/felipifranco/felipifranco.com/wp-content/themes/theron_lite/imoveis.php on line 198
    
    Warning: Cannot modify header information - headers already sent by (output started at /home/felipifranco/felipifranco.com/wp-content/themes/theron_lite/imoveis.php:74) in /home/felipifranco/felipifranco.com/wp-includes/pluggable.php on line 876

    Segue o código:

    <?php
    // Registers the new post type and taxonomy
    function wpt_venda_posttype() {
      register_post_type( 'vendas',
        array(
          'labels' => array(
            'name' => __( 'Vendas' ),
            'singular_name' => __( 'Venda' ),
            'add_new' => __( 'Add New Venda' ),
            'add_new_item' => __( 'Add New Venda' ),
            'edit_item' => __( 'Edit Venda' ),
            'new_item' => __( 'Add New Venda' ),
            'view_item' => __( 'View Venda' ),
            'search_items' => __( 'Search Venda' ),
            'not_found' => __( 'No vendas found' ),
            'not_found_in_trash' => __( 'No vendas found in trash' )
          ),
          'public' => true,
          'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
          'capability_type' => 'post',
          'rewrite' => array("slug" => "vendas"), // Permalinks format
          'menu_position' => 5,
          'has_archive'   => true,
          'register_meta_box_cb' => 'add_vendas_metaboxes'
        )
      );
    }
    add_action( 'init', 'wpt_venda_posttype' );
    
    // Add the Vendas Meta Boxes
    function add_vendas_metaboxes() {
      add_meta_box('wpt_vendas_location', 'Venda Location', 'wpt_vendas_location', 'vendas', 'side', 'default');
    }
    
    // The Venda Location Metabox
    function wpt_vendas_location() {
      global $post;
      // Noncename needed to verify where the data originated
      echo '<input type="hidden" name="vendameta_noncename" id="vendameta_noncename" value="' .
      wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
      // Get the location data if its already been entered
            $referencia = get_post_meta($post->ID, '_referencia', true);
            $finalidade = get_post_meta($post->ID, '_finalidade', true);
            $cidade = get_post_meta($post->ID, '_cidade', true);
            $dormitorios = get_post_meta($post->ID, '_dormitorios', true);
            $tipo = get_post_meta($post->ID, '_tipo', true);
            $bairro = get_post_meta($post->ID, '_bairro', true);
            $valor = get_post_meta($post->ID, '_valor', true);
            $area = get_post_meta($post->ID, '_area', true); 
    
      // Echo out the field
          echo '<p>Finalidade</p>';
        echo '<input type="text" name="_finalidade" value="' . $finalidade  . '" class="widefat" />';
            echo '<p>Tipo:</p>';
      echo '<input type="text" name="_tipo" value="' . $tipo  . '" class="widefat" />';
            echo '<p>Cidade:</p>';
      echo '<input type="text" name="_cidade" value="' . $cidade  . '" class="widefat" />';
            echo '<p>Bairro:</p>';
        echo '<input type="text" name="_bairro" value="' . $bairro  . '" class="widefat" />';
            echo '<p>Valor:</p>';
      echo '<input type="text" name="_valor" value="' . $valor  . '" class="widefat" />';
            echo '<p>Dormitórios:</p>';
        echo '<input type="text" name="_dormitorios" value="' . $dormitorios  . '" class="widefat" />';
            echo '<p>Área:</p>';
      echo '<input type="text" name="_area" value="' . $area  . '" class="widefat" />';
        echo '<p>Referência:</p>';
      echo '<input type="text" name="_referencia" value="' . $referencia  . '" class="widefat" />';
    }
    
    // Save the Metabox Data
    function wpt_save_vendas_meta($post_id, $post) {
      // 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['vendameta_noncename'], plugin_basename(__FILE__) )) {
      return $post->ID;
      }
      // Is the user allowed to edit the post or page?
      if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;
      // OK, we're authenticated: we need to find and save the data
      // We'll put it into an array to make it easier to loop though.
      $vendas_meta['_referencia'] = $_POST['_referencia'];
      $vendas_meta['_finalidade'] = $_POST['_finalidade'];
      $vendas_meta['_cidade'] = $_POST['_cidade'];
      $vendas_meta['_dormitorios'] = $_POST['_dormitorios'];
      $vendas_meta['_tipo'] = $_POST['_tipo'];
      $vendas_meta['_bairro'] = $_POST['_bairro'];
      $vendas_meta['_valor'] = $_POST['_valor'];
      $vendas_meta['_area'] = $_POST['_area'];
    
      // Add values of $vendas_meta as custom fields
      foreach ($vendas_meta as $key => $value) { // Cycle through the $vendas_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
          update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
          add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
      }
    }
    add_action('save_post', 'wpt_save_vendas_meta', 1, 2); // save the custom fields
    
    // Registers the new post type and taxonomy
    function wpt_locacao_posttype() {
      register_post_type( 'locacoes',
        array(
          'labels' => array(
            'name' => __( 'Locações' ),
            'singular_name' => __( 'Locação' ),
            'add_new' => __( 'Add New Locação' ),
            'add_new_item' => __( 'Add New Locação' ),
            'edit_item' => __( 'Edit Locação' ),
            'new_item' => __( 'Add New Locação' ),
            'view_item' => __( 'View Locação' ),
            'search_items' => __( 'Search Locação' ),
            'not_found' => __( 'No locações found' ),
            'not_found_in_trash' => __( 'No locacoes found in trash' )
          ),
          'public' => true,
          'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
          'capability_type' => 'post',
          'rewrite' => array("slug" => "locacoes"), // Permalinks format
          'menu_position' => 5,
          'has_archive'   => true,
          'register_meta_box_cb' => 'add_locacoes_metaboxes'
        )
      );
    }
    add_action( 'init', 'wpt_locacao_posttype' );
    
    // Add the Vendas Meta Boxes
    function add_locacoes_metaboxes() {
      add_meta_box('wpt_locacoes_location', 'Locacao Location', 'wpt_locacoes_location', 'locacoes', 'side', 'default');
    }
    
    // The Venda Location Metabox
    function wpt_locacoes_location() {
      global $post;
      // Noncename needed to verify where the data originated
      echo '<input type="hidden" name="locacaometa_noncename" id="locacaometa_noncename" value="' .
      wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
      // Get the location data if its already been entered
            $referencia = get_post_meta($post->ID, '_referencia', true);
            $finalidade = get_post_meta($post->ID, '_finalidade', true);
            $cidade = get_post_meta($post->ID, '_cidade', true);
            $dormitorios = get_post_meta($post->ID, '_dormitorios', true);
            $tipo = get_post_meta($post->ID, '_tipo', true);
            $bairro = get_post_meta($post->ID, '_bairro', true);
            $valor = get_post_meta($post->ID, '_valor', true);
            $area = get_post_meta($post->ID, '_area', true); 
    
      // Echo out the field
          echo '<p>Finalidade</p>';
        echo '<input type="text" name="_finalidade" value="' . $finalidade  . '" class="widefat" />';
            echo '<p>Tipo:</p>';
      echo '<input type="text" name="_tipo" value="' . $tipo  . '" class="widefat" />';
            echo '<p>Cidade:</p>';
      echo '<input type="text" name="_cidade" value="' . $cidade  . '" class="widefat" />';
            echo '<p>Bairro:</p>';
        echo '<input type="text" name="_bairro" value="' . $bairro  . '" class="widefat" />';
            echo '<p>Valor:</p>';
      echo '<input type="text" name="_valor" value="' . $valor  . '" class="widefat" />';
            echo '<p>Dormitórios:</p>';
        echo '<input type="text" name="_dormitorios" value="' . $dormitorios  . '" class="widefat" />';
            echo '<p>Área:</p>';
      echo '<input type="text" name="_area" value="' . $area  . '" class="widefat" />';
        echo '<p>Referência:</p>';
      echo '<input type="text" name="_referencia" value="' . $referencia  . '" class="widefat" />';
    }
    
    // Save the Metabox Data
    function wpt_save_locacoes_meta($post_id, $post) {
      // 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['locacaometa_noncename'], plugin_basename(__FILE__) )) {
      return $post->ID;
      }
      // Is the user allowed to edit the post or page?
      if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;
      // OK, we're authenticated: we need to find and save the data
      // We'll put it into an array to make it easier to loop though.
      $locacoes_meta['_referencia'] = $_POST['_referencia'];
      $locacoes_meta['_finalidade'] = $_POST['_finalidade'];
      $locacoes_meta['_cidade'] = $_POST['_cidade'];
      $locacoes_meta['_dormitorios'] = $_POST['_dormitorios'];
      $locacoes_meta['_tipo'] = $_POST['_tipo'];
      $locacoes_meta['_bairro'] = $_POST['_bairro'];
      $locacoes_meta['_valor'] = $_POST['_valor'];
      $locacoes_meta['_area'] = $_POST['_area'];
    
      // Add values of $vendas_meta as custom fields
      foreach ($locacoes_meta as $key => $value) { // Cycle through the $vendas_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
          update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
          add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
      }
    }
    add_action('save_post', 'wpt_save_locacoes_meta', 1, 2); // save the custom fields
    ?>
Visualizando 5 respostas - 1 até 5 (de um total de 5)
  • As 4 primeiras mensagens dizem que o PHP está tentando acessar um valor $_POST['xyz'] que não foi enviado. Tipo, ele está tentando ler, mas o post não tem isso. Confira se o formulário está enviando esses valores mesmo, e se o seu plugin só tenta acessá-los na hora certa (e não o tempo todo).

    O 5o erro diz que você está tentando modificar os headers tarde demais, depois que o output já começou – e isso não é permitido. Provavelmente você está chamando uma função tipo wp_redirect() ou semelhante, já depois que a página começou a ser exibida.

    Criador do tópico felipifranco

    (@felipifranco)

    Ql seria a melhor forma de eu criar o noncename para evitar este tipo de erro???

    Criador do tópico felipifranco

    (@felipifranco)

    Oq eu nao entendo é q o campo do valor foi criado, e posteriormente e tento acessar ele para salvar… Esta tudo funcionando corretamente, mais existe essas mensagens de erro! E nao sei como arrumar!

    Dentro do admin é um pouco mais simples usar check_admin_referer() ao invés de wp_verify_nonce(). Dê uma olhada na documentação aqui.

    Os 4 primeiros erros vc pode só esconder se quiser. São Notices, não geram problema nenhum. Normalmente, só de setar WP_DEBUG para false no wp-config.php isso já se resolve. O último vc precisa corrigir.

    Criador do tópico felipifranco

    (@felipifranco)

    Muito obrigado ricardo… Vou arrumar, assim q concluir eu falo se deu certo!! Só mais uma pergunta… Os 4 primeiros erros deve ser levados em consideração? Acredita q estas mensagens de erro pode comprometer o pleno funcionamento do wordpres??? E se vc acha importante eu buscar a resoluçao destes erro ao inves de mascaralos com o wp_debug!!

    Obrigado por enquanto!!!

Visualizando 5 respostas - 1 até 5 (de um total de 5)
  • O tópico ‘Problemas com Meta Box’ está fechado para novas respostas.