Load a specific post if user tries to access 404 page

This is the final code that works. I had to set $wp_query->is_single = true; manually: add_action( ‘template_redirect’, function(){ global $wp_query; if ( is_404() ){ $id = 1; // the post corresponding to hello-world if ( $id ) { $wp_query->is_404 = false; status_header(200); header(“HTTP/1.1 200 OK”); $wp_query->query(“page_id=$id&post_type=post”); $wp_query->the_post(); $wp_query->is_single = true; } } }, 999);

Creating FPDF directly in the browser using template_rediect

Use ob_start();, ob_clean(); before outputting the PDF to make any uwanted content is not written while cretaing PDF. add_action(“template_redirect”, “checkRedirect”); function checkRedirect() { $url = (isset($_SERVER[‘HTTPS’]) && $_SERVER[‘HTTPS’] === ‘on’ ? “https” : “http”) . “://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]”; $slug = strtolower(basename($url)); if ($slug == “pdf”) { ob_start(); require_once(getPath() . “fpdf.php”); $pdf = new \FPDF; $pdf->AddPage(); $pdf->SetFont(‘Arial’,’B’,16); $pdf->Cell(40,10,’Hello … Read more

Proper Way to Enqueue CSS for Front Page – template_redirect in functions.php?

Nope. Use the wp_enqueue_scripts hook directly for enqueing all styles and scripts. There’s no need to hook into template_include for that. template_include is specifically for modifying templates. //Adding and Encuing styles for Front Page add_action( ‘wp_enqueue_scripts’, ‘front_page_design’ ); function front_page_design(){ if ( is_front_page() || is_home()) { wp_enqueue_style( ‘home_page_style’, get_stylesheet_directory_uri() . ‘/index-style.css’, array(‘handle_of_main_style’) ); } } … Read more