我想在创建新组时添加一些自定义字段(我用于倒计时) 但在创建新组时不起作用,仅在编辑组时起作用。
我有这个代码:
function save_custom_buddypress_group_textinput($group_id) {
if ( isset($_POST['custom_group_textinput']) ) {
if ( $group_id == 0 ) {
$group_id = buddypress()->groups->new_group_id;
}elseif(bp_get_current_group_id() > 0) {
$group_id = bp_get_current_group_id();
}
$text_input_value = sanitize_text_field($_POST['custom_group_textinput']);
groups_update_groupmeta($group_id, 'custom_group_textinput', $text_input_value);
}
}
add_action('groups_group_after_save', 'save_custom_buddypress_group_textinput');
add_action('groups_group_create_complete', 'save_custom_buddypress_group_textinput');
add_action('groups_group_edit_save_complete', 'save_custom_buddypress_group_textinput');```
my code is designed to save custom fields in BuddyPress groups when a group is created or edited. However, i mentioned that it's not working when creating a new group.
$group_id become 0 and not working
这是我的最终代码:
// Funcția pentru adăugarea câmpului text la formularul de creare/actualizare al grupului
function custom_buddypress_group_textinput() {
// Asigurați-vă că sunteți într-o pagină de creare sau editare a grupului BuddyPress
if (bp_is_group_create() || bp_is_group_admin_page()) {
$group_id = bp_get_current_group_id();
$custom_group_textinput = groups_get_groupmeta( $group_id, 'custom_group_textinput' );
?>
<label for="custom_group_textinput">Time Remaining:</label> <?php // echo strtotime($custom_group_textinput); // timestamp ?>
<input type="datetime-local" name="custom_group_textinput" id="custom_group_textinput" value="<?php echo $custom_group_textinput; ?>" required />
<?php
}
}
add_action('bp_after_group_details_creation_step', 'custom_buddypress_group_textinput');
function save_custom_buddypress_group_textinput($group_id = 0, $group = null) {
if ( isset($_POST['custom_group_textinput']) ) {
if ( $group_id == 0 ) {
$group_id = buddypress()->groups->new_group_id;
}elseif(bp_get_current_group_id() > 0) {
$group_id = bp_get_current_group_id();
}
$text_input_value = sanitize_text_field($_POST['custom_group_textinput']);
groups_update_groupmeta($group_id, 'custom_group_textinput', $text_input_value);
}
}
add_action('groups_group_after_save', 'save_custom_buddypress_group_textinput');
add_action('groups_created_group', 'save_custom_buddypress_group_textinput');
add_action('groups_group_create_complete', 'save_custom_buddypress_group_textinput');
add_action('groups_group_edit_save_complete', 'save_custom_buddypress_group_textinput');