How To Completely Shut Down WordPress Search
Using the following snippet, you will be able to shut down WordPress search completely.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function fb_filter_query( $query, $error = true ) { if ( is_search() ) { $query->is_search = false; $query->query_vars[s] = false; $query->query[s] = false; // to error if ( $error == true ) $query->is_404 = true; } } add_action( 'parse_query', 'fb_filter_query' ); add_filter( 'get_search_form', create_function( '$a', "return null;" ) ); |
Snippet Source/Credit: WPEngineer
How To Enlight Searched Text In Search Results
Using the following snippet, you will be able to enlight searched text in search results.
1 |
echo $title; |
And now just before the line you modified, add this code of lines:
1 2 3 4 5 6 7 |
<?php $title = get_the_title(); $keys= explode(" ",$s); $title = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt">\0</strong>', $title); ?> |
Once done, now open style.css and append the following line to it:
1 |
strong.search-excerpt { background: yellow; } |
Snippet Source/Credit: Yoast
How To Automatically Add Facebook Open Graph To Your Posts
Adding the following snippet in your WordPress theme’s functions file, you will be automatically able to add Facebook open graph to your posts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function wptuts_opengraph_for_posts() { if ( is_singular() ) { global $post; setup_postdata( $post ); $output = '<meta property="og:type" content="article" />' . "\n"; $output .= '<meta property="og:title" content="' . esc_attr( get_the_title() ) . '" />' . "\n"; $output .= '<meta property="og:url" content="' . get_permalink() . '" />' . "\n"; $output .= '<meta property="og:description" content="' . esc_attr( get_the_excerpt() ) . '" />' . "\n"; if ( has_post_thumbnail() ) { $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' ); $output .= '<meta property="og:image" content="' . $imgsrc[0] . '" />' . "\n"; } echo $output; } } add_action( 'wp_head', 'wptuts_opengraph_for_posts' ); |
Once done, now open your header.php and replace the <html> tag with:
1 |
<html <?php language_attributes(); ?> prefix="og: http://ogp.me/ns#"> |
Snippet Source/Credit: WP Tutsplus
How To Redirect To First Post If Only One Post Is Found
Using the following snippet, you will be able to redirect to first post if only one post is found.
1 2 3 4 5 6 7 8 9 |
add_action('template_redirect', 'redirect_single_post'); function redirect_single_post() { if (is_search()) { global $wp_query; if ($wp_query->post_count == 1) { wp_redirect( get_permalink( $wp_query->posts['0']->ID ) ); } } } |
Snippet Source/Credit: Paulund.co.uk
How To Make Your WordPress Website SEO Friendly Without A Plugin
Using the following snippet, you will be able to make your WordPress website SEO friendly without a plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
function basic_wp_seo() { global $page, $paged, $post; $default_keywords = 'wordpress, plugins, themes, design, dev, development, security, htaccess, apache, php, sql, html, css, jquery, javascript, tutorials'; // customize $output = ''; // description $seo_desc = get_post_meta($post->ID, 'mm_seo_desc', true); $description = get_bloginfo('description', 'display'); $pagedata = get_post($post->ID); if (is_singular()) { if (!empty($seo_desc)) { $content = $seo_desc; } else if (!empty($pagedata)) { $content = apply_filters('the_excerpt_rss', $pagedata->post_content); $content = substr(trim(strip_tags($content)), 0, 155); $content = preg_replace('#\n#', ' ', $content); $content = preg_replace('#\s{2,}#', ' ', $content); $content = trim($content); } } else { $content = $description; } $output .= '<meta name="description" content="' . esc_attr($content) . '">' . "\n"; // keywords $keys = get_post_meta($post->ID, 'mm_seo_keywords', true); $cats = get_the_category(); $tags = get_the_tags(); if (empty($keys)) { if (!empty($cats)) foreach($cats as $cat) $keys .= $cat->name . ', '; if (!empty($tags)) foreach($tags as $tag) $keys .= $tag->name . ', '; $keys .= $default_keywords; } $output .= "\t\t" . '<meta name="keywords" content="' . esc_attr($keys) . '">' . "\n"; // robots if (is_category() || is_tag()) { $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; if ($paged > 1) { $output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n"; } else { $output .= "\t\t" . '<meta name="robots" content="index,follow">' . "\n"; } } else if (is_home() || is_singular()) { $output .= "\t\t" . '<meta name="robots" content="index,follow">' . "\n"; } else { $output .= "\t\t" . '<meta name="robots" content="noindex,follow">' . "\n"; } // title $title_custom = get_post_meta($post->ID, 'mm_seo_title', true); $url = ltrim(esc_url($_SERVER['REQUEST_URI']), '/'); $name = get_bloginfo('name', 'display'); $title = trim(wp_title('', false)); $cat = single_cat_title('', false); $tag = single_tag_title('', false); $search = get_search_query(); if (!empty($title_custom)) $title = $title_custom; if ($paged >= 2 || $page >= 2) $page_number = ' | ' . sprintf('Page %s', max($paged, $page)); else $page_number = ''; if (is_home() || is_front_page()) $seo_title = $name . ' | ' . $description; elseif (is_singular()) $seo_title = $title . ' | ' . $name; elseif (is_tag()) $seo_title = 'Tag Archive: ' . $tag . ' | ' . $name; elseif (is_category()) $seo_title = 'Category Archive: ' . $cat . ' | ' . $name; elseif (is_archive()) $seo_title = 'Archive: ' . $title . ' | ' . $name; elseif (is_search()) $seo_title = 'Search: ' . $search . ' | ' . $name; elseif (is_404()) $seo_title = '404 - Not Found: ' . $url . ' | ' . $name; else $seo_title = $name . ' | ' . $description; $output .= "\t\t" . '<title>' . esc_attr($seo_title . $page_number) . '</title>' . "\n"; return $output; } |
Once done, simply replace the $default_keywords in line 3 with your own keywords and add the following line of code into your theme’s header.php file.
1 |
<?php echo basic_wp_seo(); ?> |
Snippet Source/Credit: Jeff Starr