Skip to content

Instantly share code, notes, and snippets.

@khoipro
Last active August 26, 2023 13:11
Show Gist options
  • Save khoipro/5e477bfad75c8739cc97f393aff69909 to your computer and use it in GitHub Desktop.
Save khoipro/5e477bfad75c8739cc97f393aff69909 to your computer and use it in GitHub Desktop.
WP PHP Coding Standards
<?php
/**
* WP PHP Coding Standards
*
* @package CodingStandards
* @author codetot
**/
// To start a new PHP file, append phpDocs
/**
* Block: Accordions
*
* @package ctThemeName
* @author codetot
* @since 0.0.1
*/
// After creating a new function, new class, append phpdocs
// Auto with VS Code using this extension: https://marketplace.visualstudio.com/items?itemName=neilbrayfield.php-docblocker
/**
* Remove reviews tab from WooCommerce
*
* @param array $tabs
* @return array
*/
function theme_prefix_woocommerce_product_tabs( $tabs ) {
unset( $tabs['reviews'] );
return $tabs;
}
/**
* // Function must start with theme_prefix_ or plugin_prefix_
* // Global function must be start with ct_
**/
function theme_prefix_display_related_posts() {}
/**
* // Use _display_ to echo a markup
* // Use _get_ to return any value
**/
function theme_prefix_get_related_posts( $exclude_post_id ) {
return $posts;
}
function theme_prefix_display_related_posts( $exclude_post_id) {
get_template_part('templates/post-grid', null, [
'posts' => $posts
]);
}
/** Try to escape function as soon if not match condition **/
function theme_prefix_display_related_posts() {
if ( ! is_singular('post' ) ) {
return;
}
// Continue
}
// Always define a $variable if possible to avoid slow query or bad performance
$current_post_id = get_the_ID();
$post_title = get_the_title( $current_post_id ); // x3
$post_url = get_permalink( $current_post_id ); // x2
?>
<div class="post-card">
<a class="post-card__image-link" href="<?php echo esc_url_raw( $post_url ); ?>" title="<?php echo esc_attr( sprintf(__('Visit %s', 'text-domain'), $post_title) ); ?>"></a>
<div class="post-card__content">
<h3 class="post-card__title">
<a class="post-card__title-link" href="<?php echo esc_url_raw( $post_url ); ?>" title="<?php echo esc_attr( sprintf(__('Visit %s', 'text-domain'), $post_title) ); ?>"><?php echo esc_html( $post_title ); ?></a>
</h3>
</div>
</div>
<?php
// Always check !empty before looping
if ( ! empty( $items ) ) : ?>
<ul class="social-links__list">
<?php foreach ($items as $item) : ?>
<li class="social-links__item"></li>
<?php endforeach; ?>
</ul>
<?php endif;
// Try to split into multiple component
$social_icons = ['facebook', 'twitter', 'linkedin'];
foreach ($social_icons as $icon) :
// Split to new template path if possible
get_template_part('templates/blocks/social-share-item', null, [
'icon' => $icon
]);
endforeach;
// Extending component for reusable (no more duplicate)
if ( !empty( $posts ) ) :
foreach ($posts as $post_id) :
if ($index === 0) :
get_template_part('templates/blocks/post-card', null, [
'post_id' => $post_id,
'class' => 'post-card--primary' // Same component, only passing other BEM class to override
]);
else :
get_template_part('templates/blocks/post-card', null, [
'post_id' => $post_id,
'class' => 'post-card--row' // Same component, only passing other BEM class to override
]);
endif;
endforeach;
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment