The 15 Effective Code Snippets for WordPress next level

The 15 Effective Code Snippets for WordPress next level

WordPress gives you the freedom to customize every aspect. But it’s hard to know where to start because there is so much to customize. So, it’ll be great to have some knowledge first and then customize if you’re new. For which, I’ve come with some of our favorite WordPress code snippets to make your site work.

To work, you’ll need to add these first at your site through Code Snippets plugins or theme’s functions.php.

Helpful Code Snippets for WordPress:

1. Admin Toolbar Disable:

You don’t need to keep separate codes for login users vs. public users in some caching systems. In that case, disabling the admin toolbar may work for you.

So, do you want to disable the toolbar from all pages as logged-in users? Use the snippet below at your theme function.php.


//Disable WordPress admin bar for all logged in users
add_filter('show_admin_bar', '__return_false');

WP only shows title and meta description in your RSS feed by default. But the below snippet will do if you desire to display an image.

It’ll add a featured thumbnail of the post before the content to your site’s RSS feed.


//This will prepend your WordPress RSS feed content with the featured image
add_filter('the_content', 'athemeart_featured_image_in_rss_feed');
function athemeart_featured_image_in_rss_feed( $content ) {
  global $post;
  if( is_feed() ) {
    if ( has_post_thumbnail( $post->ID ) ){
      $prepend = '

' . get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'margin-bottom: 10px;' ) ) . '

'; $content = $prepend . $content; } } return $content; }

3. Read More Text:

Below snippet will enable you to reedit the “Continue Reading” or create a button by HTML.


// Changing excerpt more
function athemeart_change_excerpt_more_text( $more ){
  global $post;
  return '… ID).'" title="'.esc_attr(get_the_title($post->ID)).'">'.'Read More »'.'';
}
add_filter('excerpt_more', 'athemeart_change_excerpt_more_text');

4. Post Length:

In WP, the length of excerpts is 55 words, by default. And below snippets will make the length to 24 words for you. But remember, you can make the word’s length whatever you like.


//Change the default excerpt length in WordPress (default is 55 words)
function athemeart_change_excerpt_length( $length ) {
  return 24;
}
add_filter( 'excerpt_length', 'athemeart_change_excerpt_length', 9999);

5. Widgets Shortcodes:

Shortcodes are notably effective, and these are helpful to use in widgets. Therefore, the code below will enable you to add shortcodes in the text widget and execute them.


//Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');

Do you like to add your branding logo to your site with ease? No problem, the below code will let you add the top left logo to your dashboard. Also, you can rebuild CSS to link to any file with background-image properties.


//Adds a custom logo to the top left of the WordPress admin
function athemeart_custom_logo_wp_dashboard() {
echo "";
}
add_action('wp_before_admin_bar_render', 'athemeart_custom_logo_wp_dashboard');
Note: Be sure you upload an admin-icon.png at your theme’s directory.

7. SVG upload:

In today’s tech world, SVG format is turning more worthy for logo files. Yes, you’ll be able to use a plugin or below code to enable this functionality.

WP doesn’t permit SVG for security issues by default. But the below snippets only allows an admin to upload SVG.


//Enable SVG upload
function athemeart_enable_svg_upload( $mimes ) {
  //Only allow SVG upload by admins
  if ( !current_user_can( 'administrator' ) ) {
    return $mimes;
  }
  $mimes['svg']  = 'image/svg+xml';
  $mimes['svgz'] = 'image/svg+xml';
  
  return $mimes;
}
add_filter('upload_mimes', 'athemeart_enable_svg_upload');

8. XML-RPC Disable:

In general, you needn’t enable XML-RPC always and have enabled it can cause security issues. So, use the below note to disable XML-RPC and improve site security.


//Disable XML-RPC
add_filter('xmlrpc_enabled', '__return_false');

9. Migrate jQuery:

It’s best to delete it to take one request away from page loads. The below notes will delete jQuery Migrate from your site. After deleting, take a few of your pages to ensure the site is still functioning perfectly.

So, use it:


//Remove jQuery migrate
function athemeart_remove_jquery_migrate( $scripts ) {
    if ( !is_admin() && !empty( $scripts->registered['jquery'] ) ) {
        $scripts->registered['jquery']->deps = array_diff( $scripts->registered['jquery']->deps, ['jquery-migrate'] );
    }
}
add_action('wp_default_scripts', 'athemeart_remove_jquery_migrate');

10. Hiding WordPress version:

By obscurity, the WordPress version touches again on hidden security. Its version shows on the header section of the source code by default. Simply adding the following code can remove it. Or, you may use a premium plugin like Perfmatters to hide it by a 01 click.


function wp_version_remove_version() {
return '';
}
add_filter('the_generator', 'wp_version_remove_version');
Aware: Source code editing can also ruin your site if not done properly. If you’re not flexible, then call the developer first.

11. File Editing Disable:

Your site security will compromise when you’ve too many users and admin. Plus, bad practice is to giving authors or contributors admin access. Sad but true, the scenario repeats every time.

Execute the below code to disable the ‘edit_themes,’ ‘edit_plugins,’ and ‘edit_files’ abilities of all users.


define('DISALLOW_FILE_EDIT', true);

WP saves revisions every sixty (60) seconds by default. And you can change its value by putting a custom interval as below:


define( 'AUTOSAVE_INTERVAL', 160 );

Or, if you want to decrease the largest number or disable post revisions, settle the below statement:


define( 'WP_POST_REVISIONS', false );

And if you want to limit the largest number of revisions, instead, settle the below statement:


define( 'WP_POST_REVISIONS', 10 );

Plus, WP saves trash pages, posts, and comments for thirty (30) days; after that, it deletes permanently. You can replace its value with the below statement:


define( 'EMPTY_TRASH_DAYS', 10 );
Tip: You have the power to disable trash by turning its value to 0. But rest assured, you can’t recover contents anymore.

13. Memory Size:

Sometimes you may see a message like the below:

Fatal error: Allowed Memory Size of XYZ Bytes Exhausted.

It means WP tries to allocate 40Mb for one site and 64MB for multi-site installations by default. But you can add a custom value to adopt with the below line:


define( 'WP_MEMORY_LIMIT', '128M' );

And if want you can place the largest memory limit by the below statement:


define( 'WP_MAX_MEMORY_LIMIT', '256M' );

14. Constant Updates:

It’s a crucial feature for an admin that enables keep securing their website every time.

You may disable each automatic updates by settling the below statement:


define( 'AUTOMATIC_UPDATER_DISABLED', true );
Important: To disable security updates is not a good plan, ever.

Automatic or Constant updates are not working with major releases. But you’re able to update any core by below statement:


# Disables all core updates:
define( 'WP_AUTO_UPDATE_CORE', false );

# Enables all core updates, including minor and major:
define( 'WP_AUTO_UPDATE_CORE', true );

# Default value is minor:
define( 'WP_AUTO_UPDATE_CORE', 'minor' );
Remember: If you place DISALLOW_FILE_MODS to true, each edited file will disable. And it also affects your installations and updates, including theme and plugin.

15. Security:

You can disable the file editor offered in the admin panel. The below statement will veil the Editor Appearance screen:


define( 'DISALLOW_FILE_EDIT', true );

After that, you may force to move data over SSL for any sign-in and admin session by using the below statement:


define( 'FORCE_SSL_ADMIN', true );

The next two constants permit blocking external requests and listed allowed hosts.


# External Hosts:
define( 'WP_HTTP_BLOCK_EXTERNAL', true );

# List Admitted Hosts:
define( 'WP_ACCESSIBLE_HOSTS', 'example.com,*.anotherexample.com' );

Summary

So, these are the topmost and practical code snippets for WordPress to help you. And if you got any problem, you can use it with ease. But if you saw the code snippets didn’t give you the solution, please let us know through the below comment sections.

As we (Athemeart) are a core development firm, we’re always ready to support you. Write down what you need in the comments sections below. And we’ll back to you as soon as possible with the necessary info.

At last, I hope this post will help you, and please send your reviews to us. And please wear a mask before going outside; plus, stay safe and secure. Stay with us for more important, practical, and helpful articles.

Inspire us with your love!

Facebook
Twitter

Pinterest

Reddit

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *

3 + 8 =

Previous post How to Customize a WPZOOM Theme with Elementor Pro
Next post Install Nginx , PageSpeed, MariaDB, PHP and PhpMyAdmin on Ubuntu