问题描述
我使用在线生成器创建元框并使用它,但我想从前端发送数据并在编辑帖子时以自定义帖子类型在后端显示它。
我使用 class Rational_Meta_Box {
private $screens = array(
'post','Feedback_price',);
private $fields = array(
array(
'id' => 'user-name','label' => 'user name','type' => 'text',),array(
'id' => 'url-product','label' => 'url product',array(
'id' => 'price-satisfaction','label' => 'Price satisfaction',array(
'id' => 'price','label' => 'price',);
/**
* 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','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-Meta-Box',__( 'my Meta Box','fffffffffffffffff' ),'add_Meta_Box_callback' ),$screen,'advanced','default'
);
}
}
/**
* Generates the HTML for the Meta Box
*
* @param object $post wordpress post object
*/
public function add_Meta_Box_callback( $post ) {
wp_nonce_field( 'my_Meta_Box_data','my_Meta_Box_nonce' );
$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_Meta_Box_' . $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['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_Meta_Box_nonce'] ) )
return $post_id;
$nonce = $_POST['my_Meta_Box_nonce'];
if ( !wp_verify_nonce( $nonce,'my_Meta_Box_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,$_POST[ $field['id'] ] );
} else if ( $field['type'] === 'checkBox' ) {
update_post_Meta( $post_id,'0' );
}
}
}
}
new Rational_Meta_Box;
保存其余的字段,如标题。我想以相同的方式从用户那里获取其余的 MetaBox 字段,并将其保存在帖子中的自定义类型帖子中,并在编辑帖子时查看用户发送的 MetaBoxes。
"post": {
...
"consumes" : [
"multipart/form-data"
],"produces": [
"application/json"
],"parameters": [
{
"name": "updateFile","in": "formData","type": "file","required": true,}
],...
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)