Skip to content

Instantly share code, notes, and snippets.

@robertuniqid
Last active May 4, 2020 15:10
Show Gist options
  • Save robertuniqid/ee9867fe0b0a39e8da901bedd59b0ae9 to your computer and use it in GitHub Desktop.
Save robertuniqid/ee9867fe0b0a39e8da901bedd59b0ae9 to your computer and use it in GitHub Desktop.
WPEP Center Section
<?php
add_action('init', function () {
add_shortcode('ec_center', function ( $atts, $content ) {
$response = '<div class="wpep-center-section">';
$response .= do_shortcode($content);
$response .= '</div>';
return $response;
});
});
add_action('wp_footer', function () {
echo '<script type="text/javascript">
jQuery(document).ready( function() {
jQuery(".wpep-center-section").each(function () {
let width = 0;
jQuery(this).find(" > *").each(function () {
width = width < parseInt(jQuery(this).width()) ? parseInt(jQuery(this).width()) : width;
});
if (width <= 0)
width = jQuery(this).innerWidth() + 10;
jQuery(this).css("display", "block").css("margin-right", "auto").css("margin-left", "auto").css("width", width);
});
});
</script>';
});
@robertuniqid
Copy link
Author

There's nothing "wrong" with this code, in the right circumstances it will work properly, but not always.
This is a hackish-style feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment