Common code snippets

On this page:

Introduction Add the package ID to the body attribute Adding CPT-specific content on top of the Add Listing page Making the listing description optional Sorting on Listings pages: Featured first, then alphabetic Detail Page Hide ‘No Reviews’Remove GD ScriptsSupreme Directory

Introduction

We recommend using the Code Snippets plugin to add custom PHP code.
CSS can be added to GD > Design > Scripts

Add the package ID to the body attribute

  1. If you want to change the looks of the detail page depending on the price package, you can do that by adding the ID to the code of the detail page:
  2. To do this, add this code to your child theme’s functions.php file:
    // add the package id to the body
    add_filter( 'body_class', 'my_add_package_class',10,1 );
    function my_add_package_class($class){
        global $post;
        if (geodir_is_page('detail') && isset($post->package_id)) {
            $class[] = "gd-package-id-".$post->package_id;
        }
        return $class;
    }
    
  3. You can now target elements of the detail page using the package ID class, for example
    .gd-package-id-1 .geodir-tags {display: none}
    

    would hide tags for price packages with ID 1.

Adding CPT-specific content on top of the Add Listing page

  1. If you want to add any special content to your Add Listing page, you can just add that in the content section using the normal way of editing your WP page “Add Listing”.
  2. If you want to add content specific to a CPT you can use this code, adjust as required:
    function geodir_add_place_before_main_content_custom() {
    	if (isset($_GET['listing_type']) && $_GET['listing_type'] == 'gd_place') {  
    		?>
    
    <div>bla bla</div>
    
    	
    		<?php
    	}
    }
    add_action('geodir_add_listing_before_main_content', 'geodir_add_place_before_main_content_custom');
    

Making the listing description optional

  1. The listing description field on the Add Listing page is a required field by default.
  2. You can make the description field optional by adding some JS and CSS code.
  3. Add the following code to GD > Design > Scripts tab > Footer script code:
    <script type="text/javascript">
    jQuery( document ).ready(function() {
        jQuery("#geodir_post_desc_row").removeClass("required_field");
    });
    </script>
    
  4. Also add the following CSS code:
    #geodir_post_desc_row label span, 
    #geodir_post_desc_row .geodir_message_error {
       display:none !important;
    }
    

Sorting on Listings pages: Featured first, then alphabetic

  1. The sorting options for listings can only set one default sorting option.
  2. The following code will order alphabetically even if the default order is set to show featured listings first:
    add_filter('geodir_posts_order_by_sort','_my_custom_order_by',10,3);
    function _my_custom_order_by($orderby, $sort_by, $table){
    	global $wpdb;
    
    	if($sort_by=='is_featured_asc'){
    		$orderby .= " $wpdb->posts.post_title ASC, ";
    	}
    	return $orderby;
    }
    

Detail page – hide ‘No Reviews’ text if there are no reviews

  1. Maybe you have a new site without a lot of reviews.
  2. The following code will hide the ‘no Reviews’ text
    function _gd_custom_hide_no_review_text( $class ) {
        global $post;
        if ( empty( $post->rating_count ) ) {
            $class .= 'gd-custom-no-review';
        }
        return $class;
    }
    add_filter( 'geodir_post_view_article_extra_class', '_gd_custom_hide_no_review_text', 10, 1 );
    

    And this short bit of CSS
    .gd-custom-no-review .geodir-entry-meta a.geodir-pcomments {
    display: none !important;
    }

Snippet to Remove GeoDirectory Scripts

Some site owner may want to optimize their site by loading GD scripts only on GD pages. The following applies only to GD core. Each Addon has additional scripts.
In GD core most of the scripts and styles are loaded via geodir_templates_scripts() and geodir_templates_styles() functions. You can use the following to skip loading of GD Core scripts.

remove_action( 'wp_enqueue_scripts', 'geodir_templates_scripts' );
remove_action( 'wp_enqueue_scripts', 'geodir_templates_styles', 8 );

geodir_is_geodir_page() is used to identify the GD page

Here’s a Snippet that will execute only on GD Pages.
*Caution* – Snippet below is only capable of detecting GD Pages, and will not detect other WP pages where you might be using a shortcode or widget. Use with Caution!

add_action( 'wp', 'wpdocs_dequeue_script', 100 );
function wpdocs_dequeue_script() {
    if (!geodir_is_geodir_page()){
        remove_action('wp_enqueue_scripts', 'geodir_templates_scripts');
        remove_action('wp_enqueue_scripts', 'geodir_templates_styles', 8);  
    }
}

Common Code Snippets for Supreme Directory

Remove Popular Categories

Popular Categories is part of the Template and here are two different ways to remove it.
Method 1 – Add the following CSS.

.featured-area .geodir-category-list-in {display:none;}

Method 2 – Open /wp-content/themes/supreme-directory/inc/geodirectory-compatibility.php
Look for the following line to comment it out or delete it.

echo do_shortcode('[gd_popular_post_category category_limit=5]');

Change Footer Text

function dt_remove_default_footer() {
    remove_action( 'dt_footer_copyright', 'dt_footer_copyright_default', 10 );
    add_action( 'dt_footer_copyright', 'custom_dt_footer_copyright_text', 10 );
}
add_action('init', 'dt_remove_default_footer');

function custom_dt_footer_copyright_text(){
    echo 'My copyright text';
}