Add .html to the URL of WordPress Pages

There may be requirement to change the wordpress url. The permalinks options of wordpress maynot be sometime sufficient as per client specification. To get .html the post can be done by changing permalinks in admin , but adding .html extension in the page url is not done by it. For that just add the below lines in functions.php of your theme

add_action('init', 'html_page_permalink', -1);
register_activation_hook(__FILE__, 'active');
register_deactivation_hook(__FILE__, 'deactive');
function html_page_permalink() {
	global $wp_rewrite;
	if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){
		$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
	}
}
add_filter('user_trailingslashit', 'no_page_slash',66,2);
function no_page_slash($string, $type){
	global $wp_rewrite;
	if ($wp_rewrite->using_permalinks() && $wp_rewrite->use_trailing_slashes==true && $type == 'page'){
		return untrailingslashit($string);
	}else{
		return $string;
	}
}
function active() {
	global $wp_rewrite;
	if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){
		$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
	}
	$wp_rewrite->flush_rules();
}
function deactive() {
	global $wp_rewrite;
	$wp_rewrite->page_structure = str_replace(".html","",$wp_rewrite->page_structure);
	$wp_rewrite->flush_rules();
}

If you arenot intrested to do so then you can install this plugin from https://wordpress.org/plugins/html-on-pages/
Above code are also taken from the that plugin

If you want to get .php then then just change .html to .php on above code.

3 thoughts on “Add .html to the URL of WordPress Pages”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.