我在WordPress配置文件中添加了一个额外的textarea,我遇到的问题是一旦保存就不会显示。我已经看过很多关于它的帖子,甚至将它复制并粘贴到我的函数中也没有去,有什么我在这里缺少的吗?
旧代码:
add_action( 'show_user_profile', 'xtra_field' );
add_action( 'edit_user_profile', 'xtra_field' );
function xtra_field( $user ) { ?>
<h3>Teacher Information</h3>
<table class="form-table">
<tr>
<th><label for="teach_year">Year</label></th>
<td>
<input type="number" name="teach_year" id="teach_year" min="1" max="7" value="<?php echo esc_attr( get_the_author_meta( 'teach_year', $user->ID ) ); ?>" class="regular-text" /><br />
<span style="line-height: 42px;" class="description">What year do you teach?</span>
</td>
</tr>
<tr>
<th><label for="class_intro">Class Introduction</label></th>
<td>
<textarea type="text" name="class_intro" id="class_intro" rows="5" cols="30" class="regular-text" value="<?php echo esc_attr( get_the_author_meta( 'class_intro', $user->ID ) ); ?>"></textarea><br/>
<span class="description">Introduce your class.</span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'xtra_save_field' );
add_action( 'edit_user_profile_update', 'xtra_save_field' );
function xtra_save_field( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ))
return false;
update_user_meta($user_id, 'teach_year', $_POST['teach_year']);
update_user_meta($user_id, 'class_intro', $_POST['class_intro']);
}
有人离线暗示回应作者元,这看起来有效,我已经更新了代码。
新代码:
add_action( 'show_user_profile', 'xtra_field' );
add_action( 'edit_user_profile', 'xtra_field' );
function xtra_field( $user ) { ?>
<h3>Teacher Information</h3>
<table class="form-table">
<tr>
<th><label for="teach_year">Year</label></th>
<td>
<input type="number" name="teach_year" id="teach_year" min="1" max="7" value="<?php echo esc_attr(get_the_author_meta('teach_year', $user->ID)); ?>" class="regular-text" /><br />
<span style="line-height: 42px;" class="description">What year do you teach?</span>
</td>
</tr>
<tr>
<th><label for="class_intro">Class Introduction</label></th>
<td>
<textarea type="text" name="class_intro" id="class_intro" rows="5" cols="30" class="regular-text"><?php echo esc_html(get_the_author_meta('class_intro', $user->ID) ); ?></textarea>
<br/>
<span class="description">Provide your class with an introduction.</span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'xtra_save_field' );
add_action( 'edit_user_profile_update', 'xtra_save_field' );
function xtra_save_field( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ))
return false;
update_user_meta($user_id, 'teach_year', $_POST['teach_year']);
update_user_meta($user_id, 'class_intro', $_POST['class_intro']);
}
答案 0 :(得分:2)
Textarea没有value=".."
参数。相反,它保留了<textarea>
和</textarea>
之间的内容(即它的“价值”)
文档:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea