Creating a custom 403 error page to display
-
I am trying to get a custom 403 error page to display using my theme. In the folders I want to deny access I put a .htaccess file with this:
Options -Indexes
ErrorDocument 403 /wp-content/themes/mytheme/403.phpWhen I try it it gives me an error:
fatal error: Call to undefined function get_header()Is there away to get this to work? The code is the same for the 404 not found file and it works. Not sure why it can’t use the get_header() function.
-
403 files should lie outside of WordPress.
I figured that was the case. I had it ErrorDocument 403 /errors/forbidden.php
and it worked but it was a plain page. I wanted to incorporate it so it looks like the 404 page does.I’m not sure that WP can handle 403s in the way that you want. So the only other option is to literally style your 403 page to look like your theme.
I’ve written an article about this here.
Here are the brief instructions.
1. Create a copy of your 404.php file and name it 403.php. Edit this file with the message you’d like it to show.
2. Copy the code from the link I mentioned above and paste it in your functions.php file.
3. Edit the .htaccess file
ErrorDocument 403 /index.php?status=403
And before I forget you should do all these after creating a Child Theme to avoid these changes being overwritten by a theme update.
Website URL – https://www.christophereastman.com/
This is an incredibly handy post for creating a custom 403 error page. However, when I tried to follow the instructions on that link, I received the Fatal error: Call to undefined function get_header() message.
Here is my code for the 403.php page:
<?php /** * The template for displaying 403 pages (Forbidden). * */ ?> <?php get_header( ); ?> <section id="topAds"> <!-- <a href="#" title="Website Advertisement"><img src="images/sampleTopAd.png" alt="Advertisement" class="topAdvertisement"></a> --> </section> <section id="content"> <div id="pageContainer"> <div class="imageFor403"> This is my mascot - Destroyer X. </div> <h1 class="h1For403">Welcome to World 403</h1> <p> Welcome to the Error 403 page (a.k.a. World 403)! </p> <p> I wish I could say that you managed to win a special prize for accessing this page. However, the only function of this page is to let people know that you don't have permission to access a certain page or folder on the Web server. Some of the folders on the Web server aren't meant for public viewing, and that's why you're seeing this message. </p> <p> Since the Web is constantly changing, there will always be a chance that I created a newer page to display the content that you are looking for. With that being said, the easiest way to find the content that you are looking for would be to use the Website's search engine. </p> </div> </section> <?php get_sidebar( ); ?> <?php get_footer( ); ?> </body> </html>
And here is the code for functions.php:
<?php function register_my_menus( ) { register_nav_menus( array( 'top-menu' => __( 'Top Navigation Menu' ), 'bottom-menu' => __( 'Bottom Navigation Menu' ) ) ); } add_action( 'init', 'register_my_menus' ); /* IDs for sidebars must be in lowercase */ if ( function_exists( 'register_sidebar' ) ) register_sidebar( array( 'name' => __( 'Main Sidebar' ), 'id' => 'main-sidebar', 'description' => __( 'This is the widget area for the main sidebar', 'christophereastman' ), 'before_widget' => '<aside id="%1$s" class="%2$s">', 'after_widget' => '</aside>', 'before_title' => '<h3 class="widgettitle">', 'after_title' => '</h3>', ) ); /* Begin code to deregister style.css from Contact Form 7 plugin */ add_action( 'wp_print_styles', 'my_deregister_styles', 100 ); function my_deregister_styles( ) { wp_deregister_style( 'contact-form-7' ); } /* End code to deregister style.css from Contact Form 7 plugin */ /* Begin code to replace [...] in the Categories with Read More */ function new_excerpt_more( $more ) { global $post; return '... <a href="'. get_permalink( $post->ID ) . '">' . 'Read More »' . '</a>'; } add_action( 'excerpt_more', 'new_excerpt_more' ); /* End code to replace [...] in the Categories with Read More */ /* Begin code to prevent page scroll when clicking the more link */ function remove_more_link_scroll( $link ) { $link = preg_replace( '|#more-[0-9]+|', '', $link ); return $link; } add_filter( 'the_content_more_link', 'remove_more_link_scroll' ); /* End code to prevent page scroll when clicking the more link */ /* Begin code for custom Error 403 - Forbidden */ function custom_error_pages( ) { global $wp_query; if ( isset( $_REQUEST[ 'status' ] ) && $_REQUEST[ 'status' ] == 403 ) { $wp_query->is_404 = FALSE; $wp_query->is_page = TRUE; $wp_query->is_singular = TRUE; $wp_query->is_single = FALSE; $wp_query->is_home = FALSE; $wp_query->is_archive = FALSE; $wp_query->is_category = FALSE; add_filter( 'wp_title', 'custom_error_title', 65000, 2 ); add_filter( 'body_class', 'custom_error_class' ); status_header( 403 ); get_template_part( '403' ); exit; } } function custom_error_title( $title='', $sep='' ) { if ( isset( $_REQUEST[ 'status' ] ) && $_REQUEST[ 'status' ] == 403 ) { return "Forbidden ".$sep." ".get_bloginfo( 'name' ); } } function custom_error_class($classes) { if ( isset( $_REQUEST['status'] ) && $_REQUEST[ 'status' ] == 403 ) { $classes[] = "error403"; return $classes; } } add_action( 'wp', 'custom_error_pages' ); /* End code for custom Error 403 - Forbidden */ ?>
If you’re still available for this topic, Jesin A, and care to take a look at the code to see where I went wrong, I would gladly appreciate it.
Thank you very much for your time and cooperation.
I completely forgot to mention, the code works if I go to https://www.christophereastman.com/?status=403, but if I was to go to the folder where my image gallery is, I would receive the Fatal error: Call to undefined function get_header() error message.
May I know how you’ve specified the ErrorDocument in your .htaccess file.
I think you’ve done something like
ErrorDocument 403 /wp-content/themes/christophereastman/index.php?status=403
But you should only call the index.php file like this
ErrorDocument 403 /index.php?status=403
- The topic ‘Creating a custom 403 error page to display’ is closed to new replies.