You can mark every order as completed automatically by adding the following code to your functions.php file. You can also change Completed to Processing to make every order processing automatically:
/**
* Auto Complete all WooCommerce orders.
*/
function custom_woocommerce_auto_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$order->update_status( 'completed' );
}
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
For more WooCommerce hooks, see Mastering WooCommerce Hooks.
By default, there is an automatic redirect from the WooCommerce checkout page to the (empty) cart page when there are no products in the cart.
Try accessing the checkout page (link in footer) at this sample store and you will see the redirect.
There are uncommon situations where you may want to disable this redirect and show the user the checkout page anyway. You can do this by adding the following code to your functions.php file:
add_filter( 'woocommerce_checkout_redirect_empty_cart', '__return_false' );
add_filter( 'woocommerce_checkout_update_order_review_expired', '__return_false' );
Yoast automatically adds schema according to the settings you configured in the plugin. You can disable or remove this code using one of the following filters (depending on version).
For Yoast versions below 11.0, remove the schema with this filter:
function savvy_remove_yoast_json($data){
$data = array();
return $data;
}
add_filter('wpseo_json_ld_output', 'savvy_remove_yoast_json', 10, 1);
For Yoast versions 11.0 and above, use this filter:
add_filter( 'wpseo_json_ld_output', '__return_false' );
Instead of displaying the exact date your post was published, you can display it relatively – “6 months ago” or “3 weeks ago”. Use the following code to do this:
# For posts & pages #
<?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?>
# For comments #
<?php echo human_time_diff(get_comment_time('U'), current_time('timestamp')) . ' ago'; ?>
For another useful date/time display, see Display Estimated Post Reading Time.
This short snippet lets you display the total number of posts for a specific Custom Post Type. Add the code where you want to display the post count and change POST-TYPE-NAME to the name of the post type whose count you want to show.
<?php
// Get total number of posts in POST-TYPE-NAME
$count_posts = wp_count_posts('POST-TYPE-NAME');
$total_posts = $count_posts->publish;
echo $total_posts;
?>
The following filter lets you set the excerpt length that WordPress displays when calling the the_excerpt function. In this example we set the excerpt length to 20 characters only:
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');
This uses the excerpt_length filter. Learn more in WordPress Hooks Explained.
Custom Post Types (CPTs) are not included in WordPress search results by default. The following snippet lets you choose which custom post types to add to WordPress search results. Add this code to your theme’s functions.php file and change the post type names as needed:
function custom_post_type_search( $query ) {
if ($query->is_search) {
$query->set('post_type', array( 'post', 'myCustomPostType1', 'myCustomPostType2', 'myCustomPostType3'));
}
return $query;
}
add_filter( 'pre_get_posts', 'custom_post_type_search' );
If you want more advanced search options, take a look at the post “Improving search with Relevanssi“.
There is a function called wp_is_mobile that provides a condition to display content only when the user is viewing on mobile. The function returns true when the site is loaded on mobile. Use it like this:
<?php if( wp_is_mobile()){ ?>
// mobile stuff goes here
<?php } else { ?>
// desktop stuff goes here
<?php } ?>
Note: Make sure your caching mechanism caches separately for mobile and desktop. You can do this for example with the WP-Rocket plugin.
To display the site name and description in WordPress templates, use the following functions that come with WordPress:
<?php echo get_bloginfo('name');?>
<?php echo get_bloginfo('description');?>
Say you want to highlight the first post in the loop in some way. You can add a specific class to it and then style it with CSS. Do this in functions.php:
function wps_first_post_class( $classes ) {
global $wp_query;
if( 0 == $wp_query->current_post )
$classes[] = 'first';
return $classes;
}
add_filter( 'post_class', 'wps_first_post_class' );
For more on customizing the WordPress loop, see WordPress Template Hierarchy.