Site logo

Archived topic

generate_header_element_display being called when in admin

7 replies · Started by Natalie on January 26, 2021

Viewing posts 1–8 of 8

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/

Hi there,

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

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;
}

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 :)

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 :)

great - thanks for the update and the excellent support as always :-)

No problem! :)

This archived topic is closed to new replies.