Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
Created July 13, 2021 19:28
Show Gist options
  • Save jorpdesigns/f3809a7668c885e81d6e223acc1e2219 to your computer and use it in GitHub Desktop.
Save jorpdesigns/f3809a7668c885e81d6e223acc1e2219 to your computer and use it in GitHub Desktop.
Snippet for creating hidden content toggle
<?php
add_shortcode( 'hidden_content', 'hidden_content' );
function hidden_content( $atts, $content = null ) {
$paragraphedText = "<p>" . implode( "</p>\n\n<p>", preg_split( '/\n(?:\s*\n)+/', $content ) ) . "</p>";
return '<div class="hidden-content" style="display: none;">' . $paragraphedText . '</div>';
}
// ADD JS SCRIPT TO FOOTER
add_action( 'wp_footer', 'hidden_content_script' );
function hidden_content_script() { ?>
<script>
jQuery( function($) {
$(document).ready(function(){
$( ".hidden-content" ).after('<div class="read-more-button"><span class="description-toggle show" style="display: inline-block;">Read More</span><span class="description-toggle hide" style="display: none;">Show Less</span></div>');
// For WP Bakery Text Blocks
$('.wpb_text_column').on('click', '.description-toggle.show', function () {
$(this).closest(".wpb_text_column").find(".hidden-content").slideToggle(300);
$(this).closest(".wpb_text_column").find(".description-toggle.show").css('display', 'none');
$(this).closest(".wpb_text_column").find(".description-toggle.hide").css('display', 'inline-block');
});
$('.wpb_text_column').on('click', '.description-toggle.hide', function () {
$(this).closest(".wpb_text_column").find(".hidden-content").slideToggle(300);
$(this).closest(".wpb_text_column").find(".description-toggle.show").css('display', 'inline-block');
$(this).closest(".wpb_text_column").find(".description-toggle.hide").css('display', 'none');
});
});
});
</script>
<?php }
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment