options = get_option( self::opt, array() ); $this->table = $GLOBALS['wpdb']->prefix . '404_log'; if( !isset( $this->options['db_version'] ) || $this->options['db_version'] < self::db_version ) { // upgrade placeholder $this->install_table(); $defaults = array( 'max_entries' => 500, 'also_record' => array( 'ip', 'ua', 'ref' ), 'ignore_bots' => false, 'only_w_ref' => false ); foreach( $defaults as $k => $v ) if( !isset( $this->options[$k] ) ) $this->options[$k] = $v; $this->options['db_version'] = self::db_version; update_option( self::opt, $this->options ); } add_action( 'template_redirect', array( $this, 'log_404s' ) ); if( is_admin() ) { add_action( 'admin_menu', array( $this, 'settings_menu' ) ); add_action( 'admin_head', array( $this, 'load_table' ) ); } } private function install_table() { // remember, two spaces after PRIMARY KEY otherwise WP borks $sql = "CREATE TABLE $this->table ( id BIGINT NOT NULL AUTO_INCREMENT, date DATETIME NOT NULL, url VARCHAR(512) NOT NULL, ref VARCHAR(512) NOT NULL default '', ip VARCHAR(40) NOT NULL default '', ua VARCHAR(512) NOT NULL default '', PRIMARY KEY (id) );"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } function settings_menu() { add_submenu_page('tools.php', '404 Error Log', '404 Error Log', 'manage_options', '404_error_log', array( $this, 'settings_page') ); // Register a page for CSV add_submenu_page('tools.php', '404 Error Log CSV', '404 Error Log CSV', 'manage_options', '404_error_log_csv', array( $this, 'csv') ); // ...But hide it remove_submenu_page('tools.php', '404_error_log_csv'); } function load_table(){ // need to load the list table early for WP to catch it if( get_current_screen()->id == 'tools_page_404_error_log' && empty( $_GET['view'] ) ) { require_once( dirname( __FILE__ ) . '/includes/class-log-404-list-table.php' ); $this->list_table = new Log_404_List_Table( $this->options['also_record'] ); } } function csv() { global $wpdb; if( isset( $_GET['csv'] ) && isset( $_GET['noheader'] ) && check_admin_referer('404_error_log_csv') ) { $orderby = ( isset( $_GET['orderby'] ) && in_array( $_GET['orderby'], array( 'date', 'url', 'ua', 'ref', 'ip' ) ) ) ? $_GET['orderby'] : 'id'; $order = ( isset( $_GET['order'] ) && strtolower( $_GET['order'] == 'asc' ) ) ? 'ASC' : 'DESC'; $rows = $wpdb->get_results( "SELECT date, url, ref, ip, ua FROM $this->table ORDER BY $orderby $order", ARRAY_N ); $fp = fopen('php://output', 'w'); $headers = array('Date', 'URL', 'Referrer', 'IP Address', 'User Agent'); if($rows && $fp){ header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="404_error_log.csv"'); header('Cache-Control: private, max-age=0'); fputcsv($fp, $headers); foreach($rows as $row){ fputcsv($fp, $row); } } else { header('Content-Type: text/plain'); echo 'An error occurred when generating the CSV.'; } exit; } } function settings_page() { ?>
You do not currently have pretty permalinks enabled on your site. This means that WordPress does not handle requests for pages that are not found on your site (your web server handles them directly), and so this plugin cannot log them. You need to be using pretty permalinks in order for this plugin to work.
Warning: It seems that a caching/performance plugin is active on this site. This plugin has only been tested with the following caching plugins:
Other caching plugins may cache responses to requests for pages that don't exist, in which case this plugin will not be able to intercept the requests and log them.
Options updated.