Auto Generated Content Using Search Page and Bing

Open theme search.php and add the following code before get_header(); line:

<?php
define('BING_API_KEY', '');
function pete_curl_get($url, $params){$post_params = array();
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
}
$post_string = implode('&', $post_params);
$fullurl = $url."?".$post_string;
$ch = curl_init();curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($ch, CURLOPT_URL, $fullurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040608'); //you can use another user agent, please see www.user-agents.org
$result = curl_exec($ch);curl_close($ch);
return $result;
}function perform_bing_web_search($termstring){$searchurl = 'http://api.bing.net/json.aspx?';
$searchurl .= 'AppId='.'E71EB199AA96DF4903388D858EDF6C2BC4609AE8'; //change the value with your Bing API
$searchurl .= '&Query='.urlencode($termstring);
$searchurl .= '&Sources=Web';
$searchurl .= '&Web.Count=5'; //search number
$searchurl .= '&Web.Offset=0';
$searchurl .= '&Web.Options=DisableHostCollapsing+DisableQueryAlterations';
$searchurl .= '&JsonType=raw';
$response = pete_curl_get($searchurl, array());
$responseobject = json_decode($response, true);if ($responseobject['SearchResponse']['Web']['Total']==0)return array();
$allresponseresults = $responseobject['SearchResponse']['Web']['Results'];
$result = array();
foreach ($allresponseresults as $responseresult){$result[] = array('url' => $responseresult['Url'],'title' => $responseresult['Title'],'abstract' => $responseresult['Description'],);
}return $result;
}if (isset($_REQUEST['s'])) {
$termstring = urldecode($_REQUEST['s']);
} else {
$termstring = '';}

?>

And add the following code before <?php endif; ?>

<?php function CleanFileNameBan($result){
$bannedkey = array("porn", "sex", "blablabla"); // insert your banned list keyword
$result = str_replace($bannedkey, '',$result);
$result = trim($result);
return $result;
}
?>

<?php $termstring = $s ?>

<?php if ($s!='') {
$bingresults = perform_bing_web_search($termstring);
//Kamu bisa ganti kode <h1> serta test yang ada sesuai dengan kode html dan text keinginan kamu begitu juga dengan yang lainnya
print '<h2>Hasil Pencarian '.$s.' di situs lainnya:</h2>';
foreach ($bingresults as $result) {
print '<div>';
print '<h2>'.htmlspecialchars(CleanFileNameBan($result['title'])).'</h2>';
print '<p>'.CleanFileNameBan(strip_tags($result['abstract'])).'</p>';
print '<p style="color:#777777">'.$result['url'].'</p>';
print '</div>';
}
}
?>

Open theme function.php and add the following code:

/** search permalinks **/
function cws_nice_search_redirect() {
if ( is_search() && strpos( $_SERVER['REQUEST_URI'], '/wp-admin/' ) === false && strpos( $_SERVER['REQUEST_URI'], '/search/' ) === false ) {
wp_redirect( home_url( '/archieve/' . str_replace( array( ' ', '%20' ),  array( '+', '+' ), get_query_var( 's' ) ) ) );
exit();
}
}

add_action( 'template_redirect', 'cws_nice_search_redirect' );


One comment on “Auto Generated Content Using Search Page and Bing

Create Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>