Last active
February 6, 2021 03:10
-
-
Save webmandesign/011b8ecbf6cb299acb93d574b52c9f17 to your computer and use it in GitHub Desktop.
Wrapping post content in section divs similar to Medium.com. Section split occurs on Gutenberg wide and full aligned blocks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Creates section divs in the content area. | |
| * | |
| * This is a front-end solution only. | |
| * Splits content into section containers, similar to Medium.com. | |
| * Section split occurs on Gutenberg wide and full aligned blocks. | |
| * | |
| * Copy this function into your WordPress theme's `functions.php` file | |
| * and change the `themeprefix` accordingly. | |
| * | |
| * @link https://codepen.io/webmandesign/post/gutenberg-full-width-alignment-in-wordpress-themes | |
| * | |
| * @param string $content Post content. | |
| */ | |
| function themeprefix_content_sections( $content = '' ) { | |
| // Creating the initial section first. | |
| $content = '<div class="content-section">' . $content . '</div>'; | |
| // Requirements check | |
| if ( ! is_callable( 'has_blocks' ) || ! has_blocks( $content ) ) { | |
| return $content; | |
| } | |
| // Vars | |
| $current_block = ''; | |
| $content_lines = explode( PHP_EOL, $content ); | |
| $alignments = (array) apply_filters( 'themeprefix_content_sections/alignments', array( 'full', 'wide' ) ); | |
| // Processing | |
| foreach ( $content_lines as $line_number => $line_content ) { | |
| if ( false === strpos( $line_content, '<!-- wp:' ) && false === strpos( $line_content, '<!-- /wp:' ) ) { | |
| // No block definition HTML comment? Do not process this line. | |
| continue; | |
| } | |
| // Allow bypassing the line with a filter hook. | |
| $line_content_pre = apply_filters( 'themeprefix_content_sections/line_content_pre', false, $line_content, $line_number ); | |
| if ( is_string( $line_content_pre ) ) { | |
| $content_lines[ $line_number ] = $line_content_pre; | |
| continue; | |
| } | |
| // Get alignment if set. | |
| preg_match( '/"align":"([^"]*)"/i', $line_content, $alignment ); | |
| if ( isset( $alignment[1] ) ) { | |
| $alignment = $alignment[1]; | |
| } else { | |
| $alignment = ''; | |
| } | |
| if ( in_array( $alignment, $alignments ) ) { | |
| /** | |
| * Found a block with desired alignment definition? | |
| * Do this: | |
| * - get and remember block name/key in $current_block variable, | |
| * - set a wrapper CSS class, | |
| * - open our wrapper div. | |
| */ | |
| preg_match( '/wp:(\S+)/', $line_content, $current_block ); | |
| if ( isset( $current_block[1] ) ) { | |
| $current_block = $current_block[1]; | |
| $class = sprintf( | |
| (string) apply_filters( 'themeprefix_content_sections/class', 'align-wrap align-wrap-%s', $current_block, $alignment ), | |
| sanitize_title( $alignment ) | |
| ); | |
| $line_content = str_replace( | |
| '<!-- wp:', | |
| '</div>' . PHP_EOL . '<div class="' . esc_attr( $class ) . '">' . PHP_EOL . '<!-- wp:', | |
| $line_content | |
| ); | |
| } else { | |
| $current_block = ''; | |
| } | |
| } elseif ( ! empty( $current_block ) && false !== strpos( $line_content, '<!-- /wp:' . $current_block . ' -->' ) ) { | |
| /** | |
| * Have a block name/key saved and found a closing comment for that block? | |
| * Do this: | |
| * - close our wrapper div, | |
| * - reset the $current_block variable. | |
| */ | |
| $line_content = str_replace( | |
| '<!-- /wp:' . $current_block . ' -->', | |
| '<!-- /wp:' . $block . ' -->' . PHP_EOL . '</div>' . PHP_EOL . '<div class="content-section">', | |
| $line_content | |
| ); | |
| $current_block = ''; | |
| } | |
| // Replace original line content with modified one. | |
| $content_lines[ $line_number ] = $line_content; | |
| } | |
| // Putting the content back together. | |
| $content = implode( PHP_EOL, $content_lines ); | |
| // Removing empty section leftovers. | |
| $content = preg_replace( | |
| '/<div class=\"content\-section\">(\s|\n)*<\/div>/', | |
| '', | |
| $content | |
| ); | |
| // Output | |
| return $content; | |
| } // /themeprefix_content_sections | |
| // Hook before the `do_blocks` is hooked! | |
| add_filter( 'the_content', 'themeprefix_content_sections', 8 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Creates section divs in the content area
This is a front-end solution only. Splits content into section containers, similar to Medium.com. Section split occurs on Gutenberg wide and full aligned blocks.
Default functionality
By default the code:
fullandwidealigned blocks only. This can be modified usingthemeprefix_content_sections/alignmentsfilter hook, though.div.content-section.content-section-align-$alignmentdivs, where$alignmentcould be offullorwidevalue. The class can be modified usingthemeprefix_content_sections/classfilter hook.Benefits
This should allow using advantages of both styling options of:
.alignfulland.alignwidestyling,Code example
Basically, produces this HTML:
Which could be styled like so:
How to apply?
Copy this PHP function into your WordPress theme's
functions.phpfile and change thethemeprefixappropriately.Created by Oliver at WebManDesign.eu | Licensed under the terms of GPL