Ganti Kata "search" di URL Wordpress, spesial terima kasih kepada abang jevuska, double click untuk seleksi seluruhnya, paste ke function template sobat (function.php). Ini skrip work, sudah saya tes praktek ilmu om edi setiawan pada postingan membuat web semi agece
// change global search rules search page, paged and feed
add_action( 'init', 'jv_change_search_base_wp' );
function jv_change_search_base_wp()
{
global $wp_rewrite;
$search_base = 'topik'; // search_base
if ( $wp_rewrite->search_base != $search_base )
{
$wp_rewrite->search_base = $search_base;
$wp_rewrite->search_structure = $wp_rewrite->search_base . '/%search%';
}
return $wp_rewrite;
}
// adding a new rule for empty search query
add_filter( 'search_rewrite_rules', 'jv_additional_search_rewrite_rules', 10, 1 );
function jv_additional_search_rewrite_rules( $rules )
{
global $wp_rewrite;
$new_rules = array(
user_trailingslashit( $wp_rewrite->search_base ) . '?$' => 'index.php?s=' . $wp_rewrite->preg_index(1),
);
$rules = $rules + $new_rules;
return $rules;
}
// flush_rules() if our rules are not yet included
add_action( 'wp_loaded', 'jv_flush_rules' );
function jv_flush_rules()
{
global $wp_rewrite;
$rules = get_option( 'rewrite_rules' );
$search_base = 'topik'; // search_base
if ( ! isset( $rules[ $search_base . '/(.+)/?$'] ) )
$wp_rewrite->flush_rules();
}
// redirect ?s= to search base
add_action( 'template_redirect', 'jv_template_redirect' );
function jv_template_redirect()
{
if ( is_search() && isset ( $_GET['s'] ) )
{
global $wp_rewrite;
$s = sanitize_text_field( $_REQUEST['s'] ); // or get_query_var( 's' )
$location = '/';
$location .= trailingslashit( $wp_rewrite->search_base );
$location .= ( ! empty ( $s ) ) ? sanitize_title_with_dashes( $s ) . '.html' : ''; // sanitize search query with dashes
$location = home_url( $location );
wp_safe_redirect( $location, 301 );
exit;
}
}
add_action( 'parse_request', 'jv_parse_request' );
function jv_parse_request( $query )
{
if ( ! is_admin() && isset( $query->query_vars['s'] ) )
{
$s = jv_remove_char( $query->query_vars['s'] );
$query->set_query_var( 's', $s );
}
}
function jv_remove_char( $q = null )
{
$q = sanitize_title_with_dashes( urldecode( $q ), '', 'save' );
$q = wp_strip_all_tags( $q );
$q = preg_replace( '/&#?[a-z0-9]+;/i','', $q );
$q = preg_replace( '/[^%A-Za-z0-9 _-]/', ' ', $q );
$q = preg_replace( '/&.+?;/', '', $q );
$q = preg_replace( '/_+/', ' ', $q );
$q = preg_replace( '/s+/', ' ', $q );
$q = preg_replace( '|-+|', ' ', $q );
$q = urlencode( trim( $q ) );
return $q;
}
4 comments
aa
terima kasih bnyak gan script nya, sangat bermanfaat, semoga ilmu agan terus bertambah :)
waduh... setiap huruf "s" dihapus oleh printahnya..... mau buat keyword sayang jadi ayang...
buanng aja bagian ini pak nyantel
$q = preg_replace( '/s+/', ' ', $q );
atau jadikan komentar saja
// $q = preg_replace( '/s+/', ' ', $q );
Ayo sob di komen, kalau ada salah atau saran, kritik boleh coret coret dibawah, gratis kok!