WordPress 笔记

页面链接添加.html

// 页面链接添加html后缀
add_action('init', 'html_page_permalink', -1);
function html_page_permalink() {
    global $wp_rewrite;
    if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
    }
}

分类目录添加斜杠

//分类目录添加斜杠
function nice_trailingslashit($string, $type_of_url) {
    if ( $type_of_url != 'single' && $type_of_url != 'page' && $type_of_url != 'single_paged' )
        $string = trailingslashit($string);
    return $string;
}
add_filter('user_trailingslashit', 'nice_trailingslashit', 10, 2);

重命名上传文件

// 重命名WordPress上传的文件
function rename_uploaded_file($file) {
    $time = date("YmdHis");
    $unique_id = uniqid(); // 使用uniqid()函数生成唯一标识符
    $file_extension = pathinfo($file['name'], PATHINFO_EXTENSION);
    $file['name'] = $time . "_" . $unique_id . "." . $file_extension;
    return $file;
}
add_filter('wp_handle_upload_prefilter', 'rename_uploaded_file');

标签链接改成ID样式

add_action('generate_rewrite_rules','tag_rewrite_rules');
add_filter('term_link','tag_term_link',10,3);
add_action('query_vars', 'tag_query_vars');
function tag_rewrite_rules($wp_rewrite) {
  $new_rules = array(
  	'tag/(\d+)/feed/(feed|rdf|rss|rss2|atom).html' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
  	'tag/(\d+)/(feed|rdf|rss|rss2|atom).html' => 'index.php?tag_id=$matches[1]&feed=$matches[2]',
  	'tag/(\d+)/embed.html' => 'index.php?tag_id=$matches[1]&embed=true',
  	'tag/(\d+)/page/(\d+).html' => 'index.php?tag_id=$matches[1]&paged=$matches[2]',
  	'tag/(\d+).html' => 'index.php?tag_id=$matches[1]',
  );
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function tag_term_link($link,$term,$taxonomy) {
  if($taxonomy=='post_tag') {
  	return home_url('/tag/'.$term->term_id.'.html');
  }
  return $link;
}
function tag_query_vars($public_query_vars) {
  $public_query_vars[] = 'tag_id';
  return $public_query_vars;
}