{"id":22882,"date":"2015-10-22T00:44:46","date_gmt":"2015-10-22T00:44:46","guid":{"rendered":"https:\/\/wpgeodirectory.com\/docs\/?p=22882"},"modified":"2018-01-11T15:09:29","modified_gmt":"2018-01-11T15:09:29","slug":"common-code-snippets","status":"publish","type":"post","link":"https:\/\/wpgeodirectory.com\/docs\/common-code-snippets\/","title":{"rendered":"Common code snippets"},"content":{"rendered":"<h4>On this page:<\/h4>\n<p><a class=\"cnt1\" href=\"#intro\">Introduction<\/a> <a class=\"cnt1\" href=\"#package\">Add the package ID\u00a0to the body attribute<\/a> <a class=\"cnt1\" href=\"#add-listing\">Adding CPT-specific content on top of the Add Listing page<\/a> <a class=\"cnt1\" href=\"#desc\">Making the listing description optional<\/a> <a class=\"cnt1\" href=\"#sort\">Sorting on Listings pages: Featured first, then alphabetic<\/a> <a class=\"cnt1\" href=\"#hide-no-reviews\">Detail Page Hide &#8216;No Reviews&#8217;<\/a> <a class=\"cnt1\" href=\"#remove-gd-scripts\">Remove GD Scripts<\/a> <a class=\"cnt1\" href=\"#supereme-directory\">Supreme Directory<\/a> <a class=\"cnt1\" href=\"#icons\">Showing icons instead of category links in list and grid view<\/a><\/p>\n<h3><a name=\"intro\"><\/a>Introduction<\/h3>\n<p>We recommend using the <a href=\"https:\/\/wpgeodirectory.com\/docs\/useful-plugins\/#snippets\">Code Snippets plugin<\/a> to add custom PHP code.<br \/>\nCSS can be added to GD &gt; Design &gt; Scripts<\/p>\n<h3><a name=\"package\"><\/a>Add the package ID\u00a0to the body attribute<\/h3>\n<ol>\n<li>If you want to change the looks of the detail page depending on the price package, you\u00a0can do that by adding the ID to the code\u00a0of the detail page:<br \/>\n<img loading=\"lazy\" class=\"greenborder\" src=\"https:\/\/wpgeodirectory.com\/docs\/wp-content\/uploads\/sites\/3\/2016\/10\/tips18.png\" alt=\"\" width=\"570\" height=\"43\" \/><\/li>\n<li>To do this, add this code to your child theme&#8217;s functions.php file:\n<p>[php]<br \/>\n\/\/ add the package id to the body<br \/>\nadd_filter( &#8216;body_class&#8217;, &#8216;my_add_package_class&#8217;,10,1 );<br \/>\nfunction my_add_package_class($class){<br \/>\n    global $post;<br \/>\n    if (geodir_is_page(&#8216;detail&#8217;) &amp;&amp; isset($post-&gt;package_id)) {<br \/>\n        $class[] = &quot;gd-package-id-&quot;.$post-&gt;package_id;<br \/>\n    }<br \/>\n    return $class;<br \/>\n}<br \/>\n[\/php]<\/p>\n<\/li>\n<li>You can now target elements of the detail page using the package ID class, for example\n<p>[css]<br \/>\n.gd-package-id-1 .geodir-tags {display: none}<br \/>\n[\/css]<\/p>\n<p>would hide tags for price packages with ID 1.<\/li>\n<\/ol>\n<h3><a name=\"add-listing\"><\/a>Adding CPT-specific content on top of the Add Listing page<\/h3>\n<ol>\n<li>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 &#8220;Add Listing&#8221;.<\/li>\n<li>If you want to add content specific to a CPT you can use this code, adjust as required:\n<p>[php]<br \/>\nfunction geodir_add_place_before_main_content_custom() {<br \/>\n\tif (isset($_GET[&#8216;listing_type&#8217;]) &amp;&amp; $_GET[&#8216;listing_type&#8217;] == &#8216;gd_place&#8217;) {<br \/>\n\t\t?&gt;<\/p>\n<p>&lt;div&gt;bla bla&lt;\/div&gt;<\/p>\n<p>\t\t&lt;?php<br \/>\n\t}<br \/>\n}<br \/>\nadd_action(&#8216;geodir_add_listing_before_main_content&#8217;, &#8216;geodir_add_place_before_main_content_custom&#8217;);<br \/>\n[\/php]<\/p>\n<\/li>\n<\/ol>\n<h3><a name=\"desc\"><\/a>Making the listing description optional<\/h3>\n<ol>\n<li>The listing description field on the <em><strong>Add Listing<\/strong><\/em> page is a required field by default.<\/li>\n<li>You can make the description field optional by adding some JS and CSS code.<\/li>\n<li>Add the following code to <em><strong>GD &gt; Design &gt; Scripts <\/strong><\/em>tab<em><strong> &gt; Footer script code<\/strong><\/em>:\n<p>[javascript]<br \/>\n&lt;img src=&quot;data:image\/gif;base64,R0lGODlhAQABAIAAAAAAAP\/\/\/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7&quot; data-wp-preserve=&quot;%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0AjQuery(%20document%20).ready(function()%20%7B%0A%20%20%20%20jQuery(%22%23geodir_post_desc_row%22).removeClass(%22required_field%22)%3B%0A%7D)%3B%0A%3C%2Fscript%3E&quot; data-mce-resize=&quot;false&quot; data-mce-placeholder=&quot;1&quot; class=&quot;mce-object&quot; width=&quot;20&quot; height=&quot;20&quot; alt=&quot;&amp;lt;script&amp;gt;&quot; title=&quot;&amp;lt;script&amp;gt;&quot; \/&gt;<br \/>\n[\/javascript]<\/p>\n<\/li>\n<li>Also add the following CSS code:\n<p>[css]<br \/>\n#geodir_post_desc_row label span,<br \/>\n#geodir_post_desc_row .geodir_message_error {<br \/>\n   display:none !important;<br \/>\n}<br \/>\n[\/css]<\/p>\n<\/li>\n<\/ol>\n<h3><a name=\"sort\"><\/a>Sorting on Listings pages: Featured first, then alphabetic<\/h3>\n<ol>\n<li>The <a href=\"https:\/\/wpgeodirectory.com\/docs\/core-place-settings\/#sorting\">sorting options for listings<\/a> can only set one default sorting option.<\/li>\n<li>The following code will order alphabetically even if the default order is set to show featured listings first:\n<p>[php]<br \/>\nadd_filter(&#8216;geodir_posts_order_by_sort&#8217;,&#8217;_my_custom_order_by&#8217;,10,3);<br \/>\nfunction _my_custom_order_by($orderby, $sort_by, $table){<br \/>\n\tglobal $wpdb;<\/p>\n<p>\tif($sort_by==&#8217;is_featured_asc&#8217;){<br \/>\n\t\t$orderby .= &quot; $wpdb-&gt;posts.post_title ASC, &quot;;<br \/>\n\t}<br \/>\n\treturn $orderby;<br \/>\n}<br \/>\n[\/php]<\/p>\n<\/li>\n<\/ol>\n<h3><a name=\"hide-no-reviews\"><\/a>Detail page &#8211; hide &#8216;No Reviews&#8217; text if there are no reviews<\/h3>\n<ol>\n<li>Maybe you have a new site without a lot of reviews.<\/li>\n<li>The following code will hide the &#8216;no Reviews&#8217; text\n<p>[php]<br \/>\nfunction _gd_custom_hide_no_review_text( $class ) {<br \/>\n    global $post;<br \/>\n    if ( empty( $post-&gt;rating_count ) ) {<br \/>\n        $class .= &#8216;gd-custom-no-review&#8217;;<br \/>\n    }<br \/>\n    return $class;<br \/>\n}<br \/>\nadd_filter( &#8216;geodir_post_view_article_extra_class&#8217;, &#8216;_gd_custom_hide_no_review_text&#8217;, 10, 1 );<br \/>\n[\/php]<\/p>\n<p>And this short bit of CSS<br \/>\n<code>.gd-custom-no-review .geodir-entry-meta a.geodir-pcomments {<br \/>\ndisplay: none !important;<br \/>\n}<\/code><\/li>\n<\/ol>\n<h3><a name=\"remove-gd-scripts\"><\/a>Snippet to Remove GeoDirectory Scripts<\/h3>\n<p>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.<br \/>\nIn 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.<\/p>\n<p>[php]<br \/>\nremove_action( &#8216;wp_enqueue_scripts&#8217;, &#8216;geodir_templates_scripts&#8217; );<br \/>\nremove_action( &#8216;wp_enqueue_scripts&#8217;, &#8216;geodir_templates_styles&#8217;, 8 );<br \/>\n[\/php]<\/p>\n<p><code>geodir_is_geodir_page()<\/code> is used to identify the GD page<\/p>\n<p>Here&#8217;s a Snippet that will execute only on GD Pages.<br \/>\n*Caution* &#8211; 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!<\/p>\n<p>[php]<br \/>\nadd_action( &#8216;wp&#8217;, &#8216;wpdocs_dequeue_script&#8217;, 100 );<br \/>\nfunction wpdocs_dequeue_script() {<br \/>\n    if (!geodir_is_geodir_page()){<br \/>\n        remove_action(&#8216;wp_enqueue_scripts&#8217;, &#8216;geodir_templates_scripts&#8217;);<br \/>\n        remove_action(&#8216;wp_enqueue_scripts&#8217;, &#8216;geodir_templates_styles&#8217;, 8);<br \/>\n    }<br \/>\n}<br \/>\n[\/php]<\/p>\n<h3><a name=\"supreme-directory\"><\/a>Common Code Snippets for Supreme Directory<\/h3>\n<p>See the <a href=\"https:\/\/wpgeodirectory.com\/docs\/supreme-theme-faqs\/\" rel=\"noopener\" target=\"_blank\">Supreme Directory FAQ page<\/a><\/p>\n<h3><a name=\"icons\"><\/a>Showing icons instead of category links in list and grid view<\/h3>\n<p>Let&#8217;s change this<br \/>\n<img loading=\"lazy\" class=\"greenborder\" src=\"https:\/\/wpgeodirectory.com\/docs\/wp-content\/uploads\/sites\/3\/2015\/10\/codesnippet3.png\" alt=\"\" width=\"678\" height=\"230\" \/><br \/>\ninto this<br \/>\n<img loading=\"lazy\" class=\"greenborder\" src=\"https:\/\/wpgeodirectory.com\/docs\/wp-content\/uploads\/sites\/3\/2015\/10\/codesnippet2.png\" alt=\"\" width=\"681\" height=\"210\" \/><\/p>\n<ol>\n<li>Make sure to add <em><strong>categories<\/strong><\/em> to the <em><strong>Listings pages<\/strong><\/em>, as <a href=\"https:\/\/wpgeodirectory.com\/docs\/core-place-settings\/#show\">explained here<\/a>:<br \/>\n<img loading=\"lazy\" class=\"greenborder\" src=\"https:\/\/wpgeodirectory.com\/docs\/wp-content\/uploads\/sites\/3\/2015\/10\/codesnippet4.png\" alt=\"\" width=\"397\" height=\"74\" \/><\/li>\n<li>Now add this PHP code as a new <a href=\"https:\/\/wpgeodirectory.com\/docs\/useful-plugins\/#snippets\">Code Snippet<\/a> or to your child theme&#8217;s <em><strong>functions.php<\/strong><\/em>:\n<p>[php]<br \/>\nfunction _gd_custom_listing_view_category_icon( $html, $location, $cf ) {<br \/>\n    global $post, $_gd_custom_term_icons;<\/p>\n<p>    $html_var = $cf[&#8216;htmlvar_name&#8217;];<\/p>\n<p>    if ( !empty( $post-&gt;post_type ) &amp;&amp; $html_var == $post-&gt;post_type . &#8216;category&#8217; &amp;&amp; !empty( $post-&gt;{$html_var} ) ) {<br \/>\n        if ( empty( $_gd_custom_term_icons ) ) {<br \/>\n            $_gd_custom_term_icons = geodir_get_term_icon();<br \/>\n        }<\/p>\n<p>        $post_taxonomy = $post-&gt;post_type . &#8216;category&#8217;;<br \/>\n        $field_value = $post-&gt;{$html_var};<br \/>\n        $links = array();<br \/>\n        $terms = array();<br \/>\n        $termsOrdered = array();<br \/>\n        if (!is_array($field_value)) {<br \/>\n            $field_value = explode(&quot;,&quot;, trim($field_value, &quot;,&quot;));<br \/>\n        }<\/p>\n<p>        $field_value = array_unique($field_value);<\/p>\n<p>        if (!empty($field_value)) {<br \/>\n            foreach ($field_value as $term) {<br \/>\n                $term = trim($term);<\/p>\n<p>                if ($term != &#8221;) {<br \/>\n                    $term = get_term_by(&#8216;id&#8217;, $term, $html_var);<br \/>\n                    if (is_object($term)) {<br \/>\n                        $term_icon_url = ! empty( $_gd_custom_term_icons ) &amp;&amp; isset( $_gd_custom_term_icons[ $term-&gt;term_id ] ) ? $_gd_custom_term_icons[ $term-&gt;term_id ] : &#8221;;<br \/>\n                        $term_link = &#8216;&lt;a title=&quot;&#8217; . esc_attr( $term-&gt;name ) . &#8216;&quot; href=&quot;&#8217; . esc_attr(get_term_link($term, $post_taxonomy)) . &#8216;&quot;&gt;&lt;img src=&quot;&#8217; . $term_icon_url . &#8216;&quot; alt=&quot;&#8217; . esc_attr( $term-&gt;slug ) . &#8216; icon&quot; class=&quot;_gd-custom-cat-icon&quot; \/&gt;&lt;\/a&gt;&#8217;;<br \/>\n                        \/\/$term_link .= &quot; &lt;a href=&#8217;&quot; . esc_attr(get_term_link($term, $post_taxonomy)) . &quot;&#8217;&gt;&quot; . $term-&gt;name . &quot;&lt;\/a&gt;&quot;;<br \/>\n                        $links[] = $term_link;<br \/>\n                        $terms[] = $term;<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n            if (!empty($links)) {<br \/>\n                \/\/ order alphabetically<br \/>\n                asort($links);<br \/>\n                foreach (array_keys($links) as $key) {<br \/>\n                    $termsOrdered[$key] = $terms[$key];<br \/>\n                }<br \/>\n                $terms = $termsOrdered;<br \/>\n            }<br \/>\n        }<br \/>\n        \/\/$html_value = !empty($links) &amp;&amp; !empty($terms) ? wp_sprintf(&#8216;%l&#8217;, $links, (object)$terms) : &#8221;;<br \/>\n        $html_value = !empty($links) ? implode(&#8216; &#8216;, $links) : &#8221;;<\/p>\n<p>        if ($html_value != &#8221;) {<br \/>\n            $field_icon = geodir_field_icon_proccess($cf);<br \/>\n            if (strpos($field_icon, &#8216;http&#8217;) !== false) {<br \/>\n                $field_icon_af = &#8221;;<br \/>\n            } else if ($field_icon == &#8221;) {<br \/>\n                $field_icon_af = &#8221;;<br \/>\n            } else {<br \/>\n                $field_icon_af = $field_icon;<br \/>\n                $field_icon = &#8221;;<br \/>\n            }<\/p>\n<p>            $html = &#8216;<br \/>\n&lt;div class=&quot;geodir_more_info &#8216; . $cf[&#8216;css_class&#8217;] . &#8216; &#8216; . $html_var . &#8216;&quot; style=&quot;clear:both;&quot;&gt;&lt;span class=&quot;geodir-i-taxonomy geodir-i-category&quot; style=&quot;&#8217; . $field_icon . &#8216;&quot;&gt;&#8217; . $field_icon_af;<br \/>\n            $html .= (trim($cf[&#8216;site_title&#8217;])) ? __($cf[&#8216;site_title&#8217;], &#8216;geodirectory&#8217;) . &#8216;: &#8216; : &#8221;;<br \/>\n            $html .= &#8216;&lt;\/span&gt; &#8216; . $html_value . &#8216;&lt;\/div&gt;<\/p>\n<p>&#8216;;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return $html;<br \/>\n}<br \/>\nadd_filter( &#8216;geodir_custom_field_output_taxonomy_key_taxonomy&#8217;, &#8216;_gd_custom_listing_view_category_icon&#8217;, 10, 3 );<br \/>\n[\/php]\n<\/li>\n<li>And add this CSS:<br \/>\n[css]<br \/>\n.geodir_category_list_view ._gd-custom-cat-icon {<br \/>\n    vertical-align: middle;<br \/>\n    height: 24px;<br \/>\n    margin: 0 5px;<br \/>\n}<br \/>\n[\/css]<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>On this page: Introduction Add the package ID\u00a0to 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 &#8216;No Reviews&#8217; Remove GD Scripts Supreme Directory Showing icons instead of category links in list and grid view &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/wpgeodirectory.com\/docs\/common-code-snippets\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Common code snippets&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1676,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[],"amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/wpgeodirectory.com\/docs\/wp-json\/wp\/v2\/posts\/22882"}],"collection":[{"href":"https:\/\/wpgeodirectory.com\/docs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpgeodirectory.com\/docs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpgeodirectory.com\/docs\/wp-json\/wp\/v2\/users\/1676"}],"replies":[{"embeddable":true,"href":"https:\/\/wpgeodirectory.com\/docs\/wp-json\/wp\/v2\/comments?post=22882"}],"version-history":[{"count":0,"href":"https:\/\/wpgeodirectory.com\/docs\/wp-json\/wp\/v2\/posts\/22882\/revisions"}],"wp:attachment":[{"href":"https:\/\/wpgeodirectory.com\/docs\/wp-json\/wp\/v2\/media?parent=22882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpgeodirectory.com\/docs\/wp-json\/wp\/v2\/categories?post=22882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpgeodirectory.com\/docs\/wp-json\/wp\/v2\/tags?post=22882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}