admin管理员组文章数量:1336631
I hope to customize bbpress reply form area with dropdown selection list. I could not find any previous post reporting similar case in bbpress. Referring to post "Save meta box data from selected dropdown list" with link I prepared similar code for the bbpress reply post condition, but failed to achieve the goal.
add_action( 'bbp_theme_before_reply_form_content', 'so_custom_meta_box' );
//add_action( 'add_meta_boxes', 'so_custom_meta_box' );
function so_custom_meta_box($post){
add_meta_box('so_meta_box', 'Custom Box', 'custom_element_grid_class_meta_box', $post->post_type, 'normal' , 'high');
}
add_action('bbp_theme_before_reply_content', 'so_save_metabox');
function so_save_metabox(){
global $post;
if(isset($_POST["custom_element_grid_class"])){
//UPDATE:
$meta_element_class = $_POST['custom_element_grid_class'];
//END OF UPDATE
update_post_meta($reply_id, 'custom_element_grid_class_meta_box', $meta_element_class);
//print_r($_POST);
}
}
function custom_element_grid_class_meta_box($post){
$reply_id = bbp_get_reply_id();
$meta_element_class = get_post_meta($reply_id, 'custom_element_grid_class_meta_box', true); //true ensures you get just one value instead of an array
?>
<label>Choose the size of the element : </label>
<select name="custom_element_grid_class" id="custom_element_grid_class">
<option value="normal" <?php selected( $meta_element_class, 'normal' ); ?>>normal</option>
<option value="square" <?php selected( $meta_element_class, 'square' ); ?>>square</option>
<option value="wide" <?php selected( $meta_element_class, 'wide' ); ?>>wide</option>
<option value="tall" <?php selected( $meta_element_class, 'tall' ); ?>>tall</option>
</select>
<?php
}
Could anybody can provide kind help to solve the problem? Thanks!
I hope to customize bbpress reply form area with dropdown selection list. I could not find any previous post reporting similar case in bbpress. Referring to post "Save meta box data from selected dropdown list" with link https://stackoverflow/questions/17755973/save-meta-box-data-from-selected-dropdown-list I prepared similar code for the bbpress reply post condition, but failed to achieve the goal.
add_action( 'bbp_theme_before_reply_form_content', 'so_custom_meta_box' );
//add_action( 'add_meta_boxes', 'so_custom_meta_box' );
function so_custom_meta_box($post){
add_meta_box('so_meta_box', 'Custom Box', 'custom_element_grid_class_meta_box', $post->post_type, 'normal' , 'high');
}
add_action('bbp_theme_before_reply_content', 'so_save_metabox');
function so_save_metabox(){
global $post;
if(isset($_POST["custom_element_grid_class"])){
//UPDATE:
$meta_element_class = $_POST['custom_element_grid_class'];
//END OF UPDATE
update_post_meta($reply_id, 'custom_element_grid_class_meta_box', $meta_element_class);
//print_r($_POST);
}
}
function custom_element_grid_class_meta_box($post){
$reply_id = bbp_get_reply_id();
$meta_element_class = get_post_meta($reply_id, 'custom_element_grid_class_meta_box', true); //true ensures you get just one value instead of an array
?>
<label>Choose the size of the element : </label>
<select name="custom_element_grid_class" id="custom_element_grid_class">
<option value="normal" <?php selected( $meta_element_class, 'normal' ); ?>>normal</option>
<option value="square" <?php selected( $meta_element_class, 'square' ); ?>>square</option>
<option value="wide" <?php selected( $meta_element_class, 'wide' ); ?>>wide</option>
<option value="tall" <?php selected( $meta_element_class, 'tall' ); ?>>tall</option>
</select>
<?php
}
Could anybody can provide kind help to solve the problem? Thanks!
Share Improve this question asked May 21, 2020 at 13:58 DanielDaniel 111 bronze badge 2- This code can't work. Metaboxes are supported by WordPress on admin side only, they can't be rendered in any random place on the frontend. Adding fields to bbPress frontend forms is possible, but not like this. – Milan Petrovic Commented May 21, 2020 at 14:53
- Thank you for your comment, Milan Petrovic! The code will be part of plugin in my bbpress forum. So could you please kindly describe the mistakes in the codes? – Daniel Commented May 23, 2020 at 2:49
1 Answer
Reset to default 0I add a show contents part referring to a workable previous post with link https://bbpress/forums/topic/add-custom-text-fields-to-reply-form/. The below code can provide options for the user to selection. But the selection result could not be shown. I am not sure whether the selection result was added into metabox properly because I can not use the debug function. And another problem is that the “Field 1 ” was inserted into all previous reply posts. Could anybody further provide help to solve this problem?
add_action( 'bbp_theme_before_reply_form_content', 'so_additional_content' );
function so_additional_content($post){
$reply_id = bbp_get_reply_id();
$meta_element_class = get_post_meta($reply_id, 'custom_element_grid_class', true);
//$meta_element_class = get_post_meta($reply_id, 'custom_element_grid_class_meta_box', true); //true ensures you get just one value instead of an array
?>
echo '<label>Choose the size of the element : </label>
<select name="custom_element_grid_class" id="custom_element_grid_class">
<option value="normal" <?php selected( $meta_element_class, 'normal' ); ?>>normal</option>
<option value="square" <?php selected( $meta_element_class, 'square' ); ?>>square</option>
<option value="wide" <?php selected( $meta_element_class, 'wide' ); ?>>wide</option>
<option value="tall" <?php selected( $meta_element_class, 'tall' ); ?>>tall</option>
</select>'
<?php
}
add_action('bbp_new_reply_post_extras', 'so_save_metabox');
function so_save_metabox(){
global $post;
if(isset($_POST["custom_element_grid_class"])) {
//check the capability: if ( !current_user_can( 'edit_post', $post->ID )) return $post->ID;
$meta_element_class = $_POST['custom_element_grid_class'];
//END OF UPDATE
update_post_meta($reply_id, 'custom_element_grid_class', $meta_element_class);
//update_post_meta($reply_id, 'custom_element_grid_class_meta_box', $meta_element_class);
//print_r($_POST);
}
}
add_action('bbp_theme_before_reply_content', 'so_show_metabox');
function so_show_metabox() {
$meta_element_class = get_post_meta($reply_id, 'custom_element_grid_class', true);
//$meta_element_class = get_post_meta($reply_id, 'custom_element_grid_class_meta_box', true);
// the problem is "Field 1" appear in every reply post
echo "Field 1: ".$meta_element_class."End of testing "."<br>";
}
本文标签: plugin developmentSave meta box data from selected dropdown list in bbpress reply form
版权声明:本文标题:plugin development - Save meta box data from selected dropdown list in bbpress reply form 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742408091a2469233.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论