• Currently trying to develop a plugin that will place a text input on the post editor page, the metabox does show up and is visible, however, both the nonce field and the custom metabox value are not in the $_POST array when I publish the post. I’m pretty stumped as I’m new to wordpress and don’t have a ton of experience with PHP. Any help/feedback would be greatly appreciated.

    class PayperPostPrice {
      public function __construct() {
        add_action('add_meta_boxes', array($this, 'add_payper_meta_box'));
        add_action('save_post', array($this, 'save_meta_box_data'), 5, 1);
      }
    
      public function meta_box_callback($post) {
        // add nonce
        wp_nonce_field(basename(__FILE__), 'payper_meta_box_nonce');
        // Retrieve existing value if it exists
        $payper_post_price = get_post_meta($post->ID, 'payper_post_price', true);
    
        // Output HTML for input field
        ?>
          <label for="payper_post_price">Price:</label>
          <input type="text" id="payper_post_price" name="payper_post_price" value="<?php echo $payper_post_price ?>">
        <?php
      }
    
      public function add_payper_meta_box() {
        add_meta_box(
          'payper_post_price', // Unique ID for the meta box
          'PayPer', // Title of the meta box
          array($this, 'meta_box_callback'), // Callback function to display content
          'post', // Post type where the meta box should be displayed
          'side', // Context: 'normal', 'side', or 'advanced'
          'default', // Priority: 'default', 'high', 'low', or 'core'
        );
      }
    
      public function save_meta_box_data($post_id) {
        error_log('nonce: ' . $_POST['payper_meta_box_nonce']);
        // // Check if this is an autosave
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
          return;
        }
    
        // Check user permissions
        if (!current_user_can('edit_post', $post_id)) {
          return;
        }
        // Debug: Log the entire $_POST array
        error_log('$_POST: ' . print_r($_POST, true));
    
        // Check if 'payper_post_price' key exists in $_POST array
        if (isset($_POST['payper_post_price'])) {
          $payper_post_price = sanitize_text_field($_POST['payper_post_price']);
          update_post_meta($post_id, 'payper_post_price', $payper_post_price);
        } else {
          error_log('payper_post_price not found in $_POST array');
        }
      }
    }
    
    // Initialize PayPerMetaBox class
    new PayperPostPrice();
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Your class code works perfectly fine in my test site and plugin. Maybe you have a plugin or theme conflict? Or the POST request is getting redirected in a way that related data is lost?

    Is the entered data visible after updating and the page reloads? Does the meta data actually exist in the DB when you use phpMyAdmin to check?

    Full disclosure: I altered error_log() call to output to a different file since my error log is a PITA to get to.

    Thread Starter devomacdee

    (@devomacdee)

    oh, ok. I did just run this and only this plugin, and error_log('$_POST: ' . print_r($_POST, true)); this was just printing an empty array. I do have another plugin file as well, that also hooks into the save_posts action.

    public function send_post_data($post_id, $post, $update) {
        // Check if this is a new post creation, not an update
        if ($post->post_status === 'publish') {
          $post_title = $post->post_title;
          $post_url = get_permalink($post_id);
          $post_author = get_the_author_meta('display_name', $post->post_author);
          $merchant_id = get_option('payper_settings_option_name')['merchant_id_3'];
          // $price = get_option('payper_settings_option_name')['price_2'];
          $category = get_the_category($post_id);
    
          $api_key = $this->get_payper_api_key();
    
          if (empty( $api_key ) || !isset( $api_key ) || is_null( $api_key ) || $api_key === false) return;
    
          // $price = get_post_meta($post_id, 'payper_post_price', true);
          // error_log('get_post_meta: ' . get_post_meta($post_id));
    
          $price = "0.02";
          // $metas = get_post_meta($post_id);
    
          // foreach($metas as $key => $value) {
          //   error_log('get_post_meta key: ' . $key . ' => ' . $value[0]);
          // }
    
          $categoryId = '06d34685-e759-478c-a1ab-d777639aade9';
          // $categoryId = $category[0]->term_id;
    
          $data = array(
            'title' => $post_title,
            'url' => $post_url,
            'externalId' => (string)$post_id,
            'price' => $price,
            'currency' => 'USD',
            'categoryId' => $categoryId,
            'tokenAddress' => '0x776cc86fB175A141817Ca70e9D1c64b5E5EAC133',
            'tokenId' => $post_id,
            'authorName' => $post_author,
            'merchantId' => $merchant_id,
          );
    
          $args = array(
            'method' => 'POST',
            'body' => json_encode($data),
            'headers' => array(
              'Content-Type' => 'application/json',
              'Authorization' => 'Bearer ' . $api_key
            ),
          );
    
          $response = wp_remote_post(
            'https://localhost:3002/api/articles',
            $args
          );
    
          // Check if the request was successful
          if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) {
            // Post data sent successfully
            error_log('PayPer Article created successfully.');
          } else {
            // Error sending data
            error_log('Error sending post data to Payper API to create article: ' . wp_remote_retrieve_response_message($response));
          }
        }
      }
    }
    
    new New_Post_Notification();
    Thread Starter devomacdee

    (@devomacdee)

    Sorry, I also wanted to point out, that my nonce field is undefined/null in the save method. From everything I’ve seen this looks like how it should work, and I can echo that it’s there when I render the meta boxes.

    flo3

    (@flo3)

    Did you find out what the issue was?

    I’m having the same problem (content of meta box form fields are not added to the POST request when saving a page).

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.