注册时间2024-11-22
最后登录2026-6-17
在线时间1756 小时
UID1
买家信用
卖家信用
总共送礼:1 个
总共收礼:0 个
功勋·创世元老
|
交易无需等待,成交就是现在,全面资源整合网络大咖云集,让你轻松玩转互联网!
您需要 登录 才可以下载或查看,没有账号?立即注册
×
这是一款子比论坛的一个功能,就是有很多论坛的人要用到的,有些人喜欢发帖子,但是缺不选择标签,包括用户发帖也不喜欢填写标签,今天腾飞博客给大家分享一个必选标签的功能,喜欢的自行部署吧!
代码部署
将下面的代码放到:/wp-content/themes/zibll/func.php文件里面,如果没有这个文件,记得自己创建一个,记得加上php头,要不然会报错!
- /**
- * 论坛发帖标签必选功能
- * 此功能确保用户在发布论坛帖子时必须选择至少一个标签
- */
- function jipan_bbs_tag_required_early_check() {
- if (
- defined('DOING_AJAX') &&
- DOING_AJAX &&
- isset($_REQUEST['action']) &&
- $_REQUEST['action'] === 'bbs_posts_save'
- ) {
- global $zib_bbs;
-
- // 首先检查标题
- $post_title = !empty($_REQUEST['post_title']) ? strip_tags(trim($_REQUEST['post_title'])) : '';
- if (!$post_title) {
- $response = array(
- 'error' => 1,
- 'msg' => '请填写标题',
- 'ys' => 'danger'
- );
-
- header('Content-Type: application/json');
- echo json_encode($response);
- exit;
- }
-
- $title_strlen_limit = _pz('bbs_post_title_strlen_limit') ?: array('min' => 5, 'max' => 30);
- if ($title_strlen_limit['max'] && zib_new_strlen($post_title) > $title_strlen_limit['max']) {
- $response = array(
- 'error' => 1,
- 'msg' => '标题太长了,不能超过' . $title_strlen_limit['max'] . '个字',
- 'ys' => 'danger'
- );
-
- header('Content-Type: application/json');
- echo json_encode($response);
- exit;
- }
-
- if ($title_strlen_limit['min'] && zib_new_strlen($post_title) < $title_strlen_limit['min']) {
- $response = array(
- 'error' => 1,
- 'msg' => '标题太短!',
- 'ys' => 'danger'
- );
-
- header('Content-Type: application/json');
- echo json_encode($response);
- exit;
- }
-
- // 然后检查板块
- $plate = !empty($_REQUEST['plate']) ? (int) $_REQUEST['plate'] : 0;
- if (!$plate) {
- $response = array(
- 'error' => 1,
- 'msg' => '请选择' . ($zib_bbs ? $zib_bbs->plate_name : '板块'),
- 'ys' => 'danger'
- );
-
- header('Content-Type: application/json');
- echo json_encode($response);
- exit;
- }
-
- // 最后检查标签
- $has_valid_tag = false;
- $tag_g = isset($_REQUEST['tag']) ? $_REQUEST['tag'] : array();
-
- if (is_array($tag_g)) {
- foreach ($tag_g as $tag_id) {
- if (!empty($tag_id)) {
- $has_valid_tag = true;
- break;
- }
- }
- }
-
- // 如果没有有效标签,返回错误
- if (!$has_valid_tag) {
- $response = array(
- 'error' => 1,
- 'msg' => '请选择标签',
- 'ys' => 'danger'
- );
-
- // 输出JSON响应并结束执行
- header('Content-Type: application/json');
- echo json_encode($response);
- exit;
- }
- }
- }
- add_action('init', 'jipan_bbs_tag_required_early_check', 1);
- function jipan_bbs_required_tag_js() {
- ?>
- <script type="text/javascript">
- jQuery(document).ready(function($) {
- // 添加"(必选)"标记到标签选择器
- function addRequiredLabelToTags() {
- $('.tag-drop').each(function() {
- var $tagDrop = $(this);
- var $tagLabel = $tagDrop.find('.drop-btn .flex.ac').first();
-
- if ($tagLabel.length) {
- var labelText = $tagLabel.text().trim();
- if (labelText.indexOf('(必选)') === -1) {
- // 找到文本节点并在其后添加"(必选)"标记
- var $icon = $tagLabel.find('i, .fa, .icon, svg').first();
- if ($icon.length) {
- // 如果有图标,在图标后面插入必选标记
- $icon.after('<span class="ml5 mr5 c-red">(必选)</span>');
- } else {
- // 在第一个子元素后插入
- $tagLabel.prepend('<span class="c-red">(必选)</span><span class="mr5"></span>');
- }
- }
- }
- });
- }
-
- setTimeout(addRequiredLabelToTags, 500);
- $(document).on('click', '.posts-submit', function(e) {
- // 首先检查标题
- var $form = $('#bbs-posts-edit');
- var $titleInput = $form.find('input[name="post_title"]');
- var title = $titleInput.val().trim();
-
- if (!title) {
- e.preventDefault();
- e.stopImmediatePropagation();
-
- if (window.notyf) {
- window.notyf('请填写标题', 'danger');
- } else {
- alert('请填写标题');
- }
-
- $titleInput.focus();
- return false;
- }
-
- // 然后检查板块
- var $plateInput = $form.find('input[name="plate"]');
- var plate = $plateInput.val();
-
- if (!plate) {
- e.preventDefault();
- e.stopImmediatePropagation();
-
- if (window.notyf) {
- window.notyf('请选择板块', 'danger');
- } else {
- alert('请选择板块');
- }
- var $plateDrop = $form.find('.plate-drop');
- if ($plateDrop.length) {
- $('html, body').animate({
- scrollTop: $plateDrop.offset().top - 100
- }, 200);
- }
-
- return false;
- }
-
- // 最后检查标签
- var $tagInputs = $form.find('input[name="tag[]"]');
- var hasTag = false;
-
- $tagInputs.each(function() {
- if ($(this).val()) {
- hasTag = true;
- return false;
- }
- });
-
- if (!hasTag) {
- e.preventDefault();
- e.stopImmediatePropagation();
-
- if (window.notyf) {
- window.notyf('请选择标签', 'danger');
- } else {
- alert('请选择标签');
- }
-
- var $tagDrop = $form.find('.tag-drop');
- if ($tagDrop.length) {
- $('html, body').animate({
- scrollTop: $tagDrop.offset().top - 100
- }, 200);
- }
-
- return false;
- }
- });
- });
- </script>
- <?php
- }
- add_action('wp_footer', 'jipan_bbs_required_tag_js', 99);
复制代码
|
|