Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress元框是否只保存部分值?_Php_Wordpress_Meta Boxes - Fatal编程技术网

Php Wordpress元框是否只保存部分值?

Php Wordpress元框是否只保存部分值?,php,wordpress,meta-boxes,Php,Wordpress,Meta Boxes,我正在为wordpress制作costum元框。问题是wordpress似乎只保留/保存我在字段中输入的一些值。。我也找不到一个模式。。下面是代码: <?php function add_products_metaboxes() { add_meta_box('sra_product_info', 'Product Information', 'sra_products_info', 'product', 'side', 'default'); } // The Produ

我正在为wordpress制作costum元框。问题是wordpress似乎只保留/保存我在字段中输入的一些值。。我也找不到一个模式。。下面是代码:

<?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

一般来说,我建议在字段名中使用前缀。像
\u name
这样的值很有可能与其他地方同名的值发生冲突。使用像
\u product\u name
之类的东西。你能试试吗?如果您的代码与页面一起工作,它应该会产生影响。 为什么不用'true'添加第四个参数来添加更新后的元数据()和上一个值呢?有关这些函数,请参阅WordPress codex:。所以它会这样:

        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);
    }

我认为很明显,您与同名的元键存在冲突。不是在页面中,而是在帖子中。因此,请尝试使用第四个参数来确保引用的是唯一的键(我的建议是,仍然要使用清晰的前缀来区别于其他任何键,无论是插件、内核等)

标签上的
for
属性引用表单控件的
id
,不是
名称
。我已经为测试的“页面”帖子类型添加了元框,它完美地保存了所有细节。更改和删除也不是问题。为了让它运行,我必须添加这一行
add_action('admin_菜单','add_products_metaboxes'),这可能是代码中的其他地方。请注意,我是作为管理员进行编辑的。您使用什么类型的用户来更改元信息?同样作为测试,尝试在新主题上运行该代码,而不进行任何其他修改(只需使用index.php、functions.php和style.css创建一个新文件夹)。也许你的储蓄被别的东西弄乱了。我正在使用一个管理员用户来。。。。字段名称、版本和支持似乎添加正确。其他的都是空白的……哦,谢谢你在for attr上的提示。:)谢谢你的提示:)我的实际错误是我忘记了两个字段上的“”,所以这有点令人沮丧:)我将添加一个额外的参数,并为名称添加前缀:)