[Resolved] generate_header_element_display being called when in admin

Home Forums Support [Resolved] generate_header_element_display being called when in admin

Home Forums Support generate_header_element_display being called when in admin

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1633892
    Natalie

    I have code in my functions.php based on the information here:

    https://docs.generatepress.com/article/generate_header_element_display/

    my code includes these lines (and lots of similar lines)

        global $post;
    
        $legal_pp = array(884,2821));
        if ( 1945 === $element_id &&  ((in_array($post->post_parent, $legal_pp) ) || (in_array($post->ID , $legal_pp)))) {
            return true;
        }

    This works fine.

    I have debug turned on in my local install.

    I have noticed that if I’m in the admin and don’t have the website open at all, I am getting error messages :

    “Trying to get property ‘post_parent’ of non-object” for the lines in functions.php with the code above ( and similar lines)

    What I don’t understand is why that filter (generate_header_element_display) is being called when I am in admin as shouldn’t it only apply to the frontend?

    I can resolve the whole problem by putting :

    if(is_admin()) return true;
    as the first line in my function. I’m happy to do that, but would it be a good idea to document that on this page?
    https://docs.generatepress.com/article/generate_header_element_display/

    #1634155
    David
    Staff
    Customer Support

    Hi there,

    can you share the full code including the add_filter – so we can take a look

    #1634164
    Natalie

    Thanks – here’s my full code

    function sav_add_headers($display, $element_id) {
        // if a page header is already defined then return
    
        if ( $display ) return true;
    
        global $post;
    
        if ( 1993 === $element_id && is_page_template( 'saville-aptitude-page.php' ) ) {
            return true;
        }
    
        $legal_pp = sav_build_id_array(array(884));
    
        if ( 1945 === $element_id &&  ((in_array($post->post_parent, $legal_pp) ) || (in_array($post->ID , $legal_pp)))) {
            return true;
        }
    
        $strength_pp = sav_build_id_array(array(374));
        if ( 1947 === $element_id &&  ((in_array($post->post_parent, $strength_pp) ) || (in_array($post->ID , $strength_pp)))) {
            return true;
        }
    
        $wave_pp = sav_build_id_array(array(376, 1267));
        if ( 1953 === $element_id &&  ((in_array($post->post_parent, $wave_pp) ) || (in_array($post->ID , $wave_pp)))) {
            return true;
        }
    
        $consult_pp = sav_build_id_array(array(2602));
        if ( 1984 === $element_id &&  ((in_array($post->post_parent, $consult_pp) ) || (in_array($post->ID , $consult_pp)))) {
            return true;
        }
    
        return $display;
    }
    
    add_filter( 'generate_header_element_display', 'sav_add_headers', 10, 2 );

    and the sav_build_id_array function:

    function sav_build_id_array($page_ids) {
        $new_page_ids = $page_ids;
        foreach ($page_ids as $key => $value) {
            $da_id = apply_filters( 'wpml_object_id', $value, 'page', FALSE, 'da'  );
            if($da_id > 0) $new_page_ids[] = $da_id;
        }
        return $new_page_ids;
    }
    #1634584
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    We run Elements in the Dashboard so we can apply some of their settings while editing pages. For example, the content width option in the Layout Element.

    What you can do is make your code a little more bulletproof.

    So instead of just global $post;, do this:

    global $post;
    
    if ( ! is_object( $post ) ) {
        return $display;
    }

    Let me know if that helps or not πŸ™‚

    #1635401
    Natalie

    Thanks Tom

    That is obviously more robust than using
    if(is_admin()) return true;

    and it works fine

    How are you getting on with a fix for the header population issue? (which is why I need this code)
    ref: https://generatepress.com/forums/topic/element-display-rules-very-slow-to-populate/

    #1635954
    Tom
    Lead Developer
    Lead Developer

    That’s a difficult issue to fix, but it’s one that’s still on my radar.

    We’re making some big changes to Elements, and I suspect a Display Rules re-write will be in phase 2 πŸ™‚

    #1636531
    Natalie

    great – thanks for the update and the excellent support as always πŸ™‚

    #1637294
    Tom
    Lead Developer
    Lead Developer

    No problem! πŸ™‚

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.