plugin_name = '404page'; $this->plugin_slug = '404page'; $this->version = '2.1'; $this->get_settings(); $this->init(); } // get all settings private function get_settings() { $this->settings = array(); $this->settings['404page_page_id'] = $this->get_404page_id(); $this->settings['404page_hide'] = $this->get_404page_hide(); $this->settings['404page_fire_error'] = $this->get_404page_fire_error(); $this->settings['404page_method'] = $this->get_404page_method(); } // do plugin init private function init() { register_activation_hook( __FILE__, array( $this, 'init_admin_notice' ) ); if ( !is_admin() ) { add_action( 'init', array( $this, 'set_mode' ) ); } 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' ) ); if ( $this->settings['404page_hide'] and $this->settings['404page_page_id'] > 0 ) { add_action( 'pre_get_posts' ,array ( $this, 'exclude_404page' ) ); } if ( 'NOT_DISMISSED' == get_option( '404page_notice_dismissed', 'NOT_DISMISSED' ) ) { add_action( 'admin_notices', array( $this, 'admin_notice' ) ); add_action( 'wp_ajax_nopriv_404page_dismiss_admin_notice', array( $this, 'dismiss_admin_notice' ) ); add_action( 'wp_ajax_404page_dismiss_admin_notice', array( $this, 'dismiss_admin_notice' ) ); } } } // init filters function set_mode() { if ( defined( 'CUSTOMIZR_VER' ) ) { // Customizr Compatibility Mode add_filter( 'tc_404_header_content', array( $this, 'show404title_customizr_mode' ), 999 ); add_filter( 'tc_404_content', array( $this, 'show404_customizr_mode' ), 999 ); add_filter( 'tc_404_selectors', array( $this, 'show404articleselectors_customizr_mode' ), 999 ); } else { if ( $this->settings['404page_method'] != 'STD' ) { // Compatibility Mode add_filter( 'posts_results', array( $this, 'show404_compatiblity_mode' ), 999 ); } else { // Standard Mode add_filter( '404_template', array( $this, 'show404_standard_mode' ), 999 ); if ( $this->settings['404page_fire_error'] ) { add_action( 'template_redirect', array( $this, 'do_404_header_standard_mode' ) ); } } } } // show 404 page - Standard Mode function show404_standard_mode( $template ) { global $wp_query; $pageid = $this->settings['404page_page_id']; if ( $pageid > 0 ) { $wp_query = null; $wp_query = new WP_Query(); $wp_query->query( 'page_id=' . $pageid ); $wp_query->the_post(); $template = get_page_template(); rewind_posts(); add_filter( 'body_class', array( $this, 'add_404_body_class' ) ); $this->do_404page_action(); } return $template; } // show 404 page - Compatibility Mode function show404_compatiblity_mode( $posts ) { // remove the filter so we handle only the first query - no custom queries remove_filter( 'posts_results', array( $this, 'show404_compatiblity_mode' ), 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 ) ) { // we need to get the 404 page if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { // WPML is active $pageid = apply_filters( 'wpml_object_id', $pageid, 'page', true ); } elseif ( defined( 'POLYLANG_VERSION' ) ) { // Polylang is active $translatedpageid = pll_get_post( $pageid ); if ( !empty( $translatedpageid ) && 'publish' == get_post_status( $translatedpageid ) ) { $pageid = $translatedpageid; } } // as of v2.1 we do not alter the posts argument here because this does not work with SiteOrigin's Page Builder Plugin, template_include filter introduced $this->postid = $pageid; $this->template = get_page_template_slug( $pageid ); if ( $this->template == '' ) { $this->template = get_page_template(); } add_action( 'wp', array( $this, 'do_404_header' ) ); add_filter( 'body_class', array( $this, 'add_404_body_class' ) ); $posts[] = get_post( $pageid ); $this->do_404page_action(); } elseif ( 1 == count( $posts ) && 'page' == $posts[0]->post_type ) { // Do a 404 if the 404 page is opened directly if ( $this->settings['404page_fire_error'] ) { $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() ); } elseif ( defined( 'POLYLANG_VERSION' ) ) { // Polylang is active - get the post ID of the default language $curpageid = pll_get_post( $curpageid, pll_default_language() ); $pageid = pll_get_post( $pageid, pll_default_language() ); } if ( $pageid == $curpageid ) { add_action( 'wp', array( $this, 'do_404_header' ) ); add_filter( 'body_class', array( $this, 'add_404_body_class' ) ); $this->do_404page_action(); } } } } return $posts; } // send a 404 HTTP header - Standard Mode function do_404_header_standard_mode() { if ( is_page() && get_the_ID() == $this->settings['404page_page_id'] && !is_404() ) { status_header( 404 ); nocache_headers(); $this->do_404page_action(); } } // send a 404 HTTP header - Compatibility Mode 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(); } // adds the error404 class to the body classes function add_404_body_class( $classes ) { $classes[] = 'error404'; return $classes; } // show title - Customizr Compatibility Mode function show404title_customizr_mode( $title ) { return '
'; } // handle the settings field hide function admin_hide() { echo '
settings['404page_hide'], false ) . '/>'; echo '
'; echo '' . __( 'For Administrators the page is always visible.', '404page' ) . '
'; } // handle the settings field fire 404 error function admin_fire404() { echo 'settings['404page_fire_error'], false ) . '/>'; echo '
'; echo '' . __( 'Uncheck this if you want the selected page to be accessible.', '404page' ) . '
'; if ( function_exists( 'wpsupercache_activate' ) ) { echo ' ' . __( 'WP Super Cache Plugin detected', '404page' ) . '. ' . __ ( 'If the page you selected as 404 error page is in cache, always a HTTP code 200 is sent. To avoid this and send a HTTP code 404 you have to exlcude this page from caching', '404page' ) . ' (' . __( 'Click here', '404page' ) . ').
(' . __( 'Read more', '404page' ) . ')
settings['404page_method'], false ) . $dis . ' />'; echo '
'; echo 'settings['404page_method'], false ) . $dis . '/>'; echo '
'; if ( defined( 'CUSTOMIZR_VER' ) ) { echo '' . __( 'Customizr Theme detected. The 404page Plugin works in Customizr Compatibility Mode.', '404page' ) . ' (' . __( 'Read more', '404page' ) . ')
'; } elseif ( defined( 'ICL_SITEPRESS_VERSION' ) ) { echo '' . __( 'WPML Plugin detected. Operating Method set to Compatibility Mode automatically.', '404page' ) . ' (' . __( 'Read more', '404page' ) . ')
'; } elseif ( defined( 'POLYLANG_VERSION' ) ) { echo '' . __( 'Polylang Plugin detected. Operating Method set to Compatibility Mode automatically.', '404page' ) . ' (' . __( 'Read more', '404page' ) . ')
'; } elseif ( defined( 'SITEORIGIN_PANELS_VERSION' ) ) { echo '' . __( 'Page Builder by SiteOrigin Plugin detected. Operating Method set to Compatibility Mode automatically.', '404page' ) . ' (' . __( 'Read more', '404page' ) . ')
'; } elseif ( class_exists( 'bbPress' ) ) { echo '' . __( 'bbPress Plugin detected. Operating Method set to Compatibility Mode automatically.', '404page' ) . ' (' . __( 'Read more', '404page' ) . ')
'; } else { echo '' . __( 'Standard Mode uses the WordPress Template System and should work in most cases. If the 404page plugin does not work properly, probably you are using a theme or plugin that modifies the WordPress Template System. In this case the Compatibility Mode maybe can fix the problem, although it cannot be guaranteed that every possible configuration can be handled by Compatibility Mode. Standard Mode is the recommended method, only switch to Compatibility Mode if you have any problems.', '404page' ) . '
'; } } // this function hides the selected page from the list of pages function exclude_404page( $query ) { global $pagenow; if( 'edit.php' == $pagenow && ( get_query_var( 'post_type' ) && 'page' == get_query_var( 'post_type' ) ) && !current_user_can( 'create_users' ) ) { $query->set( 'post__not_in', array( $this->settings['404page_page_id'] ) ); } return $query; } // 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', 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: ';
if ( defined( 'CUSTOMIZR_VER' ) ) {
// Customizr Theme
_e( 'Customizr Theme detected. The 404page Plugin works in Customizr Compatibility Mode.', '404page' );
} elseif ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
// WPML
_e( 'WPML Plugin detected. Operating Method is set to Compatibility Mode automatically to make the 404page Plugin work with WPML.', '404page' );
} elseif ( defined( 'POLYLANG_VERSION' ) ) {
// Polylang
_e( 'Polylang Plugin detected. Operating Method is set to Compatibility Mode automatically to make the 404page Plugin work with Polylang.', '404page' );
} elseif ( defined( 'SITEORIGIN_PANELS_VERSION' ) ) {
// Page Builder by SiteOrigin
_e( 'Page Builder by SiteOrigin Plugin detected. Operating Method is set to Compatibility Mode automatically to make the 404page Plugin work with Page Builder by SiteOrigin.', '404page' );
} elseif ( class_exists( 'bbPress' ) ) {
// bbPress
_e( 'bbPress Plugin detected. Operating Method is set to Compatibility Mode automatically to make the 404page Plugin work with bbPress.', '404page' );
} elseif ( function_exists( 'wpsupercache_activate' ) ) {
// WP Super Cache
_e( 'WP Super Cache Plugin detected. Please check Settings!', '404page' );
} else {
_e( 'Please check Operating Method.', '404page' );
}
echo '
' . __( 'Settings', '404page' ) . '';
echo '