WordPress添加友链自助申请

前言

闲着无聊想弄个自助申请友链的功能,这样就不弄在评论区慢慢输入网站标题等文字了。

教程

因为我是登录才能评论所以以下代码只适用登录后才能评论。

1.首先在子主题的function.php 中添加如下代码。

function display_friendlink_form() {
    ob_start();
    ?>
    <style>
        #friendlink-form label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        #friendlink-form input[type="text"],
        #friendlink-form input[type="url"] {
            width: 20%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }

        #friendlink-form button[type="submit"] {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        #friendlink-form button[type="submit"]:hover {
            background-color: #45a049;
        }

        #friendlink-notification {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: #4CAF50;
            color: #fff;
            padding: 10px;
            border-radius: 4px;
            display: none;
            z-index: 9999;
        }
    </style>

    <form id="friendlink-form" method="post">
        <div>
            <label for="website-name">网站名称:<input type="text" name="website-name" id="website-name" required></label>
        </div>
        <div>
            <label for="website-description">网站描述:<input type="text" name="website-description" id="website-description" required></label>
        </div>
        <div>
            <label for="website-url">网站地址:<input type="url" name="website-url" id="website-url" required></label>
        </div>
        <div>
            <label for="website-icon">网站图标:<input type="url" name="website-icon" id="website-icon" required></label>
        </div>
        <button type="submit" id="submit-friendlink" disabled>提交</button>
    </form>

    <div id="friendlink-notification"></div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            var submitButton = document.getElementById('submit-friendlink');
            var websiteNameInput = document.getElementById('website-name');
            var websiteDescriptionInput = document.getElementById('website-description');
            var websiteUrlInput = document.getElementById('website-url');
            var websiteIconInput = document.getElementById('website-icon');

            function validateForm() {
                if (websiteNameInput.value.trim() !== '' &&
                    websiteDescriptionInput.value.trim() !== '' &&
                    websiteUrlInput.value.trim() !== '' &&
                    websiteIconInput.value.trim() !== '') {
                    submitButton.disabled = false;
                } else {
                    submitButton.disabled = true;
                }
            }

            websiteNameInput.addEventListener('input', validateForm);
            websiteDescriptionInput.addEventListener('input', validateForm);
            websiteUrlInput.addEventListener('input', validateForm);
            websiteIconInput.addEventListener('input', validateForm);

            document.getElementById('friendlink-form').addEventListener('submit', function(event) {
                event.preventDefault();

                var websiteName = websiteNameInput.value;
                var websiteDescription = websiteDescriptionInput.value;
                var websiteUrl = websiteUrlInput.value;
                var websiteIcon = websiteIconInput.value;

                var commentContent = '网站名称:' + websiteName + 'nn' +
                                     '网站描述:' + websiteDescription + 'nn' +
                                     '网站地址:' + websiteUrl + 'nn' +
                                     '网站图标:' + websiteIcon;

                var commentData = {
                    action: 'submit_friendlink',
                    websiteName: websiteName,
                    websiteDescription: websiteDescription,
                    websiteUrl: websiteUrl,
                    websiteIcon: websiteIcon,
                    post_id: <?php echo get_the_ID(); ?>,
                    user_id: <?php echo get_current_user_id(); ?>,
                };

                var xhr = new XMLHttpRequest();
                xhr.open('POST', '<?php echo admin_url('admin-ajax.php'); ?>', true);
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xhr.onload = function() {
                    if (xhr.status === 200) {
                        var response = JSON.parse(xhr.responseText);
                        if (response.success) {
                            location.reload(); // 手动刷新页面
                        } else {
                            showNotification('你尚未登录请登录后重试。');
                        }
                    } else {
                        showNotification('你尚未登录请登录后重试。');
                    }
                };
                xhr.send(Object.keys(commentData).map(function(key) {
                    return encodeURIComponent(key) + '=' + encodeURIComponent(commentData[key]);
                }).join('&'));
            });
        });

        function showNotification(message) {
            var notification = document.getElementById('friendlink-notification');
            notification.textContent = message;
            notification.style.display = 'block';
            setTimeout(function() {
                notification.style.display = 'none';
            }, 2000);
        }
    </script>
    <?php
    return ob_get_clean();
}
add_shortcode('friendlink_form_new', 'display_friendlink_form');

function submit_friendlink_callback() {
    if (is_user_logged_in()) {
        $websiteName = $_POST['websiteName'];
        $websiteDescription = $_POST['websiteDescription'];
        $websiteUrl = $_POST['websiteUrl'];
        $websiteIcon = $_POST['websiteIcon'];
        $postID = $_POST['post_id'];
        $userID = $_POST['user_id'];

        if (empty($websiteName) || empty($websiteDescription) || empty($websiteUrl) || empty($websiteIcon)) {
            echo json_encode(array('success' => false));
            wp_die();
        }

        $commentData = array(
            'comment_post_ID' => $postID,
            'comment_author' => get_userdata($userID)->display_name,
            'comment_content' => "网站名称:$websiteNamenn网站描述:$websiteDescriptionnn网站地址:$websiteUrlnn网站图标:$websiteIcon",
            'comment_type' => '',
            'comment_parent' => 0,
            'user_ID' => $userID,
        );

        $commentID = wp_new_comment($commentData);
        if ($commentID) {
            echo json_encode(array('success' => true));
        } else {
            echo json_encode(array('success' => false));
        }
    } else {
        echo json_encode(array('success' => false));
    }

    wp_die();
}
add_action('wp_ajax_submit_friendlink', 'submit_friendlink_callback');
add_action('wp_ajax_nopriv_submit_friendlink', 'submit_friendlink_callback');

2.在友链页面合适的地方添加[friendlink_form_new] 短代码。

 

提示:本文最后更新于2024年 5月 17日,如有错误或者已经失效,请留言告知。
THE END