Ask your WordPress questions! Pay money and get answers fast! (more info)

google cse Search WordPress

Hello
I want my site will search through google cse by search title on my site After a few seconds a window will open with search results.
Example:
search title on my site: Cars
Search results on google cse: Cars

That's how it should be: http://mynewstoday.info/cse.htm

Thank you

Answers (1)

2013-04-04

Dbranes answers:

Hi, these plugins might be of interest to you:

[[LINK href="http://wordpress.org/extend/plugins/google-cse/screenshots/"]]http://wordpress.org/extend/plugins/google-cse/screenshots/[[/LINK]]

[[LINK href="http://wordpress.org/extend/plugins/google-search-cse/screenshots/"]]http://wordpress.org/extend/plugins/google-search-cse/screenshots/[[/LINK]]

[[LINK href="http://wordpress.org/extend/plugins/google-custom-search/screenshots/"]]http://wordpress.org/extend/plugins/google-custom-search/screenshots/[[/LINK]]


vadik comments:

I'm not looking for plugin
I'm looking for someone to write code for me to make me what I asked for


Dbranes comments:

If you want to add this search to a special page or post, you can add this to your <em>functions.php</em> file in the current theme directory

function my_cse_func( $atts ) {
extract( shortcode_atts( array(
'cx' => '',
), $atts ) );


$out="
<script>
(function() {
var cx = '.$cx.';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:search></gcse:search>
";
return $out;
}
add_shortcode( 'mycse', 'my_cse_func' );


and then open the page/post and add

[mycse cx="1234"]

where you replace 1234 with your own cse id.


Dbranes comments:

So your shortcode would look something like:

[mycse cx="095382442174838362754:hisuukncdfg"]


If you want to use this shortcode also in a text-widget, you just have to add this to your <em>functions.php</em> file:

add_filter('widget_text', 'do_shortcode');


vadik comments:

I did not understand how it works your code

Look here: http://mynewstoday.info/cse.htm
And after a few seconds pop up Window Of google cse Search results.
I want it to look like this, You can customize the code.

Thank you


vadik comments:

<div id="myDiv">
<strong><span style="font-size: 16px; color: #ff0000;">Search, Please wait...</span></strong> <br />
<img src="http://s24.postimg.org/qy374im2t/Loading.gif">
</div>
<script type="text/javascript">
window.onload = function () {
setTimeout(function () {
var div = document.getElementById('myDiv');
div.innerHTML = 'now open pop-up google cse Cars results';
}, 6000);
}
</script>
<br>
<br>
<br>
my google cse Search:
<script>
(function() {
var cx = '000054068890153373265:gzhnfzwbrao';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:search></gcse:search>


vadik comments:

Search results by title not Searchbox.


Dbranes comments:

<blockquote>Search results by title not Searchbox.</blockquote>

can maybe explain this a little bit more, like how is the search process for the user.

When this text: "now open pop-up google cse Cars results" shows up in 6 seconds, you want the Google CSE to search for "cars" automatically?


vadik comments:

I will explain
Say a user searched on my site for in wordpress searchbox the word "car" that will direct them to Results by Google CSE search after 6 seconds.

ok?


Dbranes comments:

One approach would be to edit <em>search.php</em> with the code you provided:

<?php get_header();?>
... your html code ...
<?php get_footer();>


then you will have to edit the wordpress search box to add the <strong>q</strong> GET parameter.

Or add another search page and use a custom search form that points to that page.

You can for example test the Google CSE with a custom search string via the q paramter:

[[LINK href="http://mynewstoday.info/cse.htm?q=car"]]http://mynewstoday.info/cse.htm?q=car[[/LINK]]


vadik comments:

I do not get along
Write me your Skype here


vadik comments:

How do I make search results show after 6 seconds?


Dbranes comments:

Do make the search results show after 6 seconds you can changeyour html code to this:

<title>search title: Cars</title>
<center>
<div id="myDiv">
<strong><span style="font-size: 16px; color: #ff0000;">Search, Please wait...</span></strong> <br />
<img src="http://s24.postimg.org/qy374im2t/Loading.gif">
</div>
<script type="text/javascript">
window.onload = function () {
setTimeout(function () {
var div = document.getElementById('myDiv');
div.innerHTML = 'now open pop-up google cse Cars results';
var cx = '000054068890153373265:gzhnfzwbrao';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);

}, 6000);
}
</script>
<br>
<br>
<br>
my google cse Search:
</center>
<gcse:search></gcse:search>



vadik comments:

Write here your Skype please


vadik comments:

I repeated my mind
How do I open a iframe after 6 seconds?

<div id="myDiv">
<strong><span style="font-size: 16px; color: #ff0000;">Search, Please wait...</span></strong> <br />
<img src="http://s24.postimg.org/qy374im2t/Loading.gif">
</div>
<script type="text/javascript">
window.onload = function () {
setTimeout(function () {
var div = document.getElementById('myDiv');
div.innerHTML = 'now open iframe ';
}, 6000);
}
</script>


Dbranes comments:

Sorry I have to go now, so here is a wrap up for the original question.

You can just copy/paste the code below into your <em>functions.php</em> file in the current theme directory
(so you don't have to change or make templates).

This works on my Wordpress install, and you can just use the default Wordpress searchboxes.



// let's hijack the search template with your custom Html code
//
add_action('template_redirect', 'my_search_template_redirect', 1);

function my_search_template_redirect() {
// not a search page; don't do anything and return
if ((stripos($_SERVER['REQUEST_URI'], '?s=') === FALSE) && (stripos($_SERVER['REQUEST_URI'], '/search/') === FALSE) && (!is_search())) {
return;
}

get_header();

// your html template:

echo "
<center>
<div id='myDiv'>
<strong><span style='font-size: 16px; color: #ff0000;'>Search, Please wait...</span></strong> <br />
<img src='http://s24.postimg.org/qy374im2t/Loading.gif'>
</div>
<script type='text/javascript'>
window.onload = function () {
setTimeout(function () {
var div = document.getElementById('myDiv');
div.innerHTML = 'now open pop-up google cse Cars results';
var cx = '000054068890153373265:gzhnfzwbrao';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);

}, 6000);
}
</script>
<br>
<br>
<br>
my google cse Search:
</center>
<gcse:search></gcse:search>
";

get_footer();
exit();
}


// Let's enqueue jQuery just in case it's missing.
//
function my_scripts() {
wp_enqueue_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts', 'my_scripts' );


// Some jQuery stuff to add the q parameter with
// the same value as the s search parameter
//
// The search url will become: example.com/?s=cars&q=cars
// for the cars search input
//
add_filter("wp_footer","my_searchbox_edit");
function my_searchbox_edit(){
echo '
<script>
jQuery(document).ready(function($){
$("#searchform").submit(function(event){
var item = document.createElement("input");
item.name = "q";
item.type = "hidden";
item.value = $("#s").val();
$("#searchform").append(item);
});
});
</script>';
}


cheers