- This topic has 14 replies, 4 voices, and was last updated 2 years, 7 months ago by David.
-
AuthorPosts
-
November 14, 2017 at 2:16 am #423835Antar
Hello, All –
Is there a way to test if certain elements have been disabled? I want to create hooks that test if (for example) the featured image is disabled, or the title, etc.
How should I do this?
Thanks,
Antar
November 14, 2017 at 9:28 am #424225TomLead DeveloperLead DeveloperYou could do this:
$disable_top_bar = get_post_meta( get_the_ID(), '_generate-disable-top-bar', true ); $disable_header = get_post_meta( get_the_ID(), '_generate-disable-header', true ); $disable_nav = get_post_meta( get_the_ID(), '_generate-disable-nav', true ); $disable_secondary_nav = get_post_meta( get_the_ID(), '_generate-disable-secondary-nav', true ); $disable_post_image = get_post_meta( get_the_ID(), '_generate-disable-post-image', true ); $disable_headline = get_post_meta( get_the_ID(), '_generate-disable-headline', true ); $disable_footer = get_post_meta( get_the_ID(), '_generate-disable-footer', true );
Then check like this:
if ( $disable_top_bar ) { // Top bar is disabled }
November 14, 2017 at 9:33 am #424229AntarPerfect. Just what I was hoping for.
Thanks, Tom.
Antar
November 14, 2017 at 9:37 am #424237TomLead DeveloperLead DeveloperYou’re welcome π
March 13, 2022 at 4:29 am #2152710ElvisHello,
I want to have my site footer be a<footer>
element and located direcly inside body.
So, I clicked on my default element > layout to dissable site-footer.
Then created my footer as an element > block > hook withbefore_footer
location.
And it works.
In order to clean after my self, I wish to remove the now empty<div class="site-footer"> </div>
markup. But I wish to run that only when the dissable footer option is actually ran.Tried this:
function remove_site_footer_wrapper() { $disable_footer = get_post_meta( get_the_ID(), '_generate-disable-footer', true ); if ( $disable_footer ) { ?> <script type="text/javascript"> const siteFooter = document.querySelector( ".site-footer" ); siteFooter.parentNode.removeChild( siteFooter ); </script> <style> /** If no JS */ .apuri-body > .site-footer {display: none; visibility: hidden;} </style> <?php } } add_action('wp_footer', 'remove_site_footer_wrapper');
If I remove
$disable_footer
condition, it works, with it id does not! What am I missing?
Thanks.March 13, 2022 at 7:38 am #2152854DavidStaffCustomer SupportHi there,
how is the Element being disabled ? As I believe the above only applies if you use the Disable elements meta box in the post editor.
March 13, 2022 at 10:05 am #2153138ElvisIt’s dissabled via option in the layout element that is acive globally, with some exceptions. It would make no sense to dissable a global site part such as site-footer via individual page, if I plan not to use it on most pages. But yeah
get_the_ID()
does imply that what you are saying is true.So there are no alternative ways to go about this?
ThanksMarch 13, 2022 at 10:40 am #2153172DavidStaffCustomer SupportMarch 17, 2022 at 9:53 am #2158208ElvisO, nice. π Thanks. So we can also load assets for element only when element is present. Yummy. π
Can this be done with local templates too?!March 17, 2022 at 10:10 am #2158232ElvisOk, I can do it, but kinda crudely.
function remove_site_footer_wrapper() { $disable_footer = get_post_meta( get_the_ID(), '_generate-disable-footer', true ); global $generate_elements; if ( $generate_elements[144] ) { ?> <script type="text/javascript" id="apuri-default-footer-remove"> const siteFooter = document.querySelector( ".site-footer" ); siteFooter.parentNode.removeChild( siteFooter ); </script> <style> /** If no JS */ .apuri-body > .site-footer {display: none; visibility: hidden;} </style> <?php } } add_action('wp_footer', 'remove_site_footer_wrapper');
But what I would like to check is weather the block in question has the dissable footer true. I tried this, but no luck. π
function remove_site_footer_wrapper() { global $generate_elements; foreach ( $generate_elements as $element ) { $disable_footer = get_post_meta( $element->ID, '_generate-disable-footer', true ); if ( $disable_footer ) { ?> <script type="text/javascript" id="apuri-default-footer-remove"> const siteFooter = document.querySelector( ".site-footer" ); siteFooter.parentNode.removeChild( siteFooter ); </script> <style> /** If no JS */ .apuri-body > .site-footer {display: none; visibility: hidden;} </style> <?php } } } add_action('wp_footer', 'remove_site_footer_wrapper');
the print_r($generate_elements); shows id and element type. So cannot get it from there.
Thanks a lot.March 18, 2022 at 7:59 pm #2159651TomLead DeveloperLead DeveloperTry this as the
foreach
:foreach ( $generate_elements as $key => $data )
And this to get the ID:
$data['id']
March 22, 2022 at 3:38 am #2163215ElvisThanks Tom,
sure it’s an array [facepalm].Tried but does not work. I am able to get the ID both with
$generate_elements as $data
as well as$generate_elements as $key => $data
when I use$data['id']
to output.function prefix_get_your_elements() { global $generate_elements; foreach ( $generate_elements as $data ) { ?> <pre style="min-height: 800px; padding-top: 7em"> <?php print_r($data['id']); ?> </pre> <?php } }
This outputs the id’s of elements used on the current page. But, the link between the element (identified by the id) and the option to dissabled footer is where it fails.
This is my function
function remove_site_footer_wrapper() { global $generate_elements; foreach ( $generate_elements as $key => $data ) { $disable_footer = get_post_meta( $data['id'], '_generate-disable-footer', true ); if ( $disable_footer ) { ?> <script type="text/javascript" id="apuri-default-footer-remove"> const siteFooter = document.querySelector( ".site-footer" ); siteFooter.parentNode.removeChild( siteFooter ); </script> <style> /** If no JS */ .apuri-body > .site-footer {display: none; visibility: hidden;} </style> <?php } } } add_action('wp_footer', 'remove_site_footer_wrapper');
And it does not work.
ThanksMarch 23, 2022 at 6:21 pm #2165287Fernando Customer SupportHi Elvis,
You can try modifying your code into something like this:
<?php global $generate_elements; foreach ( $generate_elements as $generate_element ) { if ( $generate_element['type'] === "layout" && $generate_element['id'] === 391895 ) { ?> <script type="text/javascript" id="apuri-default-footer-remove"> const siteFooter = document.querySelector( ".site-footer" ); siteFooter.parentNode.removeChild( siteFooter ); </script> <style> /** If no JS */ .apuri-body > .site-footer {display: none; visibility: hidden;} </style> <?php } } ?>
This would check if there is a Layout Element with a specific ID active on a page.
Kindly replace
391895
with the ID of your Layout Element disabling the Site Footer.Try to
var_dump($generate_elements)
to get the specific id of the Layout Element.Tested this on my end and it seems to be working as expected.
Kindly let us know how it goes. π
March 24, 2022 at 12:07 am #2165481ElvisHello Fernando,
thanks for this, I have something like this and it works, but it’s hardcoding a particular element.
What I wanted is to really check for the dissable option, because then I would have a portable code block to use accross my sites, that would work whenever a layout block dissables a footer, header … or anything.Thanks again.
March 24, 2022 at 4:42 am #2165719DavidStaffCustomer SupportHi there,
maybe a curve ball – what about using
has_action()
https://developer.wordpress.org/reference/functions/has_action/
to determine whether the hook has the relevant callback.
has_action('generate_footer', 'generate_construct_footer')
-
AuthorPosts
- You must be logged in to reply to this topic.