在new-user.php上取消选中默认情况下的“Send User Notify”框

时间:2020-05-02 作者:Samuel

我在想办法uncheck the box by default 当我们在WordPress上创建新用户时。

现在,当我们访问wp-admin/user-new.php 如以下截图所示:

enter image description here

这是生成设置的代码(请参见文件wp-admin/user-new.php 在WordPress core中):

<tr>
    <th scope="row"><?php _e( \'Send User Notification\' ); ?></th>
    <td>
        <input type="checkbox" name="send_user_notification" id="send_user_notification" value="1" <?php checked( $new_user_send_notification ); ?> />
        <label for="send_user_notification"><?php _e( \'Send the new user an email about their account.\' ); ?></label>
    </td>
</tr>

1 个回复
最合适的回答,由SO网友:Sven 整理而成

如果不复制整个用户界面,似乎就不容易进行覆盖。因此,最快的解决方案可能是通过JavaScript取消选中复选框:

add_action(\'admin_footer\', \'uncheck_send_user_notification\');
function uncheck_send_user_notification() {
    $currentScreen = get_current_screen();
    if (\'user\' === $currentScreen->id) {
        echo \'<script type="text/javascript">document.getElementById("send_user_notification").checked = false;</script>\';
    }
}
脚本仅注入到user-new.php 页码(参见$currentScreen->id), 然后选中复选框(请参见document.getElementById) 并将选中的设置为false.

更新:不是检查函数中的当前屏幕,而是admin_footer-{$hook_suffix} 仅在特定管理页面上运行的操作:

add_action(\'admin_footer-user-new.php\', \'uncheck_send_user_notification\');
function uncheck_send_user_notification() {
    echo \'<script type="text/javascript">document.getElementById("send_user_notification").checked = false;</script>\';
}