path = plugin_dir_path( __FILE__ ); $this->artesans_plugin_name = "Artesans Search Redirect"; $this->artesans_plugin_menu = "Artesans Search Redirect"; $this->artesans_plugin_slug = "artesans-search-redirect"; $this->artesans_plugin_ref = "artesans_search_redirect"; add_action( 'plugins_loaded', array($this, 'setup_plugin') ); add_action( 'admin_notices', array($this,'admin_notices'), 11 ); add_action( 'network_admin_notices', array($this, 'admin_notices'), 11 ); add_action( 'admin_init', array($this,'register_settings_fields') ); add_action( 'admin_menu', array($this,'register_settings_page'), 20 ); add_action( 'admin_enqueue_scripts', array($this, 'admin_assets') ); add_action( 'admin_print_footer_scripts', array($this, 'add_javascript'), 100 ); } function add_javascript(){ echo ""; return; } /********************************* * NOTICES & LOCALIZATION */ function setup_plugin(){ load_plugin_textdomain( $this->artesans_plugin_slug, false, $this->path."lang/" ); } function admin_notices(){ $message = ''; if ( $message != '' ) { echo "

$message

"; } } function admin_assets($page){ wp_register_style( $this->artesans_plugin_slug, plugins_url("css/ns-custom.css",__FILE__), false, '1.0.0' ); wp_register_script( $this->artesans_plugin_slug, plugins_url("js/ns-custom.js",__FILE__), false, '1.0.0' ); if( strpos($page, $this->artesans_plugin_ref) !== false ){ wp_enqueue_style( $this->artesans_plugin_slug ); wp_enqueue_script( $this->artesans_plugin_slug ); } } /********************************** * SETTINGS PAGE */ function register_settings_fields() { add_settings_section( $this->artesans_plugin_ref.'_set_section', // ID used to identify this section and with which to register options $this->artesans_plugin_name, // Title to be displayed on the administration page false, // Callback used to render the description of the section $this->artesans_plugin_ref // Page on which to add this section of options ); add_settings_field( $this->artesans_plugin_ref.'_field1', // ID used to identify the field 'Setting Name', // The label to the left of the option interface element array($this,'show_settings_field'), // The name of the function responsible for rendering the option interface $this->artesans_plugin_ref, // The page on which this option will be displayed $this->artesans_plugin_ref.'_set_section',// The name of the section to which this field belongs array( // args to pass to the callback function rendering the option interface 'field_name' => $this->artesans_plugin_ref.'_field1' ) ); register_setting( $this->artesans_plugin_ref, $this->artesans_plugin_ref.'_field1'); } function show_settings_field($args){ $saved_value = get_option( $args['field_name'] ); // initialize in case there are no existing options if ( empty($saved_value) ) { echo '
'; } else { echo '
'; } } function register_settings_page(){ add_submenu_page( 'options-general.php', // Parent menu item slug __($this->artesans_plugin_name, $this->artesans_plugin_name), // Page Title __($this->artesans_plugin_menu, $this->artesans_plugin_name), // Menu Title 'manage_options', // Capability $this->artesans_plugin_ref, // Menu Slug array( $this, 'show_settings_page' ) // Callback function ); } function show_settings_page() { ?>
artesans_plugin_ref); ?> artesans_plugin_ref); ?>

artesans_plugin_slug );?>

"; } } new Artesans_Search_Redirect(); function artesans_search_redirect_modify_search ( $query ) { if ( $query->is_main_query() && $query->is_search ) { $word = $_GET["s"]; $word = strtolower( $word ); $redirections = get_option( 'artesans_search_redirect_field1' ); $redirections = explode( '[REDIRECTION]', $redirections ); foreach ( $redirections as $redirection ) { if ( $redirection != "" ) { $redirection =explode('[URL]', $redirection ); $keywords = $redirection[0]; // 'jander,clander, petejander way' $keywords = esc_html($keywords); // escape html $keywords = remove_accents($keywords); // Àngels - Angels $keywords = strtolower($keywords); // Angels - angels $kw = explode(",",$keywords); // array('jander','clander',' petejander way') $url = $redirection[1]; // http... if ( in_array( $word, $kw ) ) { wp_redirect($url); exit(); } } } } return $query; } add_action( 'pre_get_posts', 'artesans_search_redirect_modify_search' );