如何添加自定义文件并保存在wordpress中?

如何添加自定义文件并保存在wordpress中?,wordpress,custom-fields,Wordpress,Custom Fields,下面的代码我试过了,但它不是自定义文件的保存值 add_action( 'add_meta_boxes', 'cd_meta_box_add' ); function cd_meta_box_add() { add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'product', 'side', 'high' ); } 在上面我添加了代码,当posttype为product时显示它 function c

下面的代码我试过了,但它不是自定义文件的保存值

add_action( 'add_meta_boxes', 'cd_meta_box_add' );
 function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'product', 'side', 'high' );
}
在上面我添加了代码,当posttype为product时显示它

function cd_meta_box_cb( $product)
{
  $values = get_post_custom( $product->ID );
  $text = isset( $values['my_meta_box_text'] ) ? esc_attr($values['my_meta_box_text'][0] ) : ”;

?>
<p>
 <label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
    </p>
<?php        
}
我想将数据保存在wp_Posteta表中。上面的代码我已经试过了。我是wordpress的初学者。能给我一些建议吗?

看看播客。 这是一个非常强大的定制插件。

只需替换您的添加操作('save\u post'、'cd\u meta\u box\u save');函数并放入下面的代码

add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{
    if( isset( $_POST[ 'my_meta_box_text' ] ) ) {
        update_post_meta( $product_id,'my_meta_box_text', $_POST['my_meta_box_text'] );
    }
}
完整的代码,如(它工作正常,还保存Posteta表中的自定义字段)

add_操作('add_meta_box','cd_meta_box_add');
函数cd_meta_box_add()
{
添加元盒(“我的元盒id”、“我的第一个元盒”、“cd元盒cb”、“post”、“side”、“high”);
}
功能cd\u元盒\u cb($product)
{
$values=get\u post\u custom($product->ID);
$text=isset($values['my\u meta\u box\u text'])?esc\u attr($values['my\u meta\u box\u text'][0]):'';
?>

文本标签

使用此代码为post创建自定义字段

  class Rational_Meta_Box {
        private $screens = array(
            'post',
        );
        private $fields = array(
            array(
                'id' => 'custom-field-1',
                'label' => 'custom field 1',
                'type' => 'text',
            ),
            array(
                'id' => 'custom-field-2',
                'label' => 'custom field 2',
                'type' => 'text',
            ),
        );

/**
 * Class construct method. Adds actions to their respective WordPress hooks.
 */



    public function __construct() {
            add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
            add_action( 'save_post', array( $this, 'save_post' ) );
        }

    /**
     * Hooks into WordPress' add_meta_boxes function.
     * Goes through screens (post types) and adds the meta box.
     */



public function add_meta_boxes() {
    foreach ( $this->screens as $screen ) {
        add_meta_box(
            'my-custom-fields',
            __( 'my custom fields', 'wordpress' ),
            array( $this, 'add_meta_box_callback' ),
            $screen,
            'advanced',
            'high'
        );
    }
}

    /**
     * Generates the HTML for the meta box
     * 
     * @param object $post WordPress post object
     */



public function add_meta_box_callback( $post ) {
    wp_nonce_field( 'my_custom_fields_data', 'my_custom_fields_nonce' );
    echo 'its for custom fields for post typle';
    $this->generate_fields( $post );
}

    /**
     * Generates the field's HTML for the meta box.
     */


    public function generate_fields( $post ) {
            $output = '';
            foreach ( $this->fields as $field ) {
                $label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
                $db_value = get_post_meta( $post->ID, 'my_custom_fields_' . $field['id'], true );
                switch ( $field['type'] ) {
                    default:
                        $input = sprintf(
                            '<input %s id="%s" name="%s" type="%s" value="%s">',
                            $field['type'] !== 'color' ? 'class="regular-text"' : '',
                            $field['id'],
                            $field['id'],
                            $field['type'],
                            $db_value
                        );
                }
                $output .= $this->row_format( $label, $input );
            }
            echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
        }

        /**
         * Generates the HTML for table rows.
         */
        public function row_format( $label, $input ) {
            return sprintf(
                '<tr><th scope="row">%s</th><td>%s</td></tr>',
                $label,
                $input
            );
        }
        /**
         * Hooks into WordPress' save_post function
         */
        public function save_post( $post_id ) {
            if ( ! isset( $_POST['my_custom_fields_nonce'] ) )
                return $post_id;

            $nonce = $_POST['my_custom_fields_nonce'];
            if ( !wp_verify_nonce( $nonce, 'my_custom_fields_data' ) )
                return $post_id;

            if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
                return $post_id;

            foreach ( $this->fields as $field ) {
                if ( isset( $_POST[ $field['id'] ] ) ) {
                    switch ( $field['type'] ) {
                        case 'email':
                            $_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );
                            break;
                        case 'text':
                            $_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );
                            break;
                    }
                    update_post_meta( $post_id, 'my_custom_fields_' . $field['id'], $_POST[ $field['id'] ] );
                } else if ( $field['type'] === 'checkbox' ) {
                    update_post_meta( $post_id, 'my_custom_fields_' . $field['id'], '0' );
                }
            }
        }
    }
    new Rational_Meta_Box;
class Rational\u Meta\u框{
私有$screens=array(
"岗位",,
);
私有$fields=数组(
排列(
'id'=>'custom-field-1',
'标签'=>'自定义字段1',
'类型'=>'文本',
),
排列(
'id'=>'custom-field-2',
'标签'=>'自定义字段2',
'类型'=>'文本',
),
);
/**
*类构造方法。将操作添加到各自的WordPress挂钩。
*/
公共函数构造(){
add_操作('add_meta_box',数组($this,'add_meta_box'));
添加操作('save_post',数组($this,'save_post'));
}
/**
*钩住WordPress的“添加元框”功能。
*浏览屏幕(帖子类型)并添加元框。
*/
公共函数添加元框(){
foreach($this->屏幕显示为$screen){
添加元框(
“我的自定义字段”,
__(“我的自定义字段”、“wordpress”),
数组('add_meta_box_callback'),
$screen,
"先进",,
“高”
);
}
}
/**
*为元框生成HTML
* 
*@param object$post WordPress post object
*/
公共函数add_meta_box_回调($post){
wp_nonce_字段('my_custom_fields_data'、'my_custom_fields_nonce');
echo“其用于post typle的自定义字段”;
$this->generate_字段($post);
}
/**
*为元框生成字段的HTML。
*/
公共函数生成_字段($post){
$output='';
foreach($this->fields as$field){
$label='.$field['label'].';
$db_value=get_post_meta($post->ID,'my_custom_fields'.$field['ID'],true);
开关($field['type'])){
违约:
$input=sprintf(
'',
$field['type']!='color'?'class=“常规文本”:“,
$field['id'],
$field['id'],
$field['type'],
$db_值
);
}
$output.=$this->row\u格式($label,$input);
}
回显“.$output.”;
}
/**
*生成表格行的HTML。
*/
公共函数行_格式($label,$input){
回程冲刺(
“%s%s”,
$label,
$input
);
}
/**
*钩住WordPress的“保存帖子”功能
*/
公共功能保存帖子($post\u id){
如果(!isset($\u POST['my\u custom\u fields\u nonce']))
返回$post_id;
$nonce=$\u POST['my\u custom\u fields\u nonce'];
如果(!wp\u verify\u nonce($nonce,'my\u custom\u fields\u data'))
返回$post_id;
if(已定义('DOING_AUTOSAVE')&&DOING_AUTOSAVE)
返回$post_id;
foreach($this->fields as$field){
如果(isset($_POST[$field['id']])){
开关($field['type'])){
“电子邮件”案例:
$\u POST[$field['id']]=清理电子邮件($\u POST[$field['id']]);
打破
案例“文本”:
$\u POST[$field['id']]=清理文本字段($\u POST[$field['id']]);
打破
}
更新发布元数据($post\u id,'my\u custom\u fields'.$field['id'],$发布[$field['id']);
}else if($field['type']='checkbox'){
更新_post_meta($post_id,'my_custom_fields,'0');
}
}
}
}
新的Rational_Meta_盒;
因此,它将在您的帖子类型中创建自定义字段,并将其保存


参照此操作

保存时,是否可以检查表单是否已提交?如果是,请回显
$\u REQUEST
的值以检查是否已获取所有数据。我不想使用插件。
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
 function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'side', 'high' );

}

function cd_meta_box_cb( $product)
{
  $values = get_post_custom( $product->ID );
  $text = isset( $values['my_meta_box_text'] ) ? esc_attr($values['my_meta_box_text'][0] ) : '';

?>
<p>
 <label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
    </p>
<?php        
}


add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $product_id )
{

    if( isset( $_POST[ 'my_meta_box_text' ] ) ) {
        update_post_meta( $product_id,'my_meta_box_text', $_POST['my_meta_box_text'] );
    }
}
  class Rational_Meta_Box {
        private $screens = array(
            'post',
        );
        private $fields = array(
            array(
                'id' => 'custom-field-1',
                'label' => 'custom field 1',
                'type' => 'text',
            ),
            array(
                'id' => 'custom-field-2',
                'label' => 'custom field 2',
                'type' => 'text',
            ),
        );

/**
 * Class construct method. Adds actions to their respective WordPress hooks.
 */



    public function __construct() {
            add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
            add_action( 'save_post', array( $this, 'save_post' ) );
        }

    /**
     * Hooks into WordPress' add_meta_boxes function.
     * Goes through screens (post types) and adds the meta box.
     */



public function add_meta_boxes() {
    foreach ( $this->screens as $screen ) {
        add_meta_box(
            'my-custom-fields',
            __( 'my custom fields', 'wordpress' ),
            array( $this, 'add_meta_box_callback' ),
            $screen,
            'advanced',
            'high'
        );
    }
}

    /**
     * Generates the HTML for the meta box
     * 
     * @param object $post WordPress post object
     */



public function add_meta_box_callback( $post ) {
    wp_nonce_field( 'my_custom_fields_data', 'my_custom_fields_nonce' );
    echo 'its for custom fields for post typle';
    $this->generate_fields( $post );
}

    /**
     * Generates the field's HTML for the meta box.
     */


    public function generate_fields( $post ) {
            $output = '';
            foreach ( $this->fields as $field ) {
                $label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
                $db_value = get_post_meta( $post->ID, 'my_custom_fields_' . $field['id'], true );
                switch ( $field['type'] ) {
                    default:
                        $input = sprintf(
                            '<input %s id="%s" name="%s" type="%s" value="%s">',
                            $field['type'] !== 'color' ? 'class="regular-text"' : '',
                            $field['id'],
                            $field['id'],
                            $field['type'],
                            $db_value
                        );
                }
                $output .= $this->row_format( $label, $input );
            }
            echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
        }

        /**
         * Generates the HTML for table rows.
         */
        public function row_format( $label, $input ) {
            return sprintf(
                '<tr><th scope="row">%s</th><td>%s</td></tr>',
                $label,
                $input
            );
        }
        /**
         * Hooks into WordPress' save_post function
         */
        public function save_post( $post_id ) {
            if ( ! isset( $_POST['my_custom_fields_nonce'] ) )
                return $post_id;

            $nonce = $_POST['my_custom_fields_nonce'];
            if ( !wp_verify_nonce( $nonce, 'my_custom_fields_data' ) )
                return $post_id;

            if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
                return $post_id;

            foreach ( $this->fields as $field ) {
                if ( isset( $_POST[ $field['id'] ] ) ) {
                    switch ( $field['type'] ) {
                        case 'email':
                            $_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );
                            break;
                        case 'text':
                            $_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );
                            break;
                    }
                    update_post_meta( $post_id, 'my_custom_fields_' . $field['id'], $_POST[ $field['id'] ] );
                } else if ( $field['type'] === 'checkbox' ) {
                    update_post_meta( $post_id, 'my_custom_fields_' . $field['id'], '0' );
                }
            }
        }
    }
    new Rational_Meta_Box;