加入游戏有点晚了,但我在使用 Pretty Links Lite 时遇到了同样的问题。您添加的漂亮链接越多,网站的速度就越慢,即使使用激进的缓存也是如此。
我的解决方案是创建一个名为 redirect 的自定义帖子类型并使用一些自定义字段(尽管我使用高级自定义字段插件以获得更优雅的后端体验)。然后只需添加一个与template_redirect 挂钩的快速函数,用于检查您的帖子类型。
唯一的缺点是您需要为您的 CPT 分配一个 slug,但您可以在注册函数中轻松自定义它。
这是我的代码:
function register_redirect_cpt {
register_post_type('redirect', array(
'label' => 'redirects',
'labels' => array(
'name' => 'Redirects',
'singular_name' => 'Redirect',
'add_new' => 'Add Redirect',
'add_new_item' => 'Add New Redirect',
'edit_item' => 'Edit Redirect',
'new_item' => 'New Redirect',
'view_item' => 'View Redirect',
'search_items' => 'Search Redirects',
'not_found' => 'No Redirects found',
'not_found_in_trash' => 'No Redirects found in Trash'
),
'description' => 'Pretty Redirects',
'public' => true,
'menu_position' => 5,
'supports' => array(
'title',
'author',
'custom-fields' // This is important!!!
),
'exclude_from_search' => true,
'has_archive' => false,
'query_var' => true,
'rewrite' => array(
'slug' => 'redirect',
'with_front' => false
)
));
}
add_action('init', 'register_redirect_cpt') ;
正如我所说,您可以使用自定义字段或 ACF 插件来设置一些元框——1 个用于公共链接,另一个用于真正的目的地。我假设您使用香草自定义字段。然后将其插入您的 functions.php 或主题函数文件:
function redirect_for_cpt() {
if (!is_singular('redirect')) // If it's not a redirect then don't redirect
return;
global $wp_query;
$redirect = isset($wp_query->post->ID) ? get_post_meta($wp_query->post->ID, '_true_destination', true) : home_url(); // If you forget to set a redirect then send visitors to the home page; at least we avoid 404s this way!
wp_redirect(esc_url_raw($redirect), 302);
exit;
}
add_action('template_redirect', 'redirect_for_cpt');