Blog Code Snippets PHP Wordpress

WordPress Plugin Error – Notice: Trying to get property of non-object in…

I’m getting this error in relation to the ‘Grid & List toggle for Woocommerce’ plugin, specifically this file:

/wp-content/plugins/grid-list-toggle-for-woocommerce/woocommerce_grid_list.php on line 115


Basically, the error was corrected by surrounding the code after global $post with the following statement:
if ( is_object( $post )) {


Click here for the corrected file.

Blog Code Snippets PHP Wordpress

Display Product Categories with Thumbnail Image

Use shortcode: [show-product-categories-with-image]

Add the following to your functions.php file.

function showProductCat() {

$cat_args = array(
‘orderby’ => ‘name’,
‘order’ => ‘asc’,
‘hide_empty’ => true,

$product_categories = get_terms( ‘product_cat’, $cat_args );

$result = “<div class=’product-categories-with-image’>”;

if( !empty($product_categories) ){

foreach ($product_categories as $key => $category) {

$thumb_id = get_woocommerce_term_meta( $category->term_id, ‘thumbnail_id’, true );
$term_img = wp_get_attachment_image_src( $thumb_id, ‘thumbnail’, true );

$cat_img = “”;

if ($term_img[3]) {
$cat_img = “<img src='”. $term_img[0] .”‘ width='” . $term_img[1] . “‘ height='” . $term_img[2] . “‘ />”;

$result = $result . ‘<a href=”‘.get_term_link($category).'” >’
. $cat_img
. $category->name
. ‘</a><br/>’;


$result = $result . “</div>”;

return $result;
add_shortcode(‘show-product-categories-with-image’, ‘showProductCat’);

Code Snippets PHP Wordpress

Redirect for a Specific Page on WordPress

Here’s a snippet of code to redirect users who land on a specific page of your wordpress site.

You will need the page id. Add it to your functions.php file.

The code below redirect users to my site. 🙂

function redirect_to_another_page() {
$page_id_to_redirect = 119; //enter page id here
$destination = ''; //enter destination url here
if ( is_page($page_id_to_redirect) ) {
wp_redirect( $destination, 301 );
add_action( 'template_redirect', 'redirect_to_another_page' );

Blog Code Snippets PHP Wordpress

Deactive a Sidebar or Widgetized Area

Here’s how to deactive a Sidebar or Widgetized Area in WordPress so it won’t show on the website and neither will it show on the Widgets section of the wordpress backend.

First, get the name of the sidebar, you may need to check the theme functions file to get exactly it is.

In this case, we will assume that the sidebar name is ‘sidebar-1’.

Add the code below to your functions file.

function remove_some_widgets(){

// Unregister sidebar
unregister_sidebar( 'sidebar-1' );
add_action( 'widgets_init', 'remove_some_widgets', 11 );

Blog Code Snippets PHP Wordpress

Embed a Visual Composer page with CSS Styles

Here’s how to embed a page created with Visual Composer onto another page with the page CSS styles included. You can add this code to either the header or footer file of your theme, or another theme file for that matter.

First step: note the id of the page to be embedded.

Use this snippet and put the id into the first line of code as indicated below.

$id = 811;
$p = get_page($id);
echo apply_filters(‘the_content’, $p->post_content);

$shortcodes_custom_css = get_post_meta( $id, ‘_wpb_shortcodes_custom_css’, true );
if ( ! empty( $shortcodes_custom_css ) ) {
echo ‘<style type=”text/css” data-type=”vc_shortcodes-custom-css-‘.$id.'”>’;
echo $shortcodes_custom_css;
echo ‘</style>’;


PHP Wordpress

Load / Enqueue the latest version of css and javascript files

Add the following code to your functions.php file to load / enqueue the most recent child theme style.css file or any css/javascript file.

$cache_buster = date("YmdHi");
function my_custom_files() {
wp_register_style( 'childstyle', get_stylesheet_directory_uri() . '/style.css', $cache_buster, 'all' );
wp_enqueue_script( 'my-scripts', get_stylesheet_directory_uri() . '/scripts.js', array(), $cache_buster, true );
wp_enqueue_style( 'childstyle' );
add_action( 'wp_enqueue_scripts', 'my_custom_files', 11);

Code Snippets PHP Wordpress

Get Contents from Visual Composer Page and Load into Another with CSS Styling

$id = 200; //page id

$shortcodes_custom_css = get_post_meta( $id, ‘_wpb_shortcodes_custom_css’, true );
if ( ! empty( $shortcodes_custom_css ) ) {
echo ‘<style type=”text/css” data-type=”vc_shortcodes-custom-css-‘.$id.'”>’;
echo $shortcodes_custom_css;
echo ‘</style>’;

$post = get_post($id);
echo apply_filters(‘the_content’, $post->post_content);

Blog Code Snippets PHP Wordpress

Add a new widget area to a WordPress theme

If you are familiar with WordPress themes then you know that a lot of themes have a widgetized sidebar. This means that you can add, remove, and re-order widgets on your WordPress website by using the “widget” section of your WordPress dashboard.

Having a widgetized sidebar is very useful, but you may want to widgetize other parts of your WordPress theme as well. This is very easy to do, and once your theme is modified it will be simple for you, or the WordPress administrator, to swap widgets in and out of different parts of the website.

Step 1: Add code to theme

The first step is to add the following line of code to the part of your theme that you want to widgetize. Be sure to change “Name of Widgetized Area” to a name that makes sense for you. You will need to do this with a code editor and then upload the file via a FTP client.

if ( is_active_sidebar( 'custom-header-widget' ) ) : ?>
    <div id="header-widget-area" class="chw-widget-area widget-area" role="complementary">
    <?php dynamic_sidebar( 'custom-header-widget' ); ?>
<?php endif; ?>

Step 2: Edit functions.php

In your WordPress theme folder, there should be a functions.php file. If there isn’t, just make a new file and name it “functions.php”.

In the functions.php file, add the following code:

function wpb_widgets_init() {
    register_sidebar( array(
        'name'          => 'Custom Header Widget Area',
        'id'            => 'custom-header-widget',
        'before_widget' => '<div class="chw-widget">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="chw-title">',
        'after_title'   => '</h2>',
    ) );
add_action( 'widgets_init', 'wpb_widgets_init' );


The code above should be wrapped in PHP open and close(<?php and ?>, respectively) tags. If you already have a functions.php file those tags will already be there. If you created one yourself you will have to add them.

Make sure to change the name of the function (in this case it is “Name of Widgetized Area”) so that it matches the name you gave it in step 1.

The ‘before_widget’ and ‘after_widget’ parameters allow you to specify what code you would like to put before and after each widget. In this case I put a div with an class for styling purposes.

The ‘before_title’ and ‘after_title’ parameters allow you to wrap the widget titles in code. In this case I wrapped the title in <h3> and </h3> tags respectively.

Step 3: Adding Widgets

Once you have successfully added the widgetized area, you can start adding widgets to your WordPress site. To do this, log into your WordPress dashboard, then click on Widgets in the Appearance dropdown on the left side.

You should now see the “Name of Widgetized Area” section on the right side of your screen.

Now just click and drag widgets into the box just like your sidebar!

Code Snippets Wordpress

Customize any Form on WordPress using CSS

Have you installed a contact form plugin for WordPress and found the default layout is entirely boring?

I’ve been there! The default layout comes with a very little or absolutely no css formatting.

Below is the default layout for Ninja Forms.

Contact Form - Before Applying CSS

Luckily, I was able to write a snippet of code which will resolve that.

The Solution to Customizing any Form on WordPress using CSS

This snippet can work on any contact form for WordPress; including of course Contact Form 7 and Ninja Forms. Just copy and paste into your custom css editor. Some themes will come up with this editor which you can find in the WordPress Admin section under ‘Appearance’. If your theme doesn’t come with it, download and install Simple CSS and JS which will work perfectly well.

input, select, textarea {
border-radius: 3px;
font-size: 17px;
padding: 8px;
color: #666;

/* Reduce the textarea size to 80 pixels */
textarea {
height: 80px;

Contact Form - After Applying CSS

There you have it! A nice sleek modern looking form created by adding just a few lines of CSS.

Blog Wordpress

Two Simple Tips to Securing Your WordPress Site

1. Never use the default ‘admin’ as your administrator login username. As it is the default, unauthorized parties trying to wrongfully access your site will attempt using ‘admin’ to log in. Changing the username to something, anything other than ‘admin’ will prevent malicious attacks.

2. Add a Captcha Code. A Captcha (a backronym for “Completely Automated Public Turing test to tell Computers and Humans Apart”) is a type of challenge-response test used in computing to determine whether or not the user is human.

Usually, the Captcha test will take the form of a random set of characters shown on an image which the user has to type in an accompanying input box. Other cool captcha tests will ask the user to sum two numbers or click a checkbox proving that they’re human. There are several types of captcha tests available to check for bots which may attempt to hack your site.

Recently, I’ve had the pleasure of using the Captcha Bank plugin for WordPress – its pretty impressive. It adapted seemlessly to a specially customized login page when other Captcha plugin wouldn’t. There are lots of various options for customizing the Captcha code.