Add additional image sizes in WordPress

WordPress lets you add additional image sizes that you can use in your themes. You can do this using the add_image_size() function.

function add_custom_image_sizes() {
    add_image_size('custom-size', 300, 200, true);
}
add_action('after_setup_theme', 'add_custom_image_sizes');

This function adds an image size named “custom-size” with a width of 300 pixels and a height of 200 pixels. The last parameter (true) allows hard cropping.

Link to official WordPress documentation

Add new navigation menus to your theme

You can add additional menu locations to your WordPress theme using the register_nav_menus() function.

function register_my_menus() {
    register_nav_menus(
        array(
            'header-menu' => __('Header menu'),
            footer-menu' => __('Footer menu')
        )
    );
}
add_action('init', 'register_my_menus');

Link to official WordPress documentation

Edit “Read more” text for excerpts in WordPress

To modify the “Read more” text for excerpts, you can use the excerpt_more filter.

function custom_excerpt_more($more) {
    return '... <a class="read-more" href="'.get_permalink(get_the_ID()).'">Read more</a>';
}
add_filter('excerpt_more', 'custom_excerpt_more');

Link to official WordPress documentation

Changing the length of the excerpt in WordPress

The default length of the WordPress extract is 55 words. You can modify it using the excerpt_length filter.

function custom_excerpt_length($length) {
    return 25;
}
add_filter('excerpt_length', 'custom_excerpt_length');

Link to official WordPress documentation

Add additional file types to upload to WordPress

To allow additional file types to be uploaded, use the upload_mimes filter.

function custom_upload_mimes($mimes) {
    $mimes['svg'] = 'image/svg+xml';
    return $mimes;
}
add_filter('upload_mimes', 'custom_upload_mimes');

Link to official WordPress documentation

The WPCode extension: An ally for managing theme functions

What is WPCode?

WPCode is one of the most popular WordPress plugins for coding snippets. Founded by Syed Balkhi, also founder of popular plugins such as WPForms and WP Mail SMTP, WPCode has over a million active installations. Not only does it speed up your website, it also makes your code manageable, reusable and customizable. By using it, you can say goodbye to at least 6-8 plugins on average!

Advantages of WPCode

  • Better speed & performance: Code snippets are compact and don’t hinder your site’s speed.
  • Improved code quality: With code snippets, you can modify each line to suit your needs.
  • Enhanced security: Code snippets guarantee security against threats.
  • Better user experience: Code snippets, if well coded, do not interfere with other functions.

Disadvantages of WPCode

  • Loss of control: WPCode can sometimes miss an error that can cause a site to crash.
  • WPCode is no ordinary WordPress plugin: If WPCode malfunctions, recovering all your extracts can be a daunting task.

FAQ

What impact does adding custom functions have on the performance of my WordPress site?Adding custom functions can improve performance if they replace heavier plugins. However, it’s essential to ensure that the code added is optimized and error-free.

How can I test the compatibility of these functions with future WordPress updates?

It’s recommended to use a staging environment to test all new features or modifications before deploying them on a production site. This will enable you to check their compatibility with WordPress updates.

Is there a risk of conflict between custom functions and existing plugins?

Yes, there can be conflicts. It is therefore essential to test all new functions in a development environment and always have recent backups of your site.

How can I find out more about good development practices for WordPress?

The official WordPress documentation, as well as numerous dedicated forums and blogs, offer valuable resources for keeping up to date with best practices.

If I’m not comfortable with programming, are there any resources to help me implement these functions?

Yes, the WordPress community is vast. You can hire specialized developers or consult online tutorials for help.

The flexibility offered by WordPress is one of its greatest strengths. The functions discussed in this article show just how simple it is to make concrete, useful changes to your theme without the need for a multitude of plugins. By understanding and using these functions, you’ll be able to further customize your site and respond more precisely to your users’ needs.