WordPress rewrite search query string

I recently needed to remove the querystring “?s=mysearch” and found it a little hard as no htaccess rewrite was working. I added the following function in my functions.php and worked perfectly.

function rewrite_search_url() {
	if ( is_search() && ! empty( $_GET['s'] ) ) {
		wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
		exit();
	}	
}
add_action( 'template_redirect', 'rewrite_search_url' );
Advertisements

WordPress Create Menu Locations and Assigning Menus Programmatically

Either in your setup file or in your general functions.php, update and include the following code:

// This theme uses wp_nav_menu() in one location.
register_nav_menu( 'primary', __( 'Primary Menu', 'theme-slug' ) );
//get 'your_custom_menu' id to assign it to the primary menu location created
$menu_header = get_term_by('name', 'your_custom_menu', 'nav_menu');
$menu_header_id = $menu_header->term_id;
//if menu not found, create a new one
if($menu_header_id == 0) {
     $menu_header_id = wp_create_nav_menu('your_custom_menu');
}
//Get all locations (including the one we just created above)
$locations = get_theme_mod('nav_menu_locations');

//set the menu to the new location and save into database
$locations['primary'] = $menu_header_id;
set_theme_mod( 'nav_menu_locations', $locations );

Create Shortcode in Plugin

1. Under folder plugins create a new folder with your plugin name, example “my-sample-plugin”

2. Create a php file in the new folder with the same name “my-sample-plugin.php”

3. Setup your class in the newly created file:
class wp_my_sample_plugin {
public function __construct() {
}
}

$wpSample = new wp_my_sample_plugin ();

 

4. At the end of your constructor add the code for the shortcode:

 

add_shortcode( 'mysample', array( $this, 'my_sample_shortcode_fn' ) );

//first parameter represents the shortcode name you want to assign

//second parameter represents the function name to call

 

5. Right after the constructor, create the function name “my_sample_shortcode_fn”

 

public function my_sample_shortcode_fn($atts) {
$msg = 'Welcome to my sample plugin shortcode';
if(isset($atts['name']))
{
$msg .= ','.$atts['name'];
}
return $msg;
}

//The above function will display a welcome message whenever the shortcode is called.

//$atts represents any attributes passed along in the shortcode

//To check if an attribute is set, all we need to do is call $attr[‘name_here’]

//Attributes help making a plugin customizable through shortcodes

 

6. And finally, in your html all you need to do is add the following shortcode where ever you would like to display the content:

echo do_shortcode('[mysample name="Rochelle"]');