42 Extremely Useful Tricks for the WordPress Functions File

0
36

Are you questioning what you are able to do with the WordPress features file?

All WordPress themes include a features.php file. This file acts as a plugin, permitting theme builders and normal customers so as to add {custom} code in WordPress simply.

In this text, we’ll present you some helpful tips for the WordPress features file.

Handy WordPress functions file tips and hacks

What Is the Functions File in WordPress?

The functions.php file is a WordPress theme file that comes with all free and premium WordPress themes.

It acts as a plugin and permits theme builders to outline theme options. Users may use it so as to add their {custom} code snippets in WordPress.

However, maintaining {custom} code in your theme’s features file is just not the finest approach to save your customizations. If you replace your theme, then the features.php file might be overwritten, and you’ll lose your {custom} code snippets.

Instead, we suggest everybody use WPCode, a free plugin that allows you to insert code snippets into your WordPress web site with out enhancing any theme, plugin, or core WordPress information.

The better part is that each one your {custom} code is saved individually, so any WordPress updates gained’t take away them.

As a bonus, the WPCode plugin has an intensive library of pre-configured code snippets (together with many on this checklist). You can deploy these code snippets with a couple of clicks.

wpcode library

Having stated that, here’s a checklist of things we’ll cowl on this article. You can soar to at least one that pursuits you or just comply with alongside:

How to Add These Code Snippets to Your Website

Before we start, let’s have a look at the best way to add the code snippets on this article to your WordPress features file.

1. Add Custom Code to Functions File Using WPCode (Recommended)

First, you must set up and activate the WPCode plugin. For extra particulars, see our step-by-step information on how to install a WordPress plugin.

Upon activation, go to Code Snippets » + Add Snippet web page. You’ll see WPCode’s code library with many useful {custom} code snippets already added.

Add snippet

If your code snippet does the identical factor as of the snippets in the library, then you possibly can check out the one already added there.

Alternatively, click on the ‘blank snippet’ hyperlink to proceed including your {custom} code snippet.

On the subsequent display, present a title for your {custom} code. This could possibly be something that helps you determine what this code snippet does.

Adding your custom code

Next, you must select the ‘Code Type’. If you might be including a code that works in the features.php file, then you should choose ‘PHP Snippet’.

Below that, you must copy and paste your {custom} code into the ‘Code Preview’ field.

Finally, you must set your snippet as ‘Active’ and click on the ‘Save Snippet’ button.

Activate and save

Your saved snippet will now run like it might if you happen to had added it to the features.php file.

You can repeat the course of so as to add extra snippets when wanted. You may deactivate a snippet with out deleting it.

2. Add Custom Code Directly to the Functions File

The WPCode technique is at all times higher than including code to the theme’s features file.

However, some customers could also be writing code for a shopper’s {custom} WordPress theme or just favor so as to add code to the features.php file.

In that case, right here is how one can add code to your WordPress theme’s features.php file.

First, hook up with your WordPress web site using an FTP client. Once related, navigate to the /wp-content/themes/your-wordpress-theme/ folder.

Edit functions.php file

There you will see that the features.php file. Simply right-click and choose to edit or obtain the file to your laptop for enhancing.

You can edit it utilizing any plain textual content editor like Notepad or TextEdit.

Then, scroll all the way down to the backside of the features.php file and paste your code snippet there. You can save your modifications and add the up to date features.php file to your theme folder.

You can now go to your WordPress website to see your {custom} code in motion.

Now, let’s check out 42 totally different helpful tips for the WordPress features file.

1. Remove WordPress Version Number

You ought to always use the latest version of WordPress. However, chances are you’ll wish to take away the WordPress model quantity out of your web site.

Simply add this code snippet to your features file or as a brand new WPCode snippet:

perform wpb_remove_version() {
return '';
}
add_filter('the_generator', 'wpb_remove_version');

For detailed directions, see our information on the right way to remove the WordPress version number.

Want to white-label your WordPress admin space? Adding a {custom} dashboard brand is the first step in the course of.

First, you’ll have to add your {custom} brand to your theme’s photos folder as custom-logo.png. Your {custom} brand needs to be in a 1:1 ratio (a sq. picture) in 16×16 pixels.

After that, you possibly can add this code to your theme’s features file or as a brand new WPCode snippet:

perform wpb_custom_logo() {
echo '
<type kind="textual content/css">
#wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:earlier than {
background-image: url(' . get_bloginfo('stylesheet_directory') . '/photos/custom-logo.png) !vital;
background-position: 0 0;
colour:rgba(0, 0, 0, 0);
}
#wpadminbar #wp-admin-bar-wp-logo.hover > .ab-item .ab-icon {
background-position: 0 0;
}
</type>
';
}
//hook into the administrative header output
add_action('wp_before_admin_bar_render', 'wpb_custom_logo');

For extra particulars, see our information on how to add a custom dashboard logo in WordPress.

The footer in the WordPress admin space exhibits the message ‘Thank you for creating with WordPress.’ You can change it to something you need by including this code:

perform remove_footer_admin ()  WordPress Tutorials: <a href="https://www.wpbeginner.com" goal="_blank">WPBeginner</a></p>';



add_filter('admin_footer_text', 'remove_footer_admin');

Feel free to alter the textual content and hyperlinks that you just wish to add. Here is the way it seems on our take a look at web site.

Admin footer

4. Add Custom Dashboard Widgets in WordPress

You in all probability have seen the widgets that many plugins and themes add to the WordPress dashboard. You can add one your self by pasting the following code:

add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');

perform my_custom_dashboard_widgets() {
world $wp_meta_boxes;

wp_add_dashboard_widget('custom_help_widget', 'Theme Support', 'custom_dashboard_help');
}

perform custom_dashboard_help() {
echo '<p>Welcome to Custom Blog Theme! Need assist? Contact the developer <a href="mailto:[email protected]">right here</a>. For WordPress Tutorials go to: <a href="https://www.wpbeginner.com" goal="_blank">WPBeginner</a></p>';
}

This is what it might appear like:

Custom dashboard widget

For particulars, see our tutorial on how to add custom dashboard widgets in WordPress.

5. Change the Default Gravatar in WordPress

Have you seen the default thriller man avatar on blogs? You can simply exchange it with your individual branded {custom} avatar.

Simply add the picture you wish to use as the default avatar and add this code to your features file or the WPCode plugin:

perform wpb_custom_default_gravatar( $avatar_defaults ) {
	$myavatar="https://instance.com/wp-content/uploads/2022/10/dummygravatar.png";
	$avatar_defaults[$myavatar] = 'Default Gravatar';
	return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'wpb_custom_default_gravatar' );

Now you possibly can head to the Settings » Discussion web page and choose your default avatar.

Custom default gravatar

For detailed directions, see our information on changing the default gravatar in WordPress.

6. Dynamic Copyright Date in WordPress Footer

You can merely add a copyright date by enhancing the footer template in your theme. However, it is not going to present when your web site began, and it’ll not mechanically change the following 12 months.

This code can add a dynamic copyright date in the WordPress footer:

perform wpb_copyright() {
world $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status="publish"
");
$output="";
if($copyright_dates) {
$copyright = "© " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}

After including this perform, you’ll have to open your footer.php file and add the following code the place you want to show the dynamic copyright date:

<?php echo wpb_copyright(); ?>

This perform seems for the date of your first put up and the date of your final put up. It then returns the years wherever you name the perform.

Tip: If you might be utilizing the WPCode plugin, then you possibly can mix the two code snippets. After that, select the ‘Site Wide Footer’ location in the ‘Insertion’ part of the snippet settings. This will mechanically show the copyright date in the footer with out enhancing your theme’s footer.php file.

Add to footer using WPCode

For extra particulars, see our information on the best way to add dynamic copyright dates in WordPress.

7. Randomly Change the Background Color in WordPress

Do you wish to randomly change the background colour in your WordPress blog for every go to and web page reload? Here is the best way to simply do that.

First, add this code to your theme’s features file or the WPCode plugin:

perform wpb_bg() {
$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$colour="#".$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].
$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];

echo $colour; 
} 

Next, you’ll have to edit the header.php file in your theme. Find the <physique> tag and add exchange it with this line:

<physique <?php body_class(); ?> type="background-color:<?php wpb_bg();?>">

You can now save your modifications and go to your web site to see this code in motion.

Random background colors

For extra particulars and alternate strategies, see our tutorial on randomly changing the background color in WordPress.

8. Update WordPress URLs

If your WordPress login page keeps refreshing otherwise you can’t entry the admin space, then you must replace WordPress URLs.

One approach to do that is through the use of the wp-config.php file. However, if you happen to do this, you then can’t set the right tackle on the settings web page. The WordPress URL and Site URL fields might be locked and uneditable.

Instead, simply add this code to your features file to repair this:

update_option( 'siteurl', 'https://instance.com' );
update_option( 'dwelling', 'https://instance.com' );

Don’t overlook to interchange instance.com along with your domain name.

Once logged in, you possibly can go to the Settings web page in the WordPress admin space and set the URLs.

After that, it’s best to take away the code you added to the features file or WPCode. Otherwise, it can preserve updating these URLs every time your web site is accessed.

9. Add Additional Image Sizes in WordPress

WordPress mechanically generates a number of picture sizes whenever you add a picture. You may create further picture sizes to make use of in your theme.

Simply add this code to your theme’s features file or as a WPCode snippet:

add_image_size( 'sidebar-thumb', 120, 120, true ); // Hard Crop Mode
add_image_size( 'homepage-thumb', 220, 180 ); // Soft Crop Mode
add_image_size( 'singlepost-thumb', 590, 9999 ); // Unlimited Height Mode

This code creates three new picture sizes of various sizes. Feel free to tweak the code to fulfill your necessities.

You can then show a picture dimension wherever in your theme utilizing this code:

<?php the_post_thumbnail( 'homepage-thumb' ); ?>

For detailed directions, see our information on creating additional image sizes in WordPress.

WordPress permits theme builders to outline navigation menus after which show them.

You can add this code to your theme’s features file or as a brand new WPCode snippet to outline a brand new menu location in your theme:

perform wpb_custom_new_menu() {
  register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
}
add_action( 'init', 'wpb_custom_new_menu' );

You can now go to Appearance » Menus in your WordPress dashboard and see ‘My Custom Menu’ as the theme location choice.

Custom menu location

Note: This code will even work with block themes with the full site editing function. Adding it can allow the Menus display below Appearance.

Now you must add this code to your theme the place you wish to show the navigation menu:

 <?php
wp_nav_menu( array( 
    'theme_location' => 'my-custom-menu', 
    'container_class' => 'custom-menu-class' ) ); 
?>

For detailed directions, see our information on the best way to add custom navigation menus in WordPress themes.

11. Add Author Profile Fields

Do you wish to add further fields to your creator profiles in WordPress? You can simply do this by including this code to your features file or as a brand new WPCode snippet:

perform wpb_new_contactmethods( $contactmethods ) {
// Add Twitter
$contactmethods['twitter'] = 'Twitter';
//add Facebook
$contactmethods['facebook'] = 'Facebook';

return $contactmethods;
}
add_filter('user_contactmethods','wpb_new_contactmethods',10,1);

This code will add Twitter and Facebook fields to consumer profiles in WordPress.

New profile fields

You can now show these fields in your creator template like this:

<?php echo get_the_author_meta('twitter') ?>

You may wish to see our information on including additional user profile fields in WordPress registration.

12. Adding Widget-Ready Areas or Sidebars in WordPress Themes

This is one in all the most used code snippets, and plenty of builders already find out about including widget-ready areas or sidebars to WordPress themes. But it deserves to be on this checklist for these individuals who don’t know.

You can paste the following code in your features.php file or as a brand new WPCode snippet:

// Register Sidebars
perform custom_sidebars() {

	$args = array(
		'id'            => 'custom_sidebar',
		'identify'          => __( 'Custom Widget Area', 'text_domain' ),
		'description'   => __( 'A {custom} widget space', 'text_domain' ),
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
		'before_widget' => '<apart id="%1$s" class="widget %2$s">',
		'after_widget'  => '</apart>',
	);
	register_sidebar( $args );

}
add_action( 'widgets_init', 'custom_sidebars' );

Note: This code will even work with block themes with the full web site enhancing function. Adding it can allow the Widgets display below Appearance.

You can now go to the Appearance » Widgets web page and see your new {custom} widget space.

Custom widget area

To show this sidebar or widget-ready space in your web site, you’ll want so as to add the following code in the template the place you wish to show it:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('custom_sidebar') ) : ?>
<!–Default sidebar information goes right here–>
<?php endif; ?>

For extra particulars, see our information on the best way to add dynamic widget-ready areas and sidebars in WordPress.

Have you seen blogs that add their commercials of their RSS feeds beneath every put up? You can accomplish this simply with a easy perform. Just paste the following code:

perform wpbeginner_postrss($content material) {
if(is_feed()){
$content material="This put up was written by Syed Balkhi ".$content material.'Check out WPBeginner';
}
return $content material;
}
add_filter('the_excerpt_rss', 'wpbeginner_postrss');
add_filter('the_content', 'wpbeginner_postrss');

For extra info, see our information on the best way to add content and completely manipulate your RSS feeds.

The put up thumbnail or featured photos are often solely displayed inside your web site design. You can simply prolong that performance to your RSS feed with the following code:

perform rss_post_thumbnail($content material) {
world $put up;
if(has_post_thumbnail($post->ID)) {
$content material="<p>" . get_the_post_thumbnail($post->ID) .
'</p>' . get_the_content();
}
return $content material;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');

For extra particulars, see our information on the best way to add post thumbnails to your WordPress RSS feed.

15. Hide Login Errors in WordPress

Hackers can use login errors to guess whether or not they entered the unsuitable username or password. By hiding login errors in WordPress, you can also make your login space and WordPress website more secure.

Simply add the following code to your theme’s features file or as a brand new WPCode snippet:

perform no_wordpress_errors(){
  return 'Something is unsuitable!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );

Now, customers will see a generic message once they enter an incorrect username or password.

Custom login errors

For extra info, see our tutorial on disabling login hints in WordPress error messages.

16. Disable Login by Email in WordPress

WordPress permits customers to log in with their username or email address. You can simply disable login by electronic mail in WordPress by including this code to your features file or as a brand new WPCode snippet:

remove_filter( 'authenticate', 'wp_authenticate_email_password', 20 );

For extra info, see our information on the best way to disable login by email feature in WordPress.

17. Disable Search Feature in WordPress

If you wish to disable your WordPress web site’s search function, merely add this code to your features file or in a brand new WPCode snippet:

perform wpb_filter_query( $question, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->question[s] = false;
if ( $error == true )
$query->is_404 = true;
}}

This code merely disables the search question by modifying it and returning a 404 error as an alternative of search outcomes.

For extra info, see our tutorial on disabling the WordPress search feature.

Pro Tip: Instead of giving up on WordPress search, we suggest attempting out SearchWP. It is the best WordPress search plugin on the market that means that you can add a robust and customizable search function to your web site.

Sometimes chances are you’ll publish an article with a grammatical error or spelling mistake.

The mistake goes stay and is distributed to your RSS feed subscribers. If you’ve got electronic mail subscriptions in your WordPress weblog, then these subscribers will even get a notification.

Simply add this code to your theme’s features file or as a brand new WPCode snippet to delay posts in your RSS feed:

perform publish_later_on_feed($the place) {

	world $wpdb;

	if ( is_feed() ) {
		// timestamp in WP-format
		$now = gmdate('Y-m-d H:i:s');

		// worth for wait; + gadget
		$wait="10"; // integer

		// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
		$gadget="MINUTE"; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR

		// add SQL-sytax to default $the place
		$the place .= " AND TIMESTAMPDIFF($gadget, $wpdb->posts.post_date_gmt, '$now') > $wait ";
	}
	return $the place;
}

add_filter('posts_where', 'publish_later_on_feed');

In this code, we used 10 minutes as $wait or delay time. Feel free to alter this to any variety of minutes you need.

For a plugin technique and extra info, see our detailed information on the best way to delay posts from appearing in the WordPress RSS feed.

19. Change Read More Text for Excerpts in WordPress

Do you wish to change the textual content that seems after the excerpt in your posts? Simply add this code to your theme’s features file or as a brand new WPCode snippet:

perform modify_read_more_link() {
    return '<a category="more-link" href="' . get_permalink() . '">Your Read More Link Text</a>';
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );

Not all web sites want RSS feeds. If you wish to disable RSS feeds in your WordPress web site, then add this code to your theme’s features file or as a brand new WPCode snippet:

perform new_excerpt_more($extra) {
 world $put up;
 return '<a category="moretag" 
 href="'. get_permalink($post->ID) . '">Your Read More Link Text</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

For a plugin technique and extra info, see our information on the best way to disable RSS feeds in WordPress.

21. Change Excerpt Length in WordPress

WordPress limits excerpt lengths to 55 phrases. You can add this code to your features file or as a brand new WPCode snippet if you must change that:

perform new_excerpt_length($size) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');

Just change 100 to the variety of phrases you wish to present in the excerpts.

For alternate strategies, chances are you’ll wish to have a look at our information on how to customize WordPress excerpts (no coding required).

22. Add an Admin User in WordPress

If you’ve got forgotten your WordPress password and electronic mail, then you possibly can add an admin consumer by including this code to your theme’s features file utilizing an FTP client:

perform wpb_admin_account(){
$consumer="Username";
$go="Password";
$electronic mail="[email protected]";
if ( !username_exists( $consumer )  && !email_exists( $electronic mail ) ) {
$user_id = wp_create_user( $consumer, $go, $electronic mail );
$consumer = new WP_User( $user_id );
$user->set_role( 'administrator' );
} }
add_action('init','wpb_admin_account');

Don’t overlook to fill in the username, password, and electronic mail fields.

Important: Once you log in to your WordPress web site, don’t overlook to delete the code out of your features file.

For extra on this subject, check out our tutorial on how to add an admin user in WordPress using FTP.

23. Disable Language Switcher on Login Page

If you run a multilingual website, then WordPress shows a language selector on the login web page. You can simply disable it by including the following code to your features.php file or as a brand new WPCode snippet:

add_filter( 'login_display_language_dropdown', '__return_false' );

24. Show the Total Number of Registered Users in WordPress

Do you wish to present the complete variety of registered customers in your WordPress web site? Simply add this code to your theme’s features file or as a brand new WPCode snippet:

perform wpb_user_count() {
$usercount = count_users();
$consequence = $usercount['total_users'];
return $consequence;
}
// Creating a shortcode to show consumer rely
add_shortcode('user_count', 'wpb_user_count');

This code creates a shortcode that means that you can show the complete variety of registered customers in your web site.

Now you simply want so as to add the shortcode [user_count] to your put up or web page the place you wish to present the complete variety of customers.

For extra info and a plugin technique, see our tutorial on the best way to display the total number of registered users in WordPress.

Do you wish to exclude particular classes out of your WordPress RSS feed? You can add this code to your theme’s features file or as a brand new WPCode snippet:

perform exclude_category($question) {
	if ( $query->is_feed ) {
		$query->set('cat', '-5, -2, -3');
	}
return $question;
}
add_filter('pre_get_posts', 'exclude_category');

26. Disable URL Links in WordPress Comments

By default, WordPress converts a URL right into a clickable hyperlink in feedback.

You can cease this by including the following code to your features file or as a brand new WPCode snippet:

remove_filter( 'comment_text', 'make_clickable', 9 );

For particulars, see our article on the best way to disable autolinking in WordPress comments.

27. Add Odd and Even CSS Classes to WordPress Posts

You could have seen WordPress themes utilizing an odd and even class for WordPress feedback. It helps customers visualize the place one remark ends and the subsequent one begins.

You can use the identical approach for your WordPress posts. It seems aesthetically pleasing and helps customers rapidly scan pages with a lot of content material.

Simply add this code to your theme’s features file:

perform oddeven_post_class ( $courses ) {
   world $current_class;
   $courses[] = $current_class;
   $current_class = ($current_class == 'odd') ? 'even' : 'odd';
   return $courses;
}
add_filter ( 'post_class' , 'oddeven_post_class' );
world $current_class;
$current_class="odd";

This code merely provides an odd and even class to WordPress posts. You can now add custom CSS to type them otherwise.

Here is a few pattern code that can assist you get began:

.even {
background:#f0f8ff;
}
.odd {
 background:#f4f4fb;
}

The finish consequence will look one thing like this:

Alternating background colors

Need extra detailed directions? Take a have a look at our tutorial on how to add odd/even classes to your posts in WordPress themes.

28. Add Additional File Types to Be Uploaded in WordPress

By default, WordPress means that you can add a restricted variety of the mostly used file sorts. However, you possibly can prolong it to permit different file sorts.

Just add this code to your theme’s features file:

perform my_myme_types($mime_types){
    $mime_types['svg'] = 'picture/svg+xml'; //Adding svg extension
    $mime_types['psd'] = 'picture/vnd.adobe.photoshop'; //Adding photoshop information
    return $mime_types;
}
add_filter('upload_mimes', 'my_myme_types', 1, 1);

This code means that you can add SVG and PSD information to WordPress.

You might want to discover the mime sorts for the file sorts you wish to enable after which use them in the code.

For extra on this subject, take a look at our tutorial on the best way to add additional file types to be uploaded in WordPress.

WordPress makes use of a non-existent electronic mail tackle ([email protected]) to ship outgoing emails by default.

This electronic mail tackle could possibly be flagged as spam by electronic mail service suppliers.

Using the WP Mail SMTP plugin is the correct approach to repair this.

WP Mail SMTP

It fixes electronic mail deliverability points and means that you can select an precise electronic mail tackle to ship your WordPress emails.

To be taught extra, see our information on how to fix WordPress not sending email issue.

On the different hand, if you wish to rapidly change this to an actual electronic mail tackle, then you possibly can add the following code in your features file or as a brand new WPCode snippet:

// Function to alter electronic mail tackle
perform wpb_sender_email( $original_email_address ) {
    return '[email protected]';
}
 
// Function to alter sender identify
perform wpb_sender_name( $original_email_from ) {
    return 'Tim Smith';
}
 
// Hooking up our features to WordPress filters 
add_filter( 'wp_mail_from', 'wpb_sender_email' );
add_filter( 'wp_mail_from_name', 'wpb_sender_name' );

Don’t overlook to interchange the electronic mail tackle and identify with your individual info.

The drawback with this technique is that WordPress remains to be utilizing the mail() perform to ship emails, and such emails are most certainly to finish up in spam.

For higher alternate options, see our tutorial on how to change the sender name in outgoing WordPress emails.

30. Add an Author Info Box in WordPress Posts

If you run a multi-author site and wish to showcase creator bios at the finish of your posts, then you possibly can do this technique.

Start by including this code to your features file or as a brand new WPCode snippet:

perform wpb_author_info_box( $content material ) {

world $put up;

// Detect if it's a single put up with a put up creator
if ( is_single() && isset( $post->post_author ) ) {

// Get creator's show identify
$display_name = get_the_author_meta( 'display_name', $post->post_author );

// If show identify is just not out there then use nickname as show identify
if ( empty( $display_name ) )
$display_name = get_the_author_meta( 'nickname', $post->post_author );

// Get creator's biographical info or description
$user_description = get_the_author_meta( 'user_description', $post->post_author );

// Get creator's web site URL
$user_website = get_the_author_meta('url', $post->post_author);

// Get hyperlink to the creator archive web page
$user_posts = get_author_posts_url( get_the_author_meta( 'ID' , $post->post_author));
	
// Get User Gravatar
$user_gravatar =  get_avatar( get_the_author_meta( 'ID' , $post->post_author) , 90 );

if ( ! empty( $display_name ) )

$author_details="<p class="author_name">About " . $display_name . '</p>';

if ( ! empty( $user_description ) )
// Author avatar and bio might be displayed if creator has stuffed in description. 

$author_details .= '<p class="author_details">' . $user_gravatar . nl2br( $user_description ). '</p>';

$author_details .= '<p class="author_links"><a href="'. $user_posts .'">View all posts by ' . $display_name . '</a>';  

// Check if creator has a web site of their profile
if ( ! empty( $user_website ) )  <a href="' . $user_website .'" goal="_blank" rel="nofollow noopener">Website</a></p>';

 else {
// if there is no such thing as a creator web site then simply shut the paragraph
$author_details .= '</p>';
}

// Pass all this information to put up content material
$content material = $content material . '<footer class="author_bio_section" >' . $author_details . '</footer>';
}
return $content material;
}

// Add our perform to the put up content material filter
add_action( 'the_content', 'wpb_author_info_box' );

// Allow HTML in creator bio part
remove_filter('pre_user_description', 'wp_filter_kses');

Next, you will want so as to add some custom CSS to make it look higher.

You can use this pattern CSS as a place to begin:

.author_bio_section{
background: none repeat scroll 0 0 #F5F5F5;
padding: 15px;
border: 1px stable #ccc;
}

.author_name{
font-size:16px;
font-weight: daring;
}

.author_details img {
border: 1px stable #D8D8D8;
border-radius: 50%;
float: left;
margin: 0 10px 10px 0;
}

This is how your creator field will appear like:

Author bio box

For a plugin technique and extra detailed directions, take a look at our article on how to add an author info box in WordPress posts.

31. Disable XML-RPC in WordPress

XML-RPC is a technique that permits third-party apps to speak along with your WordPress web site remotely. This may trigger safety points and may be exploited by hackers.

To flip off XML-RPC in WordPress, add the following code to your features file or as a brand new WPCode snippet:

add_filter('xmlrpc_enabled', '__return_false');

You could wish to learn our article on how to disable XML-RPC in WordPress for extra info.

32. Automatically Link Featured Images to Posts

If your WordPress theme doesn’t mechanically hyperlink featured photos to full articles, then you possibly can do this technique.

Simply add this code to your theme’s features file or as a brand new WPCode snippet:

perform wpb_autolink_featured_images( $html, $post_id, $post_image_id ) {

If (! is_singular()) { 

$html="<a href="" . get_permalink( $post_id ) . '" title="' . esc_attr( get_the_title( $post_id ) ) . '">' . $html . '</a>';
return $html;

} else { 

return $html;

}

}
add_filter( 'post_thumbnail_html', 'wpb_autolink_featured_images', 10, 3 );

You could wish to learn our article on how to automatically link featured images to posts in WordPress.

33. Disable Block Editor in WordPress

WordPress makes use of a contemporary and intuitive editor for writing content material and enhancing your web site. This editor makes use of blocks for commonly-used content material and format parts, which is why it’s referred to as the Block Editor.

However, chances are you’ll want to make use of the older Classic Editor in some use instances.

The best approach to disable the block editor is through the use of the Classic Editor plugin. However, if you happen to don’t wish to use a separate plugin, then simply add the following code to your features file or as a brand new WPCode snippet:

add_filter('gutenberg_can_edit_post', '__return_false', 5);
add_filter('use_block_editor_for_post', '__return_false', 5);

For extra particulars, see our tutorial on the best way to disable the Block Editor and use the Classic Editor.

34. Disable Block Widgets in WordPress

WordPress switched from traditional widgets to dam widgets in WordPress 5.8. The new block widgets are simpler to make use of and offer you extra design management than traditional widgets.

However, some customers should wish to use traditional widgets. In that case, you should use the following code in your theme’s features file or as a brand new WPCode snippet:

add_filter( 'use_widgets_block_editor', '__return_false' );

For extra particulars, see our article on the best way to disable widget blocks (restore classic widgets).

35. Display the Last Updated Date in WordPress

When guests view a put up or web page in your WordPress weblog, your WordPress theme will present the date the put up was revealed. This is okay for most blogs and static web sites.

However, WordPress can also be utilized by web sites the place outdated articles are recurrently up to date. In these publications, displaying the date and time the put up was final modified is important.

Last updated date

You can present the final up to date date utilizing the following code in your theme’s features file or as a brand new WPCode snippet:

$u_time          = get_the_time( 'U' );
$u_modified_time = get_the_modified_time( 'U' );
// Only show modified date if 24hrs have handed since the put up was revealed.
if ( $u_modified_time >= $u_time + 86400 ) {

	$updated_date = get_the_modified_time( 'F jS, Y' );
	$updated_time = get_the_modified_time( 'h:i a' );

	$up to date = '<p class="last-updated">';

	$up to date .= sprintf(
	// Translators: Placeholders get changed with the date and time when the put up was modified.
		esc_html__( 'Last up to date on %1$s at %2$s' ),
		$updated_date,
		$updated_time
	);
	$up to date .= '</p>';

	echo wp_kses_post( $up to date );
}

For alternate strategies and extra particulars, see our information on how to display the last updated date in WordPress.

36. Use Lowercase Filenames for Uploads

If you run a multi-author web site, then authors could add photos with filenames in higher and lowercase.

Adding the following code ensures that each one filenames are in lowercase:

add_filter( 'sanitize_file_name', 'mb_strtolower' );

Note: The code is not going to change filenames for present uploads. For alternate strategies, see our tutorial on how to rename images and media files in WordPress.

37. Disable WordPress Admin Bar on Frontend

By default, WordPress shows the admin bar at the high when a logged-in consumer views your web site.

You can disable the admin bar for all customers besides web site directors. Simply add the following code to your features file or as a brand new WPCode snippet:

/* Disable WordPress Admin Bar for all customers */
add_filter( 'show_admin_bar', '__return_false' );

For extra particulars, see our information on the best way to disable the WordPress admin bar for all users except administrators.

38. Change Howdy Admin Text in Admin Area

WordPress shows a ‘Howdy Admin’ greeting in the WordPress dashboard. ‘Admin’ is changed by the logged-in consumer’s identify.

Howdy greeting

You can change the default greeting to your individual by including the following code in your features file or as a brand new WPCode snippet:

perform wpcode_snippet_replace_howdy( $wp_admin_bar ) {

	// Edit the line beneath to set what you need the admin bar to show intead of "Howdy,".
	$new_howdy = 'Welcome,';

	$my_account = $wp_admin_bar->get_node( 'my-account' );
	$wp_admin_bar->add_node(
		array(
			'id'    => 'my-account',
			'title' => str_replace( 'Howdy,', $new_howdy, $my_account->title ),
		)
	);
}

add_filter( 'admin_bar_menu', 'wpcode_snippet_replace_howdy', 25 );

For extra particulars, see our article on changing the ‘Howdy Admin’ message in WordPress.

39. Disable Code Editing in Block Editor

The block editor means that you can change to the Code Editor. This turns out to be useful if you must add some HTML code manually.

However, chances are you’ll wish to preserve this function restricted to web site directors.

You can add the following code to your features file or as a WPCode snippet to realize this:

add_filter( 'block_editor_settings_all', perform ( $settings ) {
	
	$settings['codeEditingEnabled'] = current_user_can( 'manage_options' );

	return $settings;
} );

40. Disable Plugin / Theme File Editor

WordPress comes with a built-in editor the place you possibly can edit plugin information. You can see it by going to the Plugins » Plugin File Editor web page.

Plugin file editor in WordPress

Similarly, WordPress additionally features a file editor for traditional themes at Appearance » Theme File Editor.

Note: If you employ a block theme, then the theme file editor is just not seen.

Theme file editor

We don’t suggest utilizing these editors for making modifications to your theme or plugin. A tiny mistake in code could make your web site inaccessible to all customers.

To disable the plugin/theme editor, add the following code to your features file or as a WPCode snippet:

// Disable the Plugin and Theme Editor
if ( ! outlined( 'DISALLOW_FILE_EDIT' ) ) {
	outline( 'DISALLOW_FILE_EDIT', true );
}

For extra particulars, see our tutorial on the best way to disable the plugin/theme editor in WordPress.

41. Disable New User Notification Emails

By default, WordPress sends an electronic mail notification when a brand new consumer joins your WordPress web site.

If you run a WordPress membership website or require customers to signup, then you’re going to get a notification every time a consumer joins your web site.

To flip off these notifications, you possibly can add the following to your features file or as a brand new WPCode snippet:

perform wpcode_send_new_user_notifications( $user_id, $notify = 'consumer' ) {
	if ( empty( $notify ) || 'admin' === $notify ) {
		return;
	} elseif ( 'each' === $notify ) {
		// Send new customers the electronic mail however not the admin.
		$notify = 'consumer';
	}
	wp_send_new_user_notifications( $user_id, $notify );
}

add_action(
	'init',
	perform () {
		// Disable default electronic mail notifications.
		remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
		remove_action( 'edit_user_created_user', 'wp_send_new_user_notifications' );

		// Replace with {custom} perform that solely sends to consumer.
		add_action( 'register_new_user', 'wpcode_send_new_user_notifications' );
		add_action( 'edit_user_created_user', 'wpcode_send_new_user_notifications', 10, 2 );
	}
);

For extra particulars, see our tutorial on the best way to disable new user email notifications in WordPress.

42. Disable Automatic Update Email Notifications

Occasionally, WordPress could mechanically set up safety and upkeep updates or replace a plugin with a crucial vulnerability.

It sends an automated replace electronic mail notification after every replace. If you handle a number of WordPress web sites, then chances are you’ll get a number of such emails.

You can add this code to your features file or as a brand new WPCode snippet to show off these electronic mail notifications:

/ Disable auto-update emails.
add_filter( 'auto_core_update_send_email', '__return_false' );

// Disable auto-update emails for plugins.
add_filter( 'auto_plugin_update_send_email', '__return_false' );

// Disable auto-update emails for themes.
add_filter( 'auto_theme_update_send_email', '__return_false' );

To be taught extra, see our article on the best way to disable automatic update emails in WordPress.

We hope this text helped you be taught some new helpful tips for the features.php file in WordPress. You may wish to see our final information to boost WordPress speed and performance and our skilled picks for the best code editors for Mac and Windows.

If you preferred this text, then please subscribe to our YouTube Channel for WordPress video tutorials. You may discover us on Twitter and Facebook.



Source link