-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-pingbacks.php
More file actions
153 lines (143 loc) · 6.01 KB
/
add-pingbacks.php
File metadata and controls
153 lines (143 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/*
* Plugin Name
*
* @package AddPingbacks
* @author simonquasar
* @copyright 2026 simonquasar
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: Add Pingbacks
* Plugin URI: https://github.com/simonquasar/add-pingbacks
* Description: Manually add a Pingback to any post.
* Version: 1.2.3
* Requires at least: 5.0
* Requires PHP: 5.6
* Author: simonquasar
* Author URI: https://www.simonquasar.net/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: add-pingbacks
*/
defined('ABSPATH') || exit;
add_action('admin_menu', function() {
add_submenu_page(
'edit-comments.php',
'Add Pingbacks',
'Add Pingbacks',
'manage_options',
'add-pingbacks',
'render_pingbacks_page'
);
});
function render_pingbacks_page() {
if (!current_user_can('manage_options')) {
return;
}
if (isset($_POST['submit_pingback']) && check_admin_referer('add_pingback_action', 'add_pingback_nonce')) {
$post_id = filter_var($_POST['post_id'], FILTER_VALIDATE_INT);
$url = esc_url_raw($_POST['url']);
$author = sanitize_text_field($_POST['author']);
$content = sanitize_textarea_field($_POST['content']);
if ($post_id && $url && $content) {
$host = parse_url($url, PHP_URL_HOST);
wp_insert_comment(array(
'comment_post_ID' => $post_id,
'comment_author' => $author ? $author : $host,
'comment_author_url' => $url,
'comment_content' => $content,
'comment_type' => 'pingback',
'comment_approved' => 1
));
echo '<div class="notice notice-success"><p>' . esc_html__('Pingback added!', 'add-pingbacks') . '</p></div>';
}
}
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<form method="post">
<?php wp_nonce_field('add_pingback_action', 'add_pingback_nonce'); ?>
<table class="form-table">
<tr>
<td colspan="2">
<p class="description" style="text-align: right; font-style: italic; color: #666;">
made simple by <a href="https://www.simonquasar.net" target="_blank">simonquasar</a> since 2014
</p>
</td>
</tr>
<tr>
<th><label for="post-type">Post Type <span style="color:red">*</span></label></th>
<td>
<select id="post-type" style="max-width: 95%;">
<?php
$types = get_post_types(array('public' => true), 'objects');
foreach ($types as $type) {
echo '<option value="' . esc_attr($type->name) . '">' . esc_html($type->label) . '</option>';
}
?>
</select>
</td>
</tr>
<tr>
<th><label for="posts">Post <span style="color:red">*</span></label></th>
<td>
<select name="post_id" id="posts" style="max-width: 95%;" required>
<?php
$posts = get_posts(array('post_type' => 'post', 'posts_per_page' => -1));
foreach ($posts as $post) {
echo '<option value="' . esc_attr($post->ID) . '">' . esc_html($post->post_title) . '</option>';
}
?>
</select>
</td>
</tr>
<tr>
<th>URL <span style="color:red">*</span></th>
<td><input type="url" name="url" class="regular-text" style="width: 95%;" placeholder="https://example.com/your-article" required></td>
</tr>
<tr>
<th>Author</th>
<td><input type="text" name="author" class="regular-text" style="width: 95%;" placeholder="Author (optional)"></td>
</tr>
<tr>
<th>Content <span style="color:red">*</span></th>
<td><textarea name="content" style="width: 95%; height: 200px;" placeholder="Enter any referring content here..." required></textarea></td>
</tr>
<tr>
<th></th>
<td><input type="submit" name="submit_pingback" class="button button-primary" value="<?php echo esc_attr__('Add Pingback', 'text-domain'); ?>"></td>
</tr>
</table>
</form>
</div>
<script>
jQuery(document).ready(function($) {
$('#post-type').on('change', function() {
var type = $(this).val();
var data = {
action: 'get_posts',
type: type,
security: '<?php echo wp_create_nonce("get_posts_nonce"); ?>'
};
$.post(ajaxurl, data, function(response) {
$('#posts').html(response);
});
});
});
</script>
<?php
}
add_action('wp_ajax_get_posts', function() {
check_ajax_referer('get_posts_nonce', 'security');
$type = sanitize_text_field(isset($_POST['type']) ? $_POST['type'] : '');
if (empty($type)) {
wp_send_json_error('Invalid post type');
return;
}
$posts = get_posts(array('post_type' => $type, 'posts_per_page' => -1));
foreach ($posts as $post) {
echo '<option value="' . esc_attr($post->ID) . '">' . esc_html($post->post_title) . '</option>';
}
wp_die();
});