Best practices and sample code snippets that you should be aware of when writing WordPress themes for eCommerce to help you get started writing your own themes

图片[1]-在编写适用于电子商务的WordPress主题时应该注意的最佳实践和示例代码片段,以帮助您开始编写您自己的主题-光子波动网 | WordPress教程、Elementor教程与故障修复
  1. Ensure fast page speed

Fast page loading speed is crucial for e-commerce websites. Therefore, we can make sure that the page loading speed is fast by doing the following:

// Remove the jQuery library version parameter.
function remove_jquery_version_parameter( $src ) {
if ( strpos( $src, 'jquery' ) ) {
$src = remove_query_arg( 'ver', $src );
}
return $src.
}
add_filter( 'script_loader_src', 'remove_jquery_version_parameter' );
add_filter( 'style_loader_src', 'remove_jquery_version_parameter' );

// Remove the emoji script that comes with WordPress.
function disable_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ).
add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
}
add_action( 'init', 'disable_emojis' );
function disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}

// Compressing HTML
function compress_html( $html ) {
$compressed = preg_replace( '/\s{2,}/', ' ', $html );
$compressed = preg_replace( '//', ", $compressed );
return $compressed;
}
function maybe_compress_html() {
if ( ! is_user_logged_in() && ! is_admin() ) {
ob_start( 'compress_html' ).
}
}
add_action( 'init', 'maybe_compress_html' );

  1. Structured data using schema.org

Using schema.org structured data helps search engines understand the content on your website and improves SEO rankings. Below is a sample code snippet for adding product structured data on an e-commerce website:

function add_product_schema() {
if ( is_single() && 'product' == get_post_type() ) {
$product = wc_get_product( get_the_ID() );
$json_ld = array(
'@context' => 'http://schema.org',
'@type' => 'Product',
'name' => $product->get_name(),
'description' => $product->get_description(),
'sku' => $product->get_sku(),
'offers' => array(
'@type' => '
'Offer', 'price' => $product->get_price(), 'priceCurrency' => get_woocommerce_currency(), 'availability' => 'http://schema.org/' . ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ), ), ); echo ''; } } add_action( 'wp_head', 'add_product_schema' );

  1. Optimize page titles and descriptions

Page titles and descriptions are important factors in search engine rankings. In WordPress, we can set the page title and description by using the following code snippet:

function custom_meta_title() { global $post; if ( is_front_page() ) { $title = get_bloginfo( 'name' ); } elseif ( is _home() ) { $title = get_the_title( get_option( 'page_for_posts', true ) ); } elseif ( is_singular() ) { $title = get _the_title( $post->ID ); } elseif ( is_category() ) { $title = single_cat_title( ", false ); } elseif ( is_tag() ) { $title = single_tag_title( ", false ); } elseif ( is_author() ) { $title = get_the_author_meta( 'display_name', $post->post_author ); } elseif ( is_search() ) { $title = 'Search Results for ' . get_search_query(); } else { $title = get_bloginfo( 'name' ); } return $title; } add_filter( 'pre_ get_document_title', 'custom_meta_title' ); }

function custom_meta_description() { global $post; if ( is_singular() ) { $description = get_the_excerpt(); if ( empty( $description ) ) { $description = wp_trim_words( $post->post_content, 30 ); } } elseif ( is_category() || is_tag() ) { $description = term_description(); } elseif ( is_home() ) { $description = get_the_excerpt(); if ( empty( $description ) ) } elseif ( is_home() || is_front_page() ) { $description = get_bloginfo( 'description' ); } else { $description = "; } return $description; } add_filter( 'get_the_excerpt', 'custom_meta_description ' ); add_filter( 'excerpt_more', '__return_false' );

  1. Optimize page URLs

Page URLs are also an important factor in SEO ranking. In WordPress, we can optimize page URLs by following code snippets:

function custom_permalink( $permalink, $post ) { if ( ‘product’ === $post->post_type ) { $permalink = home_url( ‘/products/’ . $post->post_name . ‘/’ ); } return $permalink; } add_filter( ‘post_type_link’, ‘custom_permalink’, 10, 2 );

Above are some of the best practices and sample code snippets that you should be aware of when writing WordPress themes for eCommerce. Of course, this is just the tip of the iceberg and there are many other optimizations that can be applied to eCommerce websites.

function custom_rewrite_rule() { add_rewrite_rule( '^products/([^/]*)/?' , 'index.php?product=$matches[1]', 'top' ); } add_action( 'init ', 'custom_rewrite_rule' );


Contact Us
Can't read the tutorial? Contact us for a free answer! Free help for personal, small business sites!
客服微信
Customer Service
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: [email protected]
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
This article was written by Harry
THE END
If you like it, support it.
kudos0 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments