Je crée un thème WordPress en utilisant la méthodologie BEM pour la publication. Pour implémenter la méthodologie BEM, je supprime/modifie certains templates et classes WordPress par défaut. Par exemple, pour mettre en œuvre la méthodologie BEM dans la liste post commentaire, j'ai créé la classe marcheur personnalisée:Est-il sûr de supprimer/modifier les classes/modèles WordPress par défaut pour implémenter BEM?
<?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
?>
De même, je l'ai aussi créé la classe Walker pour le menu de navigation, et sous forme de commentaire modifiée pour mettre en œuvre la méthodologie BEM.
Mais est-il sécuritaire de supprimer/modifier les classes et modèles WordPress par défaut?
Tous les plugins fonctionnent correctement? – BHUSHAN
@BHUSHAN Je n'utilise pas beaucoup de plugins. Les plugins que j'utilise ne posent aucun problème. En général, pour insérer du contenu quelque part, les plugins WordPress utilisent des filtres WordPress. – Paleo
Pour clarifier ... il est sûr de supprimer/modifier les classes et modèles WordPress par défaut * dans votre propre thème * - pas directement dans le code WP ou le thème de base (si vous utilisez un thème enfant). Juste pensé que j'ajouterais que dans le cas où un autre utilisateur n'a pas remarqué que l'OP utilise son propre thème et pensait qu'il était sûr de changer les fichiers WP directement :) – FluffyKitten