Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8834815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:06:49+00:00 2026-06-14T09:06:49+00:00

I´m woring on a costum meta-box for wordpress. Trouble is wordpress only seems to

  • 0

I´m woring on a costum meta-box for wordpress. Trouble is wordpress only seems to retain/save some of the values I enter in the fields.. I can´t really find a pattern either.. so here’s the code:

<?php
function add_products_metaboxes() {
    add_meta_box('sra_product_info', 'Product Information', 'sra_products_info', 'product', 'side', 'default');
  }
  // The Productinfo Metabox
function sra_products_info() {
    //get access to the post object
    global $post;
    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="productmeta_noncename" id="productmeta_noncename" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    // Get the data from the field if its already been entered
    $name = get_post_meta($post->ID, '_name', true);
    $price = get_post_meta($post->ID, '_price', true);
    $includes = get_post_meta($post->ID, '_includes', true);
    $supports = get_post_meta($post->ID, '_supports', true);
    $version = get_post_meta($post->ID, '_version' , true);
    $extrainfo = get_post_meta($post->ID, '_extrainfo', true);
    // Echo out the form
   echo '<form>';
    echo '<label for="_name">Name</label>' . '<input type="text" name="_name" value="' . $name . '"/>';
    echo '<label for="_price">Price</label>' . '<input type="text" name="_price" value="' . $price . '"/>';
    echo '<label for="_includes">Includes</label> <textarea name="_includes" rows="4" cols="10">' . $includes . '</textarea>'; 
    echo '<label for="_supports">Supports</label> <input type="text" name="_supports" value="' . $supports . '"/>';
    echo '<label for="_version">Version</label>' . '<input type="text" name="_version" value="' . $version . '"/>';
    echo '<label for="_extrainfo">Extras</label> <textarea name="_extrainfo" rows="4" cols="10">' . $extrainfo . '</textarea>'; 
   echo '</form>';

}

// Save the Metabox Data
function sra_save_product_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['productmeta_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
    // check if the field exists in the posts array - if it does, then put cintent in $product_meta.
    // this code needs to be refactored!

if (isset($_POST['_name'])) {
    $product_meta['_name'] = $_POST['_name'];    
    }

 if (isset($_POST['_price'])) {
    $product_meta['_price'] = $_POST['_price'];    
    }

if (isset($_POST['_includes'])) {
    $product_meta['_includes'] = $_POST['_includes'];    
    }

if (isset($_POST['_supports'])) {
    $product_meta['_supports'] = $_POST['_supports'];    
    }

if (isset($_POST['_version'])) {
    $product_meta['_version'] = $_POST['_version'];    
    }

if (isset($_POST['_extrainfo'])) {
    $product_meta['_extrainfo'] = $_POST['_extrainfo'];    
    }


    // Add values of $prpduct_meta as custom fields
    foreach ($product_meta as $key => $value) { // Cycle through the $product_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 CSL (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', 'sra_save_product_meta', 1, 2); // save the custom fields

Do you see any obvious mistakes? I think I’ve become a bit blind to my own mistakes in this code..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:06:50+00:00Added an answer on June 14, 2026 at 9:06 am

    In general, I would advise to use prefixes for your field names. Values like _name have too much chances getting in conflict with other values with the same name elsewhere. Use things like _product_name, etc. Can you try that? If your code works with pages, it should have an impact.
    And why not adding the 4th parameter with ‘true’ to add_post_meta() and previous value for update_post_meta()? See WordPress codex about these functions: http://codex.wordpress.org/Function_Reference/add_post_meta. So it would go:

            if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value, /***OLDVALUE***/ ); // See http://codex.wordpress.org/Function_Reference/update_post_meta for old value
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value, true);
        }
    

    I think it is clear you have a conflict with a metakey with the same name. Not in pages, but in posts. So try to use this 4th parameter to ensure you are referencing a unique key (and my advice, still, use clear prefixes to differenciate from anything else, be it plugins, core, etc.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with Json, how can I NSlog only the title in this code: NSDictionary
Working with an undisclosed API, I found a function that can set the number
Working on game where plates will be falling from top to bottom. Some plates
hi there i am woring with java play framework and i want to implement
Working on Progress 9.1E on a Windows box. We've got a standard 4GL GUI
I'm seeing something I can't explain with Groovy (1.8) mixins when I drop an
I'm creating some custom modules on PyroCMS, and the problem is, that the default
I have made a form for client registration, and I can't make this postvalidators
I'm woring against a model which consists of a number of different types (Properties,
i'm making some experiments with Jquery UI draggable, but currently my application is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.