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 '