Chào bạn, chức năng thống kê và phân loại liên kết trong WordPress là công cụ hữu ích giúp bạn quản lý các liên kết của bài viết, trang, và từ khóa một cách hiệu quả.
Chức năng này cho phép bạn dễ dàng thu thập và phân loại tất cả các liên kết có trong website của mình, bao gồm:
Dưới đây là một đoạn mã mẫu cho chức năng này. Bạn có thể tham khảo và phát triển thêm
<?php
// Register a new menu page in the WordPress admin
add_action('admin_menu', 'all_links_menu_page');
function all_links_menu_page() {
add_menu_page(
'All Links', // Page title
'All Links', // Menu title
'manage_options', // Capability
'all-links', // Menu slug
'all_links_page_content', // Callback function
'dashicons-admin-links', // Icon
6 // Position
);
}
function all_links_page_content() {
?>
<div class="wrap">
<h1>All Links</h1>
<h2>Posts Links</h2>
<textarea id="posts-links" rows="10" style="width: 100%;"><?php echo get_all_post_links(); ?></textarea>
<button onclick="copyToClipboard('posts-links')">Copy All Post Links</button>
<button onclick="downloadAsTxt('posts-links', 'posts-links.txt')">Download Post Links</button>
<h2>Pages Links</h2>
<textarea id="pages-links" rows="10" style="width: 100%;"><?php echo get_all_page_links(); ?></textarea>
<button onclick="copyToClipboard('pages-links')">Copy All Page Links</button>
<button onclick="downloadAsTxt('pages-links', 'pages-links.txt')">Download Page Links</button>
<h2>Tags Links</h2>
<textarea id="tags-links" rows="10" style="width: 100%;"><?php echo get_all_tag_links(); ?></textarea>
<button onclick="copyToClipboard('tags-links')">Copy All Tag Links</button>
<button onclick="downloadAsTxt('tags-links', 'tags-links.txt')">Download Tag Links</button>
<h2>All Posts, Pages, and Tags Links</h2>
<textarea id="all-links" rows="10" style="width: 100%;"><?php echo get_all_links(); ?></textarea>
<button onclick="copyToClipboard('all-links')">Copy All Links</button>
<button onclick="downloadAsTxt('all-links', 'all-links.txt')">Download All Links</button>
<!-- New section to select a specific date -->
<h2>Links Created After a Specific Date</h2>
<label for="date">Select a date:</label>
<input type="date" id="date" name="date">
<button onclick="getLinksFromDate()">Get Links</button>
<textarea id="recent-links" rows="10" style="width: 100%;"></textarea>
<button onclick="copyToClipboard('recent-links')">Copy Recent Links</button>
<button onclick="downloadAsTxt('recent-links', 'recent-links.txt')">Download Recent Links</button>
</div>
<script>
function copyToClipboard(elementId) {
var copyText = document.getElementById(elementId);
copyText.select();
copyText.setSelectionRange(0, 99999); // For mobile devices
document.execCommand("copy");
alert("Copied the links: " + copyText.value);
}
function getLinksFromDate() {
var selectedDate = document.getElementById('date').value;
if (!selectedDate) {
alert('Please select a valid date');
return;
}
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
var data = {
'action': 'get_links_from_date',
'date': selectedDate
};
jQuery.post(ajaxurl, data, function(response) {
document.getElementById('recent-links').value = response;
});
}
function downloadAsTxt(elementId, filename) {
var text = document.getElementById(elementId).value;
var blob = new Blob([text], { type: 'text/plain' });
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = filename;
link.click();
}
</script>
<?php
}
function get_all_post_links() {
$posts = get_posts(array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => -1
));
$links = '';
foreach ($posts as $post) {
$links .= get_permalink($post) . "\n";
}
return $links;
}
function get_all_page_links() {
$pages = get_pages(array(
'post_status' => 'publish'
));
$links = '';
foreach ($pages as $page) {
$links .= get_permalink($page) . "\n";
}
return $links;
}
function get_all_tag_links() {
$tags = get_terms(array(
'taxonomy' => 'post_tag',
'hide_empty' => true
));
$links = '';
foreach ($tags as $tag) {
$links .= get_tag_link($tag) . "\n";
}
return $links;
}
function get_all_links() {
$all_links = get_all_post_links() . get_all_page_links() . get_all_tag_links();
return $all_links;
}
// New function to get links created after a specific date
add_action('wp_ajax_get_links_from_date', 'get_links_from_date');
function get_links_from_date() {
$date = sanitize_text_field($_POST['date']);
// Create a date range for the selected day
$start_date = date('Y-m-d 00:00:00', strtotime($date));
$end_date = date('Y-m-d 23:59:59', strtotime($date));
$args = array(
'date_query' => array(
array(
'after' => $start_date,
'before' => $end_date,
'inclusive' => true
),
),
'post_status' => 'publish',
'posts_per_page' => -1,
);
// Get posts created on the selected date
$posts = get_posts($args);
$links = '';
$tags = array(); // Initialize an array to collect tags
foreach ($posts as $post) {
$links .= get_permalink($post) . "\n";
$post_tags = wp_get_post_tags($post->ID); // Get tags for the post
foreach ($post_tags as $tag) {
$tags[] = get_tag_link($tag->term_id); // Collect tag links
}
}
// Remove duplicate tags
$tags = array_unique($tags);
$links .= "\nTags:\n" . implode("\n", $tags); // Append tags to links
echo $links;
wp_die();
}
Với đoạn mã trên, bạn có thể tạo một trang trong bảng điều khiển WordPress để hiển thị tất cả các liên kết của bài viết, trang, và thẻ.
Giza Data không chỉ mang đến cho bạn một chức năng mạnh mẽ mà còn cung cấp dịch vụ NVMe hosting chất lượng cao, giúp tối ưu hóa trải nghiệm WordPress của bạn. Dịch vụ của Giza Data đảm bảo:
Để biết thêm chi tiết về dịch vụ NVMe hosting chất lượng cao, hãy truy cập Giza Data.