Skip to content

Instantly share code, notes, and snippets.

@madnil
Last active December 18, 2015 14:29
Show Gist options
  • Save madnil/5797878 to your computer and use it in GitHub Desktop.
Save madnil/5797878 to your computer and use it in GitHub Desktop.
This code snippet creates the shortcode "sel" under WordPress to automatically search for the surrounded words. The search engine can be defined by an argument. With no argument the search own your own WP site is used. The snippet needs to be included in the functions.php of the WordPress theme.
<?php
/*
Usage: [sel engine=ddg|bing|google]word1 word2 ...[/sel]
"ddg": DuckDuckGo
"bing": Bing
"google": Google
"": Site Search (Default)
*/
function search_engine_links ( $atts, $content = null ) {
extract(shortcode_atts(array(
"engine" => ''
), $atts));
switch ($engine) {
case "ddg": // DuckDuckGo
$search_str = "https://duckduckgo.com/?q=";
break;
case "bing": // Bing
$search_str = "http://www.bing.com/search?q=";
break;
case "google": // Google
$search_str = "http://www.google.de/search?q=";
break;
default: // your own WP site, replace with your own domain
$search_str = "http://www.acky.de/?s=";
break;
}
return '<a href="' . $search_str . do_shortcode(urlencode($content)) . '">' . do_shortcode($content) . '</a>';
}
if ( function_exists('add_shortcode') ) {
add_shortcode('sel', 'search_engine_links');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment