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 '

' . get_the_title( $this->settings['404page_page_id'] ) . '

'; } // show content - Customizr Compatibility Mode function show404_customizr_mode( $content ) { return '
' . apply_filters( 'the_content', get_post_field( 'post_content', $this->settings['404page_page_id'] ) ) . '
'; $this->do_404page_action(); } // change article selectors - Customizr Compatibility Mode function show404articleselectors_customizr_mode( $selectors ) { return 'id="post-' . $this->settings['404page_page_id'] . '" ' . 'class="' . join( ' ', get_post_class( 'row-fluid', $this->settings['404page_page_id'] ) ) . '"'; } // init the admin section function admin_init() { $this->wp_url = 'https://wordpress.org/plugins/' . $this->plugin_slug; $this->my_url = 'http://smartware.cc/free-wordpress-plugins/' . $this->plugin_slug; $this->dc_url = 'http://smartware.cc/docs/' . $this->plugin_slug; load_plugin_textdomain( '404page' ); add_settings_section( '404page-settings', null, null, '404page_settings_section' ); register_setting( '404page_settings', '404page_page_id' ); register_setting( '404page_settings', '404page_hide' ); register_setting( '404page_settings', '404page_method' ); register_setting( '404page_settings', '404page_fire_error' ); 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_settings_field( '404page_settings_hide', __( 'Hide 404 page', '404page' ) . ' ' , array( $this, 'admin_hide' ), '404page_settings_section', '404page-settings', array( 'label_for' => '404page_hide' ) ); add_settings_field( '404page_settings_fire', __( 'Fire 404 error', '404page' ) . ' ' , array( $this, 'admin_fire404' ), '404page_settings_section', '404page-settings', array( 'label_for' => '404page_fire_error' ) ); add_settings_field( '404page_settings_method', __( 'Operating Method', '404page' ) . ' ' , array( $this, 'admin_method' ), '404page_settings_section', '404page-settings', array( 'label_for' => '404page_method' ) ); } // 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'] ) ); echo ''; echo ''; echo ''; echo '

 

'; } // 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' ) . ')

'; } } // handle the settings field method function admin_method() { if ( defined( 'CUSTOMIZR_VER' ) || defined( 'ICL_SITEPRESS_VERSION' ) || defined( 'POLYLANG_VERSION' ) || defined( 'SITEORIGIN_PANELS_VERSION' ) || class_exists( 'bbPress' ) ) { $dis = ' disabled="disabled"'; } else { $dis = ''; } echo '

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 Version: version; ?>

show_meta_boxes(); } ?>

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 '

'; } } // dismiss the notice (AJAX) function dismiss_admin_notice() { update_option( '404page_notice_dismissed', 'DISMISSED' ); } // Reset dismiss state for notice on plugin activation function init_admin_notice( $network_wide ) { global $wpdb; if ( is_multisite() && $network_wide ) { $current_blog = $wpdb->blogid; $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); foreach ( $blog_ids as $blog_id ) { switch_to_blog( $blog_id ); delete_option( '404page_notice_dismissed' ); } switch_to_blog( $current_blog ); } else { delete_option( '404page_notice_dismissed' ); } } // returns the id of the 404 page if one is defined, returns 0 if none is defined, returns -1 if the defined page id does not exist private function get_404page_id() { $pageid = get_option( '404page_page_id', 0 ); if ( $pageid != 0 ) { $page = get_post( $pageid ); if ( !$page || $page->post_status != 'publish' ) { $pageid = -1; } } return $pageid; } // returns the selected method private function get_404page_method() { if ( defined( 'ICL_SITEPRESS_VERSION' ) || defined( 'POLYLANG_VERSION' ) || defined( 'SITEORIGIN_PANELS_VERSION' ) || class_exists( 'bbPress' ) ) { // WPML or bbPress is active return 'CMP'; } else { return get_option( '404page_method', 'STD' ); } } // should we hide the selected 404 page from the page list? private function get_404page_hide() { return (bool)get_option( '404page_hide', false ); } // should we fire an 404 error if the selected page is accessed directly? private function get_404page_fire_error() { return (bool)get_option( '404page_fire_error', true ); } // make plugin expandable function do_404page_action() { do_action( '404page_after_404' ); } // show meta boxes function show_meta_boxes() { ?>

  •   
  •   
  •   
  •   
  •   

  •   
  •   
  •   
  •   

' . __( 'Settings', '404page' ) . '') ); } // uninstall plugin function uninstall() { if( is_multisite() ) { $this->uninstall_network(); } else { $this->uninstall_single(); } } // uninstall network wide function uninstall_network() { global $wpdb; $activeblog = $wpdb->blogid; $blogids = $wpdb->get_col( esc_sql( 'SELECT blog_id FROM ' . $wpdb->blogs ) ); foreach ($blogids as $blogid) { switch_to_blog( $blogid ); $this->uninstall_single(); } switch_to_blog( $activeblog ); } // uninstall single blog function uninstall_single() { foreach ( $this->settings as $key => $value) { delete_option( $key ); } } } $smart404page = new Smart404Page(); ?>