The following snippet takes the first image it finds in the post and sets it as the featured image. If you choose a featured image manually, it will of course display that one.
function autoset_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if (!$already_has_thumb) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
}
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');
For more on WordPress image handling, see Image Sizes in WordPress.
When users enter an incorrect username or password, an error message appears telling them which one is wrong. If someone is trying to break into your site by guessing one of these, that error message can help them understand what they got wrong. Block this by adding the following line to your functions.php file. For more security tips, see hardening your WordPress site:
add_filter('login_errors',create_function('$a', "return null;"));
Some prefer to work with the visual editor when writing posts and others prefer the text editor. Change the default post editor by adding the following lines to your functions.php file:
# Visual Editor as default
add_filter( 'wp_default_editor', create_function('', 'return "tinymce";') );
# HTML Editor as default
add_filter( 'wp_default_editor', create_function('', 'return "html";') );
For more editor configuration, see Enable Gutenberg for Custom Post Types.
You can redirect users who log into your WordPress site to different URLs based on their role.
function redirect_user_on_role()
{
global $current_user;
get_currentuserinfo();
if ($current_user->user_level == 0)
{
wp_redirect( home_url() ); exit;
}
else if ($current_user->user_level > 1)
{
wp_redirect( home_url() ); exit;
}
else if ($current_user->user_level >8)
{
wp_redirect( home_url() ); exit;
}
else
{
$redirect_to = 'http://google.com/';
return $redirect_to;
}
}
add_action('admin_init','redirect_user_on_role');
For more security best practices, see Hardening WordPress Security.
By default WordPress blocks comments on Custom Post Types. To enable comments on a custom post type, add comments under supports where you register it (line 32):
function snippet_custom_init() {
$labels = array(
'name' => 'Snippet',
'singular_name' => 'Snippet',
'add_new' => 'Add New Snippet',
'add_new_item' => 'Add New Snippet',
'edit_item' => 'Edit Snippet',
'new_item' => 'New Snippet',
'all_items' => 'All Snippets',
'view_item' => 'View Snippet',
'search_items' => 'Search Snippets',
'not_found' => 'No snippets found',
'not_found_in_trash' => 'No snippets found in trash',
'parent_item_colon' => '',
'menu_name' => 'Snippets',
);
$args = array(
'labels' => $labels,
'exclude_from_search' => false,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'snippet' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'taxonomies' => array('category'),
'menu_position' => null,
'supports' => array( 'title', 'author', 'thumbnail', 'excerpt', 'comments', 'editor' )
);
register_post_type( 'snippet', $args );
}
add_action( 'init', 'snippet_custom_init', 0);
A trend I have seen on many blogs is to change the order of comments in WordPress posts so the most recently written comments appear at the top. If your blog posts have many comments, this option can be preferable to prevent the latest comments from being “buried” at the bottom of the page.
Here is how to reverse comment order in WordPress.
Option 1
Go to Settings > Discussion. Under “Other comment settings” find “Older comments at the top of each page” and change the setting accordingly.
%22%20transform%3D%22translate(1.4%201.4)%20scale(2.72656)%22%20fill-opacity%3D%22.5%22%3E%3Cellipse%20fill%3D%22%23fff%22%20cx%3D%2225%22%20cy%3D%2215%22%20rx%3D%2248%22%20ry%3D%22116%22%2F%3E%3Cellipse%20fill%3D%22%23fff%22%20cx%3D%2234%22%20cy%3D%2222%22%20rx%3D%2239%22%20ry%3D%22135%22%2F%3E%3Cellipse%20fill%3D%22%23d8d8d8%22%20cx%3D%22195%22%20cy%3D%2219%22%20rx%3D%22122%22%20ry%3D%22122%22%2F%3E%3Cellipse%20fill%3D%22%23fff%22%20cx%3D%2233%22%20cy%3D%2216%22%20rx%3D%2240%22%20ry%3D%2277%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E)
Option 2
Add the following code to your theme’s functions.php file:
function wpb_reverse_comments($comments) {
return array_reverse($comments);
}
add_filter ('comments_array', 'wpb_reverse_comments');
Here we use the comments_array filter to reverse the comment order.
That is it – hope this tip helps when you want to reverse comment order on your WordPress site.
For more comment customization, see Show the Total Number of Comments in WordPress.
This week I tried to update a theme on a WordPress site and ran into the error Fatal Error: Maximum Execution Time Exceeded in WordPress. The fix in this case is simple, but can be frustrating for beginners. Here is how to fix the Maximum Execution Time Exceeded error in WordPress.
%22%20transform%3D%22translate(1%201)%20scale(2.03125)%22%20fill-opacity%3D%22.5%22%3E%3Cellipse%20fill%3D%22%23ababab%22%20cx%3D%22126%22%20cy%3D%2211%22%20rx%3D%22255%22%20ry%3D%2216%22%2F%3E%3Cellipse%20fill%3D%22%23ebebeb%22%20rx%3D%221%22%20ry%3D%221%22%20transform%3D%22matrix(.03806%2029.00747%20-194.91218%20.25571%20132.9%2056)%22%2F%3E%3Cellipse%20fill%3D%22%23c1c1c1%22%20cx%3D%22134%22%20cy%3D%2226%22%20rx%3D%22169%22%20ry%3D%222%22%2F%3E%3Cpath%20fill%3D%22%23e2e2e2%22%20d%3D%22M3%2096l64-69-77%201z%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E)
WordPress is largely built on PHP, and to protect servers there is a limit on how long a PHP script can run. Some hosting providers allow longer execution time and others limit it more strictly. When the script hits the limit, you get this message.
How to fix it?
To change the PHP script execution time limit, connect via FTP to your server, find the .htaccess file in the same directory as wp-content and wp-admin. Edit the file and add:
<IfModule mod_php7.c>
php_value max_execution_time 300
</IfModule>
This sets execution time to 300 seconds (5 minutes); you can change it as needed. For reference, the Avada theme version 3.8 requires at least 180 seconds to work properly.
*Note the PHP version in the code – adjust if needed for your server.
For more wp-config.php tweaks, see Optimize WordPress Configuration via wp-config.php.
On one of the recent WordPress sites I built for a client, I was asked to change the position of the Add to Cart button in WooCommerce. You can do this by hooking into the action woocommerce_single_product_summary.
The action is defined in content-single-product.php:
<?php
/**
* woocommerce_single_product_summary hook
*
* @hooked woocommerce_template_single_title - 5
* @hooked woocommerce_template_single_price - 10
* @hooked woocommerce_template_single_excerpt - 20
* @hooked woocommerce_template_single_add_to_cart - 30
* @hooked woocommerce_template_single_meta - 40
* @hooked woocommerce_template_single_sharing - 50
*/
do_action( 'woocommerce_single_product_summary' );
?>
In the code above you will find a description of the hook parts, with numbers indicating priority/order. You can change the priority/order by removing and re-adding the parts like this:
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 15 );
Add the code above to your functions.php file. Experiment with the priority to change the Add to Cart button position in WooCommerce.
To exclude pages from search results, add the following code to your functions.php file. You can also limit search to a specific Post Type by changing “post” on line 7 to the slug of the Post Type you want to display.
function filter_pages_from_search($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','filter_pages_from_search');
Conversely, to make WordPress search pages as well as posts, use this code:
function filter_pages_from_search($query) {
if ($query->is_search) {
$query->set('post_type', array('post', 'page'));
}
return $query;
}
add_filter('pre_get_posts','filter_pages_from_search');
For more on controlling WordPress search visibility, see Prevent Search Engines from Indexing Search Results.
To create a custom page template as the homepage in WordPress, duplicate page.php from your theme’s main directory or create a new PHP file and add the following code at the top:
<?php /* Template Name: xxxxxx */ ?>
Replace xxxxxx with the name that will appear under “Page Attributes” when you create a new page.
Go to your WordPress admin panel, create a new page and select this template.
%22%20transform%3D%22translate(1.2%201.2)%20scale(2.3711)%22%20fill-opacity%3D%22.5%22%3E%3Cellipse%20fill%3D%22%23cacaca%22%20rx%3D%221%22%20ry%3D%221%22%20transform%3D%22matrix(-17.29584%20-32.61896%2035.40186%20-18.77144%20114.7%20195.7)%22%2F%3E%3Cellipse%20fill%3D%22%23c7c7c7%22%20cx%3D%223%22%20cy%3D%2286%22%20rx%3D%2242%22%20ry%3D%2223%22%2F%3E%3Cellipse%20fill%3D%22%23fff%22%20rx%3D%221%22%20ry%3D%221%22%20transform%3D%22rotate(-110.7%2090.2%20-20.1)%20scale(129.21869%2074.70396)%22%2F%3E%3Cellipse%20fill%3D%22%23fff%22%20cx%3D%2244%22%20cy%3D%22229%22%20rx%3D%2235%22%20ry%3D%22135%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E)
After publishing the page, go to Settings > Reading in the WordPress admin.
%27%20fill-opacity%3D%27.5%27%3E%3Cellipse%20fill%3D%22%23ccc%22%20fill-opacity%3D%22.5%22%20rx%3D%221%22%20ry%3D%221%22%20transform%3D%22matrix(9.35521%2070.11404%20-96.09846%2012.82227%20187.6%20153.7)%22%2F%3E%3Cellipse%20fill%3D%22%23cbcbcb%22%20fill-opacity%3D%22.5%22%20rx%3D%221%22%20ry%3D%221%22%20transform%3D%22rotate(-90.6%20267.6%20-226.3)%20scale(73.00035%20139.14122)%22%2F%3E%3Cpath%20fill%3D%22%23fff%22%20fill-opacity%3D%22.5%22%20d%3D%22M569.5%20238l-321-270.5%2060.9%20344z%22%2F%3E%3Cellipse%20fill%3D%22%23fff%22%20fill-opacity%3D%22.5%22%20rx%3D%221%22%20ry%3D%221%22%20transform%3D%22matrix(-304.47242%20209.18673%20-42.6458%20-62.0712%2072.2%2052.8)%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E)
Select your page as the homepage. That is it – you now have a custom homepage. See the post on WordPress page templates.