put_contents( ARTTE_ROBOTS_FILE, $content, FS_CHMOD_FILE ); if ( $result ) { $wp_filesystem->chmod( ARTTE_ROBOTS_FILE, FS_CHMOD_FILE ); } return $result; } /** * Serve robots.txt dynamically if physical file not available. */ function artte_output_custom_robots_txt( $output, $public ) { if ( file_exists( ARTTE_ROBOTS_FILE ) ) { $content = file_get_contents( ARTTE_ROBOTS_FILE ); } else { $content = artte_get_default_robots_content(); } header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' ); header( 'Pragma: no-cache' ); return $content; } add_filter( 'robots_txt', 'artte_output_custom_robots_txt', 10, 2 ); /** * Admin menu registration. */ function artte_add_admin_menu() { add_menu_page( __( 'Robots.txt Editor', 'admin-robots-txt-editor' ), __( 'Robots.txt', 'admin-robots-txt-editor' ), 'manage_options', 'admin-robots-txt', 'artte_render_robots_editor', 'dashicons-media-text', 91 ); } add_action( 'admin_menu', 'artte_add_admin_menu' ); /** * Render the admin settings page. */ function artte_render_robots_editor() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to access this page.', 'admin-robots-txt-editor' ) ); } $message = ''; $success = false; // Save updated content. if ( isset( $_POST['robots_content'] ) && check_admin_referer( 'artte_update_robots' ) ) { $content = sanitize_textarea_field( wp_unslash( $_POST['robots_content'] ) ); if ( artte_write_robots_file( $content ) ) { $message = esc_html__( 'robots.txt has been updated successfully.', 'admin-robots-txt-editor' ); $success = true; } else { $message = esc_html__( 'Error: Unable to write robots.txt file. Please check file permissions (should be 644).', 'admin-robots-txt-editor' ); } } // Reset to default content. if ( isset( $_POST['reset_robots'] ) && check_admin_referer( 'artte_update_robots' ) ) { $default = artte_get_default_robots_content(); if ( artte_write_robots_file( $default ) ) { $message = esc_html__( 'robots.txt has been reset to default.', 'admin-robots-txt-editor' ); $success = true; } else { $message = esc_html__( 'Error: Unable to reset robots.txt file. Please check permissions.', 'admin-robots-txt-editor' ); } } // Load current content. $robots_content = file_exists( ARTTE_ROBOTS_FILE ) ? file_get_contents( ARTTE_ROBOTS_FILE ) : artte_get_default_robots_content(); ?>