Changing WordPress search URL - php

this is the code which I'm trying
function search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
global $opt; if($opt['rewrite_slug_search']){ $search_url = $opt['rewrite_slug_search'];
} else { $search_url = 'search'; }
wp_redirect( home_url( $search_url ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'search_url_rewrite' );
the $search_url is option from my theme settings panel but there is a problem with the slash when I type test in search box I get searchtest instead of search/test do you have any idea how to get a slash between?

Related

WooCommerce Session Variables Initialization

I'm trying to create a session variable in shop page and then I want to show this variable values in checkout page.
I wrote this code in woocommerce function page:
// Creating session variables
add_action( 'template_redirect', 'oturum_degiskeni_olustur' );
function oturum_degiskeni_olustur () {
if ( isset( $_GET['konumu'] ) || isset( $_GET['masa_no'] ) ) {
$konum = isset( $_GET['konumu'] ) ? esc_attr( $_GET['konumu'] ) : '';
$masa = isset( $_GET['masa_no'] ) ? esc_attr( $_GET['masa_no'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'konum' => $konum, 'masa' => $masa ) );
}
}
// Show session variables in checkout page
add_action('woocommerce_before_checkout_form', 'yeni_alanlar');
function yeni_alanlar($checkout){
$data = WC()->session->get('custom_data');
echo '<div id="custom_checkout_field">';
if( isset($data['konum']) && ! empty($data['konum']) && isset($data['masa']) && ! empty($data['masa']) ){
echo '<p><strong>Konumunuz : </strong>' . $data['konum'] .'<br/><strong>Masanız : </strong>' . $data['masa'] .'</p>';
}
echo '</div>';
}
URL for session variables: www.domain.com/shop/?konumu=newyork&masa_no=12
But this code working when user's second visit.
For example; user use this custom URL and visit the shop page and continue to cart and checkout page. But, my code is not working. Then, user going to this custom URL again and continue to cart & checkout page and code is working this time.
It is not working when first visit.
Why is that? How can I solve this issue?
You need to early init customer session, so we will change template_redirect to init hook using the WC_Session_Handler method set_customer_session_cookie() like:
// Creating session variables
add_action( 'init', 'oturum_degiskeni_olustur' );
function oturum_degiskeni_olustur () {
// Early initialize customer session
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
if ( isset( $_GET['konumu'] ) && isset( $_GET['masa_no'] ) ) {
$konum = isset( $_GET['konumu'] ) ? esc_attr( $_GET['konumu'] ) : '';
$masa = isset( $_GET['masa_no'] ) ? esc_attr( $_GET['masa_no'] ) : '';
// Set the session data
WC()->session->set( 'custom_data', array( 'konum' => $konum, 'masa' => $masa ) );
}
}
// Show session variables in checkout page
add_action('woocommerce_before_checkout_form', 'yeni_alanlar');
function yeni_alanlar(){
if( ( $data = WC()->session->get('custom_data') )
&& isset($data['konum']) && isset($data['masa']) ) {
echo '<div id="custom_checkout_field">
<p><strong>Konumunuz : </strong>' . $data['konum'] .'<br/>
<strong>Masanız : </strong>' . $data['masa'] .'</p>
</div>';
}
}
Now it should better work.

Redirect custom page to a product tag

Is there a way to redirect a page to a product tag? Before this, I did it with a product category and it worked. So I changed the code to redirect it with a product tag but it didn't work
function so_38101217_template_redirect(){
if( function_exists( 'is_product_tag' ) && is_product_tag( 'raya-2020' ) ){
$redirect_page_id = 9905; // adjust to ID of page you are redirecting to
wp_redirect( get_permalink( $redirect_page_id ) );
exit();
}
}
Thanks in advance
function so_38101217_template_redirect() {
if ( function_exists( 'is_product_tag' ) && is_product_tag( 'raya-2020' ) ) {
$redirect_page_id = 9905; // adjust to ID of page you are redirecting to
wp_redirect( get_permalink( $redirect_page_id ) );
exit();
}
}
add_action( 'template_redirect', 'so_38101217_template_redirect' );
Try this code - add this line in your functions.php in the active theme

How can I redirect this to a new tab?

I'm working on redirecting posts to url's and this currently does the job nicely but I cannot work out how to get it to open the url in a new tab.
defined( 'ABSPATH' ) or exit;
function bhrdr2p_redirect_post_to_url() {
if( !is_singular() ) return;
global $post;
$redirect = esc_url( get_post_meta( $post->ID, 'redirect', true ) );
if( $redirect ) {
wp_redirect( $redirect, 301 );
exit;
}
}
add_action( 'template_redirect', 'bhrdr2p_redirect_post_to_url' );
//redirect any drafted posts
add_action('template_redirect', 'bhrdr2p_rtrash_redirect');
function bhrdr2p_rtrash_redirect(){
if ( !current_user_can( 'edit_pages' ) ) {
if (is_404()){
global $wp_query, $wpdb;
$page_id = $wpdb->get_var( $wp_query->request );
$post_status = get_post_status( $page_id );
$redirect = esc_url( get_post_meta( $page_id, 'redirect', true ) );
if($post_status == 'draft' && $redirect){
wp_redirect( $redirect , 301);
die();
}
}
}
}
Since PHP is server-side, you can't.
Both the header() function and Wordpress' wp_redirect() do not offer such functionality.
You could try using JavaScript, it might work:
Change the Wordpress redirect:
wp_redirect( $redirect , 301);
To a small JavaScript script.
echo "<script> window.open(".$redirect.", '_blank') </script>";
you must use the javascript here to redirect in new tab
if($post_status == 'draft' && $redirect){
<script>
window.open( '<?php echo $redirect; ?>', '_blank' );
</script>
die();
}

Fixing/hardening add_query_arg() usage in WP function

According to this recent post by the WordPress team, we need to check on our theme files to see if it's using the add_query_arg() and remove_query_arg() functions. Having done a full search of our current theme, I can see a reference to add_query_arg() in this snippet in our pagination.php file:
Here is PHP Code as follows :
if ( !function_exists( 'get_multipage_link' ) ) :
function get_multipage_link( $page = 1 ) {
global $post, $wp_rewrite;
if ( 1 == $page ) {
$url = get_permalink();
} else {
if ( '' == get_option('permalink_structure') || in_array( $post->post_status, array( 'draft', 'pending') ) )
add_query_arg( 'page', $page, get_permalink() );
elseif ( 'page' == get_option( 'show_on_front' ) && get_option('page_on_front') == $post->ID )
$url = trailingslashit( get_permalink() ) . user_trailingslashit( $wp_rewrite->pagination_base . "/$page", 'single_paged' );
else
$url = trailingslashit( get_permalink() ) . user_trailingslashit( $page, 'single_paged' );
}
return $url;
}
endif;
Unfortunately, I'm a bit out of my depth here. Is that reference OK as it is referencing get_permalink() and not a URL? That's way I don't need to use the esc_url() that the blog posting recommends?

Wordpress rewrite with php

Is possible change this url
www.12345.com/?s=&cp_state=city
to this?
www.12345.com/city
Already try with htaccess but in wordpress it doesen't work
Hi have this code. Is possible change it to have a url like this www.12345.com/city?
add_action('template_redirect', 'search_url_rewrite_rule');
function search_url_rewrite_rule() {
global $wp_rewrite;
if ( is_search() && isset( $_GET['s'] ) ) {
$s = str_replace( array( ' ', '%20' ), '+', get_query_var( 's' ) );
wp_redirect( home_url( $wp_rewrite->search_base . '/' . remove_accents ( $s ) ) );
exit();
}
}
add_action('init','change_search_permalinks');
function change_search_permalinks( ) {
global $wp_rewrite;
$wp_rewrite->search_base = 'search';
}
Thank you in advance
In php level it is not good idea
You can use url shortlink in admin area of wordpress.

Categories