hide widgets on certain pages in wordpress

There are many ways to hide widgets from certain page in wordpress. Here we will give code sample and information about wordpress plugin which will be helpful to wordpress users to manage widgets in smarter way.

hide widgets on certain pages

There is manual and old way of hiding widgets on certain pages which is adding code in theme. You can get your page name or page id and use following code.


add_filter( 'widget_display_callback', 'hide_widget_pages', 10, 3 );
function hide_widget_pages( $instance, $widget, $args ) {
if ( $widget->id_base == 'pages' ) { // change 'pages' to widget name
if ( !is_page( 'contact' ) ) {    // change page name
return false;
}
}
}

You need to copy and paste above code into functions.php file (themes folder).

Now we will tell you about automatic and smart way. There is very nice and good wordpress plugin which will give you more options in widget so you can control your widget more smartly.

Display Widgets

Change your sidebar content for different pages, categories, custom taxonomies, and WPML languages. Avoid creating multiple sidebars and duplicating widgets by adding check boxes to each widget in the admin (as long as it is written in the WordPress version 2.8 format) which will either show or hide the widgets on every site page. Great for avoiding extra coding and keeping your sidebars clean.

By default, ‘Hide on checked pages’ is selected with no boxes checked, so all current widgets will continue to display on all pages.

hide widgets on certain pages in wordpress
hide widgets on certain pages in wordpress

Above is screen shot of widget admin section. There are many options you can find for every widget. I found this plugin is very helpful to me.

There is option of hide widget for checked pages. You can check certain pages for hiding widgets. This is code free solution which i recommend for all wordpress users.

Create Widgets using Visual Editor via WYSIWYG Widgets plugin

Many times we want to create just simple text and image widget using the wysiwyg editor. using WYSIWYG Widgets create HTML and image or media in text widget.

Create Widgets using Visual Editor

WordPress widgets are very important in CMS. In every CMS like WP and Drupal we have sidebar widgets. In Drupal creating a sidebar is very easy. WordPress cannot give you default and easy create widget or sidebar functionality. Many times we want to create just simple text and image widget using the wysiwyg editor. There are many easy ways to create widget using text widget functionality. We can add the HTML and image or media in text widget.

With text widget we can add the HTML, CSS and images code in text widget and show in sidebar or footer section. But many times admin does not know about CSS and HTML but still admin wants to add the images and formated text in widgets. There is very nice wordpress plugin which will give you ability to add the widget with formated text and images.

WYSIWYG Widgets

Create Widgets using Visual Editor
Create Widgets using Visual Editor

WYSIWYG Widgets or rich text widgets

This plugin adds so called “Widget Blocks” to your website which you can easily display in your widget areas. Edit widget content using the default WordPress visual editor and media uploading functionality. Create widgets like you would create posts or pages.

You can create or edit the widget blocks just like you would edit any post or page, with all the default WordPress editing functions enabled. This way, you can use the visual editor that comes with WordPress to format your widgets. You can even use media uploading to insert images and so forth.

Create Widgets using Visual Editor config
Create Widgets using Visual Editor config

Features:

  • Create beautiful widgets without having to write HTML code
  • Easily insert media into your widget content
  • Add headings, lists, blockquotes and other HTML elements to your widgets using the WordPress visual editor
  • Use WP Links dialog to easily link to any of your pages or posts from a widget
  • Use shortcodes inside your widgets
  • Translation ready

First install the install and activate the WYSIWYG Widgets plugin. Then install and activate the WYSIWYG Widgets plugin. go to Widget Blocks » Add New to create a new widget block. Basically Widget Blocks are custom post types and every post will became new widget. It is very cool and easy to use. It is useful when you are giving the all the control to admin.

how to add google search to wordpress manually

wordpress has inbuilt search, still people want to add the advanced search in their site. we will show, how to add google search to wordpress manually.

how to add google search to wordpress manually

There are too many sites build on wordpress. wordpress gives inbuild search option in cms. But still people want to add the advanced search in there site. I suggest you can add the google search in wordpress site. Adding google search in wordpress site is really SEO friendly also. It is very easy to add the google search system in wordpress sites.

In some very simple steps you can add the google search in wordpress sites.

First login to google and go to following URL:
https://www.google.com/cse/

how to add google search to wordpress manually
how to add google search to wordpress manually

Just add the your site and and get your site custom google search for your site.

Before this you need to create the wordpress page for showing the search result page.

I created page called “search result”. For getting JS code you need give search result page path in google settings.

You will get options for selecting search result layout. Here you will get two javascripts code for google search form and search result.

After getting the two javscript code. You need to add the form code into wordpress widget.

On search result page just add the second code and save it. That sit. Your custom site google search is ready.

You can Customize Google Custom Search Engine Colors and Looks as your site color and theme.

wordpress gogogle search -CSE - Look and Feel
wordpress gogogle search -CSE – Look and Feel

Sample Form Code: (you can use following code in wordpress widget)

<form action="https://purabtech.in/search-result/" id="cse-search-box">
<div>
815y3hyfmru" />
<input type="hidden" name="cof" value="FORID:10" />
<input type="hidden" name="ie" value="ISO-8859-1" />
<input type="text" name="q" size="32" />
<input type="submit" name="sa" value="Search" />
</div>
</form>

Following code you can use in search result page

cse-search-results">

var googleSearchIframeName = "cse-search-results";
var googleSearchFormName = "cse-search-box";
var googleSearchFrameWidth = 550;
var googleSearchDomain = "www.google.com";
var googleSearchPath = "/cse";
// ]]></script>

Google Search Trick in wordpress. There is width issue in search result page. Google search result render with 768px width.
You need to just add following CSS code in your wordpress theme style.css file.

#cse-search-results iframe {width: 200px; }

custom google site search you can add in any wordpress site without any wordpress plugin.

How to create wordpress widget plugin

How to create wordpress widget plugin, In this article, given full code and with explanation.  For creating wordpress widget you just need use our code. In many wordpress themes we want to create the wordpress widget. For creating the wordpress widget you just need to put following code in functions.php file.

How to create wordpress widget plugin

This functions.php file you can find in wordpress theme folder.

class My_Widget extends WP_Widget {
function My_Widget() {
parent::WP_Widget(false, 'Our Test Widget');
}
function form($instance) {
// outputs the options form on admin
}
function update($new_instance, $old_instance) {
// processes widget options to be saved
return $new_instance;
}
function widget($args, $instance) {
// outputs the content of the widget
}
}
register_widget('My_Widget');

this code

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
class bm_widget_popularPosts extends WP_Widget {

function bm_widget_popularPosts() {
parent::WP_Widget(false, 'Popular Posts');
}

function widget($args, $instance) {
$args['title'] = $instance['title'];
bm_popularPosts($args);
}

function update($new_instance, $old_instance) {
return $new_instance;
}

function form($instance) {
$title = esc_attr($instance['title']);
?></pre>
php echo $this->get_field_id('title'); ?>"><!--?php _e('Title:'); ?--> <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
<pre>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
}
}
function bm_popularPosts($args = array(), $displayComments = TRUE, $interval = '') {

global $wpdb;

$postCount = 5;

$request = 'SELECT *
FROM ' . $wpdb->posts . '
WHERE ';

if ($interval != '') {
$request .= 'post_date>DATE_SUB(NOW(), ' . $interval . ') ';
}

$request .= 'post_status="publish"
AND comment_count > 0
ORDER BY comment_count DESC LIMIT 0, ' . $postCount;

$posts = $wpdb->get_results($request);

if (count($posts) >= 1) {

if (!isset($args['title']) {
$args['title'] = 'Popular Posts';
}

foreach ($posts as $post) {
wp_cache_add($post->ID, $post, 'posts');
$popularPosts[] = array(
'title' => stripslashes($post->post_title),
'url' => get_permalink($post->ID),
'comment_count' => $post->comment_count,
);
}

echo $args['before_widget'] . $args['before_title'] . $args['title'] . $args['after_title'];
?>

<ol>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
foreach ($popularPosts as $post) {
?>
<li>
php echo $post['url'];?>"><!--?php echo $post['title']; ?-->
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
if ($displayComments) {
?>
(<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php echo $post['comment_count'] . ' ' . __('comments', BM_THEMENAME); ?>)
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
}
?>
</li>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
}
?>
</ol>

<?php
echo $args['after_widget'];
}
}
?>

 

How to create wordpress widget plugin
How to create wordpress widget plugin