plugin_name = '404page'; $this->plugin_slug = '404page'; $this->version = '2.0'; $this->get_settings(); $this->init(); } // get all settings private function get_settings() { $this->settings = array(); $this->settings['404page_page_id'] = $this->get_404page_id(); } private function init() { if ( !is_admin() ) { add_filter( 'posts_results', array( $this, 'show404' ), 999 ); } else { add_action( 'admin_init', array( $this, 'admin_init' ) ); add_action( 'admin_menu', array( $this, 'admin_menu' ) ); add_action( 'admin_head', array( $this, 'admin_css' ) ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_settings_link' ) ); } } // redirect 404 page - compatibility mode - introduced in version 2.0 function show404( $posts ) { global $wp_query; // remove the filter so we handle only the first query - no custom queries remove_filter( 'posts_results', array( $this, 'show404' ), 999 ); $pageid = $this->settings['404page_page_id']; if ( 0 != $pageid ) { if ( empty( $posts ) && is_main_query() && !is_robots() && !is_home() && !is_feed() && !is_search() &&( !defined('DOING_AJAX') || !DOING_AJAX ) ) { // show the 404 page if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { // WPML is active $pageid = apply_filters( 'wpml_object_id', $pageid, 'page', true ); } $posts[] = get_post( $pageid ); add_action( 'wp', array( $this, 'do_404_header' ) ); add_filter( 'body_class', array( $this, 'add_404_body_class' ) ); } elseif ( 1 == count( $posts ) && 'page' == $posts[0]->post_type ) { // Do a 404 if the 404 page is opened directly $curpageid = $posts[0]->ID; if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { // WPML is active - get the post ID of the default language global $sitepress; $curpageid = apply_filters( 'wpml_object_id', $curpageid, 'page', $sitepress->get_default_language() ); $pageid = apply_filters( 'wpml_object_id', $pageid, 'page', $sitepress->get_default_language() ); } if ( $pageid == $curpageid ) { add_action( 'wp', array( $this, 'do_404_header' ) ); add_filter( 'body_class', array( $this, 'add_404_body_class' ) ); } } } return $posts; } function do_404_header() { // remove the action so we handle only the first query - no custom queries remove_action( 'wp', array( $this, 'do_404_header' ) ); status_header( 404 ); nocache_headers(); } function add_404_body_class( $classes ) { // adds the error404 class to the body classes $classes[] = 'error404'; return $classes; } // init the admin section function admin_init() { load_plugin_textdomain( '404page' ); add_settings_section( '404page-settings', null, null, '404page_settings_section' ); register_setting( '404page_settings', '404page_page_id' ); add_settings_field( '404page_settings_404page', __( 'Page to be displayed as 404 page', '404page' ) . ' ' , array( $this, 'admin_404page' ), '404page_settings_section', '404page-settings', array( 'label_for' => '404page_page_id' ) ); } // add css function admin_css() { echo ''; } // handle the settings field page id function admin_404page() { if ( $this->settings['404page_page_id'] < 0 ) { echo '
' . __( 'The page you have selected as 404 page does not exist anymore. Please choose another page.', '404page' ) . '
'; } wp_dropdown_pages( array( 'name' => '404page_page_id', 'id' => 'select404page', 'echo' => 1, 'show_option_none' => __( '— NONE (WP default 404 page) —', '404page'), 'option_none_value' => '0', 'selected' => $this->settings['404page_page_id'] ) ); } // adds the options page to admin menu function admin_menu() { $page_handle = add_theme_page ( __( '404 Error Page', "404page" ), __( '404 Error Page', '404page' ), 'manage_options', '404pagesettings', array( $this, 'admin_page' ) ); add_action( 'admin_print_scripts-' . $page_handle, array( $this, 'admin_js' ) ); } // adds javascript to the 404page settings page function admin_js() { wp_enqueue_script( '404pagejs', plugins_url( '/404page.js', __FILE__ ), 'jquery', $this->version, true ); } // creates the options page function admin_page() { if ( !current_user_can( 'manage_options' ) ) { wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); } ?>

404page


Plugin Version: version; ?>

show_meta_boxes(); } ?>

post_status != 'publish' ) { $pageid = -1; } } return $pageid; } // show meta boxes function show_meta_boxes() { ?>

  •   
  •   
  •   
  •   
  •   

  •   
  •   
  •   
  •   
' . __( 'Settings', '404page' ) . '') ); } } $smart404page = new Smart404Page(); ?>