Customizing template with ACF, issues

This topic contains 11 replies, has 4 voices, and was last updated by  Jansen Tolle 7 years, 6 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #373899

    Jansen Tolle
    Free User
    Post count: 13

    Hello all! I love this plugin so far, but I’m having a real tough time getting it to play nicely with Advanced Custom Fields.

    Rather than relying solely on widgets, our clients prefer to have a ‘wizard’-like interface to enter content. Typically we set up a handful of custom fields and pull them into the front-end, which works great. However, I can’t get custom fields to work inside of GD!

    I’m my geodir-home.php file, I’ve successfully added some wrapper HTML and other things, but when using ACF’s get_field functions, they always return nothing. I pull the same code out and plop it into another non GD page and it works as expected. Hmm.

    Anyone ever run into this before?

    This works
    echo(get_post_meta($post->ID, “home_intro_content”, true ));

    This does not work
    the_field( “home_intro_content”, $post->ID );

    However, both work just fine if I put them on a random page. I’ve verified that the ID is being pulled properly, and tried a bunch of other stuff to no avail.

    #373933

    Paolo
    Site Admin
    Post count: 31206

    Hi,

    as far as I know ACF is not compatible at all with GD as we use custom DB tables and PHP API that ACF is not aware of.

    However I asked to the developers to check your case and let us know if they see a solution.

    They’ll let us know asap.

    Thanks

    #373940

    Jansen Tolle
    Free User
    Post count: 13

    I’m not trying to add custom fields to listings or do anything crazy. I just can’t figure out why these functions seem to .. malfunction .. while inside of the GD templates

    #374293

    Jansen Tolle
    Free User
    Post count: 13

    Just for further debugging purposes, I’ve tried to narrow this down a bit more by checking the output of get_field_object() at various points. It’s worth mentioning that using get_post_meta on the same custom field seems to work, but this obviously has limitations compared to ACF.

    Here’s the example code

    
    
    // Getting an example field from the home page. This example is being used on a non-GD page
    // 57 is the ID of the home page. Hard-coding it here so we can be sure there is nothing goofy about any $post->ID variables being passed in
    print_r (get_field_object( "home_intro_content", 57 ));

    On a standard page, this works great. It tells me all the information about the field from the home page. Here’s the output:

    Array ( [ID] => 331 [key] => field_58f12396b6471 [label] => Content [name] => home_intro_content [prefix] => acf [type] => textarea [value] =>

    This text should introduce visitors to the website, explaining its purpose concisely. Like the “Elevator Speech” for your business. Small batch DIY gochujang mixtape bicycle rights, squid ethical authentic salvia twee banh mi cray. Sustainable migas irony, street art actually gochujang echo park edison bulb squid pinterest salvia occupy stumptown DIY 8-bit. Literally lo-fi live-edge normcore butcher woke migas, man bun kale chips blog ethical etsy vinyl. Vinyl craft beer lyft af. Explore the map below!

    This text should introduce visitors to the website, explaining its purpose concisely. Like the “Elevator Speech” for your business. Small batch DIY gochujang mixtape bicycle rights, squid ethical authentic salvia twee banh mi cray. Sustainable migas irony, street art actually gochujang echo park edison bulb squid pinterest salvia occupy stumptown DIY 8-bit. Literally lo-fi live-edge normcore butcher woke migas, man bun kale chips blog ethical etsy vinyl. Vinyl craft beer lyft af. Explore the map below!
    [menu_order] => 3 [instructions] => 200-ish words about the site. [required] => 1 [id] => [class] => [conditional_logic] => 0 [parent] => 323 [wrapper] => Array ( [width] => [class] => [id] => ) [_name] => home_intro_content [_prepare] => 0 [_valid] => 1 [default_value] => [placeholder] => [maxlength] => [rows] => [new_lines] => wpautop )

    Using this same exact code anywhere withing a GD page, the function returns a bunch of nonsense. I’ve tried this in all sorts of areas, before and after various geodir actions (eg geodir_wrapper_open). It always gives me something like this:

    Array ( [ID] => 251 [key] => field_5536634644729 [label] => Columns [name] => home_intro_content [prefix] => acf [type] => repeater [value] => [menu_order] => 0 [instructions] => [required] => 1 [id] => [class] => [conditional_logic] => 0 [parent] => 250 [wrapper] => Array ( [width] => [class] => [id] => ) [_name] => multiple_columns [_prepare] => 0 [_valid] => 1 [min] => 0 [max] => 4 [layout] => table [button_label] => Add Column [collapsed] => [parent_layout] => 5536632844728 [sub_fields] => Array ( [0] => Array ( [ID] => 252 [key] => field_553663864472a [label] => Content [name] => mc_content [prefix] => acf [type] => wysiwyg [value] => [menu_order] => 0 [instructions] => [required] => 1 [id] => [class] => [conditional_logic] => 0 [parent] => 251 [wrapper] => Array ( [width] => [class] => [id] => ) [_name] => mc_content [_prepare] => 0 [_valid] => 1 [tabs] => all [toolbar] => full [media_upload] => 1 [default_value] => [delay] => 0 ) ) )

    As you can see, all of the values are either blank or incorrect when using this function on a GD page. I’m wondering if, somehow, GD has a function that is blowing this up.

    Any help would be appreciated – I really don’t want to have to hardcode in a bunch of stuff that will make it difficult for my clients to work on the site.

    #375061

    Jansen Tolle
    Free User
    Post count: 13

    Well, I figured it out and I must say I am quite astonished with some of the coding going on here. Wow…

    Add this to your functions file to make this plugin work with ACF

    
    
    function override_the_stupid_plugin_filters($query) {
        global $wp_query, $wpdb, $geodir_post_type, $table, $dist, $mylat, $mylon, $s, $snear, $s, $s_A, $s_SA;
    
        // fix woocommerce shop products filtered by language for GD + WPML + Woocommerce
        if (!geodir_is_geodir_page()) {
            return;
        }
        
        //----------------------_START_CUSTOMIZED CODE---------------------------
        $removeFilterNicely = function($filter) {
            if( empty($GLOBALS['wp_filter'][$filter]) || empty($GLOBALS['wp_filter'][$filter]['callbacks']) ) {
                return;
            }
            foreach($GLOBALS['wp_filter'][$filter]['callbacks'] as $priority => $callbacks) {
                foreach($callbacks as $reference => $callback) {
                    if( stripos($callback['function'],'ACF') !== FALSE || stripos(get_class($callback['function']), 'ACF') !== FALSE ) {
                        unset($GLOBALS['wp_filter'][$filter]['callbacks'][$priority][$reference]);
                    }
                }
            }
        };
        $removeFilterNicely('query');
        $removeFilterNicely('posts_search');
        $removeFilterNicely('posts_fields');
        $removeFilterNicely('posts_join');
        $removeFilterNicely('posts_orderby');
        $removeFilterNicely('posts_where');
        /** remove all pre filters
        remove_all_filters('query');
        remove_all_filters('posts_search');
        remove_all_filters('posts_fields');
        remove_all_filters('posts_join');
        remove_all_filters('posts_orderby');
        remove_all_filters('posts_where');
        **/
        
        //----------------------_END_CUSTOMIZED CODE---------------------------
        
        if ((is_search() && isset($_REQUEST['geodir_search']) && $_REQUEST['geodir_search'] != '')):
            if (isset($_REQUEST['scat']) && $_REQUEST['scat'] == 'all')
                $_REQUEST['scat'] = '';
            //if(isset($_REQUEST['s']) && $_REQUEST['s'] == '+') $_REQUEST['s'] = '';
            if (isset($_REQUEST['sdist'])) {
                ($_REQUEST['sdist'] != '0' && $_REQUEST['sdist'] != '') ? $dist = esc_attr($_REQUEST['sdist']) : $dist = 25000;
            } elseif (get_option('geodir_search_dist') != '') {
                $dist = get_option('geodir_search_dist');
            } else {
                $dist = 25000;
            } //  Distance
    
            if (isset($_REQUEST['sgeo_lat'])) {
                $mylat = (float) esc_attr($_REQUEST['sgeo_lat']);
            } else {
                $mylat = (float) geodir_get_current_city_lat();
            } //  Latitude
    
            if (isset($_REQUEST['sgeo_lon'])) {
                $mylon = (float) esc_attr($_REQUEST['sgeo_lon']);
            } else {
                $mylon = (float) geodir_get_current_city_lng();
            } //  Distance
    
            if (isset($_REQUEST['snear'])) {
                $snear = trim(esc_attr($_REQUEST['snear']));
            }
    
            if (isset($_REQUEST['s'])) {
                $s = trim(esc_attr(wp_strip_all_tags($_REQUEST['s'])));
            }
    
            if ($snear == 'NEAR ME') {
                $ip = $_SERVER['REMOTE_ADDR'];
                $addr_details = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $ip));
                $mylat = stripslashes(geodir_utf8_ucfirst($addr_details[geoplugin_latitude]));
                $mylon = stripslashes(geodir_utf8_ucfirst($addr_details[geoplugin_longitude]));
            }
            if (strstr($s, ',')) {
                $s_AA = str_replace(" ", "", $s);
                $s_A = explode(",", $s_AA);
                $s_A = implode('","', $s_A);
                $s_A = '"' . $s_A . '"';
            } else {
                $s_A = '"' . $s . '"';
            }
            if (strstr($s, ' ')) {
                $s_SA = explode(" ", $s);
            } else {
                $s_SA = '';
            }
        endif;
    }
    #375100

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Hi Jansen,

    We reset query filters on our pages at the earliest possible time, we have our own custom table queries that can and were being broken by poor catch all filters with no specific checks.
    I could go into a long explanation about it but i think u get the gist of things since you just are looping through removing just ACF ones.

    If you would like a full explanation or to discuss it please just ask.

    Thanks,

    Stiofan

    #375136

    Jansen Tolle
    Free User
    Post count: 13

    Yeah, I figured it out 😉

    I think it would be better to use a less destructive method of removing filters if possible, this can create a lot of headaches for developers. Or at least make it clear that this is happening.

    Feel free to add my code to some sort of FAQ in the docs – I guarantee I’m not the first one to run into this.

    #375611

    Michael Browning
    Buyer
    Post count: 17

    Am I understanding you correctly that this additional code will allow ACF to operate as intended on GeoDirectory Front End pages and in the back end as well? Are there any performance drawbacks or otherwise that this might present?

    #375614

    Jansen Tolle
    Free User
    Post count: 13

    I am not certain about the backend, but yes – this does allow ACF fields to be added to pages with WPGD functinality (such as the home page).

    I don’t believe there are any major performance implications

    #375618

    Jansen Tolle
    Free User
    Post count: 13

    Also I forgot to include this bit – can’t edit my previous comment. Have to remove the problematic action and replace it with our version.

    
    
    remove_action('pre_get_posts', 'set_listing_request', 0);
    add_action('pre_get_posts', 'override_the_stupid_plugin_filters', 0);
    #375621

    Michael Browning
    Buyer
    Post count: 17

    Does that go before or after the aforementioned code? Also would you like to collab on this in Github or something so other people can use/improve on this script?

    #375798

    Jansen Tolle
    Free User
    Post count: 13

    Here’s the full code, for clarification. It’d be better if the developers just merged in this fix or added a way to specify filters to exclude from their obliteration.

    Pardon all of my anger that comes through in the code :p it took me HOURS to figure this out.

    
    
    remove_action('pre_get_posts', 'set_listing_request', 0);
    add_action('pre_get_posts', 'override_the_stupid_plugin_filters', 0);
    /**
     * NOTE THIS OVERRIDES THE FUNCTION CURRECTLY USED BY THE STPUID PLUGIN BECAUSE IT DELETES ACF FUNCTIONALITY
     * Sets the location request parameters.
     *
     * @since 1.0.0
     *
     * @global object $wp_query WordPress Query object.
     * @global object $wpdb WordPress Database object.
     * @global string $geodir_post_type The post type.
     * @global array $geodir_addon_list List of active GeoDirectory extensions.
     * @global string $table Listing table name.
     * @global float $dist Distance value to be filtered.
     * @global string $mylat Current latitude.
     * @global string $mylon Current longitude.
     * @global string $s Search keyword.
     * @global string $snear Nearest location to search.
     * @global string $s_A Extra parameters.
     * @global string $s_SA Extra parameters.
     */
    function override_the_stupid_plugin_filters($query) {
        global $wp_query, $wpdb, $geodir_post_type, $table, $dist, $mylat, $mylon, $s, $snear, $s, $s_A, $s_SA;
    
        // fix woocommerce shop products filtered by language for GD + WPML + Woocommerce
        if (!geodir_is_geodir_page()) {
            return;
        }
        
        //----------------------_START_CUSTOMIZED CODE---------------------------
        $removeFilterNicely = function($filter) {
            if( empty($GLOBALS['wp_filter'][$filter]) || empty($GLOBALS['wp_filter'][$filter]['callbacks']) ) {
                return;
            }
            foreach($GLOBALS['wp_filter'][$filter]['callbacks'] as $priority => $callbacks) {
                foreach($callbacks as $reference => $callback) {
                    if( stripos($callback['function'],'ACF') !== FALSE || stripos(get_class($callback['function']), 'ACF') !== FALSE ) {
                        unset($GLOBALS['wp_filter'][$filter]['callbacks'][$priority][$reference]);
                    }
                }
            }
        };
        $removeFilterNicely('query');
        $removeFilterNicely('posts_search');
        $removeFilterNicely('posts_fields');
        $removeFilterNicely('posts_join');
        $removeFilterNicely('posts_orderby');
        $removeFilterNicely('posts_where');
        /** remove all pre filters
        remove_all_filters('query');
        remove_all_filters('posts_search');
        remove_all_filters('posts_fields');
        remove_all_filters('posts_join');
        remove_all_filters('posts_orderby');
        remove_all_filters('posts_where');
        **/
        
        //----------------------_END_CUSTOMIZED CODE---------------------------
        
        if ((is_search() && isset($_REQUEST['geodir_search']) && $_REQUEST['geodir_search'] != '')):
            if (isset($_REQUEST['scat']) && $_REQUEST['scat'] == 'all')
                $_REQUEST['scat'] = '';
            //if(isset($_REQUEST['s']) && $_REQUEST['s'] == '+') $_REQUEST['s'] = '';
            if (isset($_REQUEST['sdist'])) {
                ($_REQUEST['sdist'] != '0' && $_REQUEST['sdist'] != '') ? $dist = esc_attr($_REQUEST['sdist']) : $dist = 25000;
            } elseif (get_option('geodir_search_dist') != '') {
                $dist = get_option('geodir_search_dist');
            } else {
                $dist = 25000;
            } //  Distance
    
            if (isset($_REQUEST['sgeo_lat'])) {
                $mylat = (float) esc_attr($_REQUEST['sgeo_lat']);
            } else {
                $mylat = (float) geodir_get_current_city_lat();
            } //  Latitude
    
            if (isset($_REQUEST['sgeo_lon'])) {
                $mylon = (float) esc_attr($_REQUEST['sgeo_lon']);
            } else {
                $mylon = (float) geodir_get_current_city_lng();
            } //  Distance
    
            if (isset($_REQUEST['snear'])) {
                $snear = trim(esc_attr($_REQUEST['snear']));
            }
    
            if (isset($_REQUEST['s'])) {
                $s = trim(esc_attr(wp_strip_all_tags($_REQUEST['s'])));
            }
    
            if ($snear == 'NEAR ME') {
                $ip = $_SERVER['REMOTE_ADDR'];
                $addr_details = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $ip));
                $mylat = stripslashes(geodir_utf8_ucfirst($addr_details[geoplugin_latitude]));
                $mylon = stripslashes(geodir_utf8_ucfirst($addr_details[geoplugin_longitude]));
            }
            if (strstr($s, ',')) {
                $s_AA = str_replace(" ", "", $s);
                $s_A = explode(",", $s_AA);
                $s_A = implode('","', $s_A);
                $s_A = '"' . $s_A . '"';
            } else {
                $s_A = '"' . $s . '"';
            }
            if (strstr($s, ' ')) {
                $s_SA = explode(" ", $s);
            } else {
                $s_SA = '';
            }
        endif;
    }
Viewing 12 posts - 1 through 12 (of 12 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount