{"id":1015,"date":"2025-03-24T14:28:16","date_gmt":"2025-03-24T14:28:16","guid":{"rendered":"https:\/\/wpgeodirectory.com\/documentation\/article\/hide-show-gd-ninja-form-widget-based-on-listing-package-price\/"},"modified":"2025-04-03T15:24:38","modified_gmt":"2025-04-03T14:24:38","slug":"hide-show-gd-ninja-form-widget-based-on-listing-package-price","status":"publish","type":"gd_place","link":"https:\/\/wpgeodirectory.com\/documentation\/article\/code-snippets\/hide-show-gd-ninja-form-widget-based-on-listing-package-price\/","title":{"rendered":"Hide \/ Show GD Ninja Form widget based on listing package price"},"content":{"rendered":"\n<p>See snippets comments for the form&#8217;s hidden fields<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">\/*\n * 2 forms:\n * one that goes to the listing owner email for paid listings,\n * the other goes to the site admin, for free listings.\n * \n * All forms must have a hidden field labelled &#039;package_type&#039;,\n * with default value set to &#039;free&#039; &amp; &#039;paid&#039;, respectivelly\n * Both forms are added to the single template, and the filter hides or shows either.\n *\/\n\nadd_filter( &#039;geodir_show_ninja_form_widget&#039;, &#039;gd_support_maybe_show_ninja&#039;, 999, 3 );\n\nfunction gd_support_maybe_show_ninja( $show, $post, $args ) {\n    if ( ! $post instanceof WP_Post || ! class_exists( &#039;GeoDir_Pricing_Package&#039; ) || ! function_exists( &#039;Ninja_Forms&#039; ) ) {\n        return $show;\n    }\n    $form_id = empty( $args&#091;&#039;form_id&#039;&#093; ) ? false : $args&#091;&#039;form_id&#039;&#093;;\n    if ( ! $form_id ) return $show;\n\n    $fields = Ninja_Forms()-&gt;form( $form_id )-&gt;get_fields();\n    if ( empty( $fields ) || ! is_array( $fields ) ) return $show;\n\n    $form_package = false;\n    foreach ( $fields as $field ) {\n        $settings = $field-&gt;get_settings();\n        if ( empty( $settings&#091;&#039;type&#039;&#093; ) || empty( $settings&#091;&#039;label&#039;&#093; ) || empty( $settings&#091;&#039;default&#039;&#093; ) ) {\n            continue;\n        }\n        if ( &#039;hidden&#039; === $settings&#091;&#039;type&#039;&#093; &amp;&amp; &#039;package_type&#039; === $settings&#091;&#039;label&#039;&#093; ) {\n            $form_package = $settings&#091;&#039;default&#039;&#093;;\n            break;\n        }\n    }\n\n    if ( ! $form_package ) return $show;\n\n    $package_id         = geodir_get_post_package_id( $post-&gt;ID, $post-&gt;post_type );\n    $package            = GeoDir_Pricing_Package::get_instance( $package_id );\n    if ( ! $package ) return $show;\n    $package_is_free    = ! intval( $package-&gt;amount ) ? true : false;\n\n    switch( $form_package ) {\n        case &#039;free&#039;:\n            return $package_is_free ? true : false;\n        case &#039;paid&#039;:\n            return $package_is_free ? false : true;\n        default:\n            return $show;\n\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>See snippets comments for the form&#8217;s hidden fields<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"article\/tags":[],"article\/categories":[211,433],"class_list":["post-1015","gd_place","type-gd_place","status-publish","hentry","gd_placecategory-tips-tricks-more","gd_placecategory-code-snippets"],"_links":{"self":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/1015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article"}],"about":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/types\/gd_place"}],"author":[{"embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/comments?post=1015"}],"version-history":[{"count":0,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/1015\/revisions"}],"wp:attachment":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/media?parent=1015"}],"wp:term":[{"taxonomy":"gd_place_tags","embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/tags?post=1015"},{"taxonomy":"gd_placecategory","embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/categories?post=1015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}