[Resolved] Conditional Test For Disabled Elements

Home Forums Support [Resolved] Conditional Test For Disabled Elements

Home Forums Support Conditional Test For Disabled Elements

  • This topic has 14 replies, 4 voices, and was last updated 2 years ago by David.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #423835
    Antar

    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

    #424225
    Tom
    Lead Developer
    Lead Developer

    You 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
    }
    #424229
    Antar

    Perfect. Just what I was hoping for.

    Thanks, Tom.

    Antar

    #424237
    Tom
    Lead Developer
    Lead Developer

    You’re welcome πŸ™‚

    #2152710
    Elvis

    Hello,
    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 with before_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.

    #2152854
    David
    Staff
    Customer Support

    Hi 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.

    #2153138
    Elvis

    It’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?
    Thanks

    #2153172
    David
    Staff
    Customer Support
    #2158208
    Elvis

    O, nice. πŸ™‚ Thanks. So we can also load assets for element only when element is present. Yummy. πŸ™‚
    Can this be done with local templates too?!

    #2158232
    Elvis

    Ok, 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.

    #2159651
    Tom
    Lead Developer
    Lead Developer

    Try this as the foreach:

    foreach ( $generate_elements as $key => $data )

    And this to get the ID: $data['id']

    #2163215
    Elvis

    Thanks 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.
    Thanks

    #2165287
    Fernando
    Customer Support

    Hi 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. πŸ™‚

    #2165481
    Elvis

    Hello 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.

    #2165719
    David
    Staff
    Customer Support

    Hi 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')

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