htaccess redirect own custom plugin issues
-
I am needing some help, think this will be the right forum but if not please point me in the right direction.
I been trying to add some function to my website with the help of Chat GPT from OpenAi but i get a different code each time so I am a bit lost with it all. I was hoping that I could then use this to learn from as I would know what all the functions/features were.
Can anyone have a look through the code and tell me what is going on,
It was supposed to have a lot of options within the settings page such as
As if backup has been done, if not do they want to Create backup
Then a schedule for auto scanning for changes
a scan now button
Then display all links found with changes and give option to redirect to the new url from what was changes, redirect to home or leave as it is.
I know that the code is missing a lot but I was impressed it worked after I debugged it.<?php /** * Plugin Name: Htaccess Redirect * Description: A plugin to monitor all URLs of the site and find any that are broken, and create a 301 redirect in the htaccess file to the new location. * Version: 1.0.1 * Author: OpenAI, HostMi * Author URI: https://openai.com * License: GPL2 */ // Make sure we are in the WordPress context if ( ! defined( 'ABSPATH' ) ) { exit; } // Register the options page and its settings add_action( 'admin_menu', 'htaccess_redirect_admin_menu' ); function htaccess_redirect_admin_menu() { add_options_page( 'Htaccess Redirect', 'Htaccess Redirect', 'manage_options', 'htaccess-redirect', 'htaccess_redirect_options_page' ); add_action( 'admin_init', 'htaccess_redirect_settings' ); } function htaccess_redirect_settings() { // Register the plugin's settings register_setting( 'htaccess_redirect_options', 'htaccess_redirect_options', 'htaccess_redirect_options_validate' ); // Add a section for the plugin's general settings add_settings_section( 'htaccess_redirect_section_general', 'General Settings', 'htaccess_redirect_section_general_cb', 'htaccess-redirect' ); // Add a field for the "Backup Htaccess File" setting add_settings_field( 'htaccess_redirect_field_backup', 'Backup Htaccess File', 'htaccess_redirect_field_backup_cb', 'htaccess-redirect', 'htaccess_redirect_section_general' ); // Add a field for the "Redirect Type" setting add_settings_field( 'htaccess_redirect_field_redirect_type', 'Redirect Type', 'htaccess_redirect_field_redirect_type_cb', 'htaccess-redirect', 'htaccess_redirect_section_general' ); // Add a field for the "Custom URL" setting add_settings_field( 'htaccess_redirect_field_custom_url', 'Custom URL', 'htaccess_redirect_field_custom_url_cb', 'htaccess_redirect_section_general' ); } // Render the options page function htaccess_redirect_options_page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( 'You do not have sufficient permissions to access this page.' ); } ?> <div class="wrap"> <h1>Htaccess Redirect</h1> <form action="options.php" method="post"> <?php settings_fields( 'htaccess_redirect_options' ); do_settings_sections( 'htaccess-redirect' ); submit_button(); ?> </form> </div> <?php } // Render the "Backup Htaccess File" field function htaccess_redirect_field_backup_cb() { $options = get_option( 'htaccess_redirect_options' ); $backup_htaccess = ( isset( $options['backup_htaccess'] ) && 1 === $options['backup_htaccess'] ); ?> <input type="checkbox" name="htaccess_redirect_options[backup_htaccess]" value="1" <?php checked( $backup_htaccess ); ?> /> <?php } // Render the "Redirect Type" field function htaccess_redirect_field_redirect_type_cb() { $options = get_option( 'htaccess_redirect_options' ); $redirect_type = ( isset( $options['redirect_type'] ) ) ? esc_attr( $options['redirect_type'] ) : ''; ?> <input type="radio" name="htaccess_redirect_options[redirect_type]" value="301" <?php checked( '301', $redirect_type ); ?> /> 301 Moved Permanently<br> <input type="radio" name="htaccess_redirect_options[redirect_type]" value="302" <?php checked( '302', $redirect_type ); ?> /> 302 Found<br> <input type="radio" name="htaccess_redirect_options[redirect_type]" value="307" <?php checked( '307', $redirect_type ); ?> /> 307 Temporary Redirect<br> <input type="radio" name="htaccess_redirect_options[redirect_type]" value="308" <?php checked( '308', $redirect_type ); ?> /> 308 Permanent Redirect <?php } // Render the "Custom URL" field function htaccess_redirect_field_custom_url_cb() { $options = get_option( 'htaccess_redirect_options' ); $custom_url = ( isset( $options['custom_url'] ) ) ? esc_url( $options['custom_url'] ) : ''; ?> <input type="text" name="htaccess_redirect_options[custom_url]" value="<?php echo esc_url( $custom_url ); ?>" class="regular-text" /> <?php } // Validate the plugin's options function htaccess_redirect_options_validate( $input ) { $output = array(); if ( isset( $input['backup_htaccess'] ) && 1 === $input['backup_htaccess'] ) { $output['backup_htaccess'] = 1; } if ( isset( $input['redirect_type'] ) && in_array( $input['redirect_type'], array( '301', '302', '307', '308' ), true ) ) { $output['redirect_type'] = $input['redirect_type']; } if ( isset( $input['custom_url'] ) && '' !== $input['custom_url'] ) { $output['custom_url'] = esc_url_raw( $input['custom_url'] ); } return $output; } // Monitor all URLs of the site and find any that are broken add_action( 'wp_footer', 'htaccess_redirect_find_broken_urls' ); function htaccess_redirect_find_broken_urls() { // Get the plugin's options $options = get_option( 'htaccess_redirect_options' ); // Check if the "Backup Htaccess File" option is enabled if ( isset( $options['backup_htaccess'] ) && 1 === $options['backup_htaccess'] ) { // Backup the htaccess file htaccess_redirect_backup_htaccess(); } // Get all the URLs of the site $urls = htaccess_redirect_get_site_urls(); // Check each URL for 404 errors foreach ( $urls as $url ) { $response = wp_remote_get( $url, array( 'timeout' => 5 ) ); if ( is_wp_error( $response ) || 404 === wp_remote_retrieve_response_code( $response ) ) { // Add a redirect for the broken URL htaccess_redirect_add_redirect( $url ); } } } // Backup the htaccess file function htaccess_redirect_backup_htaccess() { // Get the path to the htaccess file $htaccess_file = get_home_path() . '.htaccess'; // Check if the htaccess file exists if ( file_exists( $htaccess_file ) ) { // Backup the htaccess file copy( $htaccess_file, $htaccess_file . '-backup' ); } } // Get all the URLs of the site function htaccess_redirect_get_site_urls() { // Initialize the array of URLs $urls = array(); // Get all the posts of the site $posts = get_posts( array( 'post_type' => 'any', 'post_status' => 'publish', 'posts_per_page' => -1, ) ); // Add the permalinks of the posts to the array of URLs foreach ( $posts as $post ) { $urls[] = get_permalink( $post ); } // Return the array of URLs return $urls; } // Add a redirect for the specified URL function htaccess_redirect_add_redirect( $url ) { // Get the plugin's options $options = get_option( 'htaccess_redirect_options' ); // Get the path to the htaccess file $htaccess_file = get_home_path() . '.htaccess'; // Check if the htaccess file exists if ( file_exists( $htaccess_file ) ) { // Get the contents of the htaccess file $htaccess_contents = file_get_contents( $htaccess_file ); // Check if the redirect already exists in the htaccess file if ( false === strpos( $htaccess_contents, 'RedirectMatch 301 ' . $url ) ) { // Check if a custom URL is specified if ( isset( $options['custom_url'] ) && '' !== $options['custom_url'] ) { // Use the custom URL as the destination for the redirect $redirect_url = $options['custom_url']; } else { // Use the homepage as the destination for the redirect $redirect_url = home_url(); } // Add the redirect to the htaccess file file_put_contents( $htaccess_file, "RedirectMatch {$options['redirect_type']} {$url} {$redirect_url}\n", FILE_APPEND ); } } } // The code to be executed when the plugin is activated function htaccess_redirect_activate() { // Perform any necessary actions when the plugin is activated } // The code to be executed when the plugin is deactivated function htaccess_redirect_deactivate() { // Perform any necessary actions when the plugin is deactivated } // The code to be executed when the plugin is uninstalled function htaccess_redirect_uninstall() { // Perform any necessary actions when the plugin is uninstalled } // Register the activation hook register_activation_hook( __FILE__, 'htaccess_redirect_activate' ); // Register the deactivation hook register_deactivation_hook( __FILE__, 'htaccess_redirect_deactivate' ); // Register the uninstall hook register_uninstall_hook( __FILE__, 'htaccess_redirect_uninstall' );
- The topic ‘htaccess redirect own custom plugin issues’ is closed to new replies.