Is it safe to remove/modify default WordPress classes/templates to implement BEM?

253 Views Asked by At

I am creating a WordPress theme using BEM methodology for public release. To implement BEM methodology, I am removing/modifying some default WordPress templates and classes. For example, to implement BEM methodology in post comment list, I have created custom walker class:

<?php
// Walker class used to create an HTML list of comments.
class BEM_Walker_Comment extends Walker_Comment {

    // Starts the list before the elements are added.
    public function start_lvl( &$output, $depth = 0, $args = array() ) {
        $GLOBALS['comment_depth'] = $depth + 1;
        $output .= '<ol class="comment__children">' . "\n";
    }

    // Ends the list of items after the elements are added.
    public function end_lvl( &$output, $depth = 0, $args = array() ) {
        $GLOBALS['comment_depth'] = $depth + 1;
        $output .= "</ol>\n";
    }

    // Starts the element output.
    public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
        $depth++;
        $GLOBALS['comment_depth'] = $depth;
        $GLOBALS['comment'] = $comment;

        if ( ! empty( $args['callback'] ) ) {
            ob_start();
            call_user_func( $args['callback'], $comment, $args, $depth );
            $output .= ob_get_clean();
            return;
        }

        if ( ( 'pingback' === $comment->comment_type || 'trackback' === $comment->comment_type ) && $args['short_ping'] ) {
            ob_start();
            $this->ping( $comment, $depth, $args );
            $output .= ob_get_clean();
        } else {
            ob_start();
            $this->html5_comment( $comment, $depth, $args );
            $output .= ob_get_clean();
        }
    }

    // Ends the element output, if needed.
    public function end_el( &$output, $comment, $depth = 0, $args = array() ) {
        if ( ! empty( $args['end-callback'] ) ) {
            ob_start();
            call_user_func( $args['end-callback'], $comment, $args, $depth );
            $output .= ob_get_clean();
            return;
        }
        $output .= "</li>\n";
    }

    // Outputs a comment in the HTML5 format.
    protected function html5_comment( $comment, $depth, $args ) {
        ?>
        <li <?php $this->comment_class( $this->has_children ? 'comment__parent' : '', $comment ); ?>>
            <article class="comment__body">
                <footer class="comment__meta">
                    <div class="comment__author">
                        <?php
                        if ( 0 !== $args['avatar_size'] ) {
                            echo get_avatar( $comment, $args['avatar_size'] );
                        }

                        $url     = get_comment_author_url( $comment );
                        $author  = get_comment_author( $comment );
                        if ( empty( $url ) || 'http://' === $url ) {
                            $author_name = $author;
                        } else {
                            $author_name = '<a class="comment__author-url" href="' . esc_url( $url ) . '" rel="external nofollow">' . esc_html( $author ) . '</a>';
                        }
                        printf( '<b class="comment__author-name">%s</b>', $author_name );
                        ?>
                    </div>
                    <div class="comment-metadata">
                        <a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
                            <time datetime="<?php comment_time( 'c' ); ?>">
                                <?php printf( __( '%1$s at %2$s', 'runway' ), get_comment_date( '', $comment ), get_comment_time() ); ?>
                            </time>
                        </a>
                        <?php edit_comment_link( __( 'Edit', 'runway' ), '<span class="comment__edit">', '</span>' ); ?>
                    </div>
                    <?php if ( '0' === $comment->comment_approved ) : ?>
                    <p class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'runway' ); ?></p>
                    <?php endif; ?>
                </footer>
                <div class="comment__content">
                    <?php comment_text(); ?>
                </div>
                <?php
                comment_reply_link( array_merge( $args, array(
                    'add_below' => 'div-comment',
                    'depth'     => $depth,
                    'max_depth' => $args['max_depth'],
                    'before'    => '<div class="comment__reply">',
                    'after'     => '</div>',
                ) ) );
                ?>
            </article>
        <?php
    }

    // Generates semantic classes for each comment element.
    protected function comment_class( $class = '', $comment = null, $post_id = null, $echo = true ) {
        $classes = join( ' ', get_comment_class( $class, $comment, $post_id ) );

        $classes = str_replace( ' byuser',          ' comment--by-user',        $classes );
        $classes = str_replace( ' comment-author-',' comment--author-',     $classes );
        $classes = str_replace( ' bypostauthor',    ' comment--by-post-author', $classes );
        $classes = str_replace( ' odd',         ' comment--odd',            $classes );
        $classes = str_replace( ' alt',         ' comment--alt',            $classes );
        $classes = str_replace( ' even',            ' comment--even',           $classes );
        $classes = str_replace( ' thread-odd',      ' comment--thread-odd',     $classes );
        $classes = str_replace( ' thread-alt',      ' comment--thread-alt',     $classes );
        $classes = str_replace( ' thread-even', ' comment--thread-even',    $classes );
        $classes = str_replace( ' depth-',          ' comment--depth-',         $classes );

        // Separates classes with a single space, collates classes for comment DIV.
        $class = 'class="' . $classes . '"';
        if ( $echo ) {
            echo $class;
        } else {
            return $class;
        }
    }
} // BEM_Walker_Comment class
?>

Similarly, I have also created walker class for Navigation menu, and modified comment form to implement BEM methodology.

But is it safe to remove/modify default WordPress classes and templates?

1

There are 1 best solutions below

3
On

But is it safe to remove/modify default WordPress classes and templates?

Yes!

I use BEM for my WordPress templates and I remove all the original CSS classes from WordPress. The only things we need to keep are:

  1. id="s" for the search field;
  2. Several elements in the comments, including the id="com-{commentId}" on comment items, it is used by a JS helper from WordPress to move the "reply" field.