Home » Here are 20 actionable tips to write better and more efficient PHP functions

Here are 20 actionable tips to write better and more efficient PHP functions

Estimated Reading Time: 4 min

Rate this post

Here are 20 useful WordPress code snippets to enhance your WordPress development and improve functionality:


1. Disable the WordPress Admin Bar

Remove the admin bar for non-admin users.

if (!current_user_can('manage_options')) {
    add_filter('show_admin_bar', '__return_false');
}

2. Change the Login Logo URL

Redirect the login logo link to your website.

function custom_login_url() {
    return home_url();
}
add_filter('login_headerurl', 'custom_login_url');

3. Add Custom Image Sizes

Define custom image sizes for your theme.

add_theme_support('post-thumbnails');
add_image_size('custom-size', 800, 600, true); // Cropped

4. Limit Post Revisions

Reduce the number of post revisions saved in the database.

define('WP_POST_REVISIONS', 3);

5. Remove WordPress Version Number

Enhance security by hiding the WordPress version.

remove_action('wp_head', 'wp_generator');

6. Disable XML-RPC

Prevent potential misuse of XML-RPC functionality.

add_filter('xmlrpc_enabled', '__return_false');

7. Redirect Non-Logged-In Users

Redirect visitors to the login page if they’re not logged in.

function redirect_non_logged_in_users() {
    if (!is_user_logged_in() && !is_page('login')) {
        wp_redirect(wp_login_url());
        exit;
    }
}
add_action('template_redirect', 'redirect_non_logged_in_users');

8. Customize the Login Page Style

Add custom CSS to the login page.

function custom_login_css() {
    echo '<style>body.login { background-color: #f4f4f4; }</style>';
}
add_action('login_head', 'custom_login_css');

9. Enqueue Custom Scripts and Styles

Properly add custom CSS and JS to your theme.

function enqueue_custom_assets() {
    wp_enqueue_style('custom-style', get_template_directory_uri() . '/css/style.css');
    wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/script.js', array('jquery'), null, true);
}
add_action('wp_enqueue_scripts', 'enqueue_custom_assets');

10. Remove Unnecessary Dashboard Widgets

Clean up the WordPress dashboard.

function remove_dashboard_widgets() {
    remove_meta_box('dashboard_primary', 'dashboard', 'side');
    remove_meta_box('dashboard_quick_press', 'dashboard', 'side');
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');

11. Register Custom Widget Areas

Add custom widget areas to your theme.

function custom_widget_areas() {
    register_sidebar(array(
        'name' => 'Sidebar Widget Area',
        'id' => 'sidebar-widget-area',
        'before_widget' => '<div class="widget">',
        'after_widget' => '</div>',
        'before_title' => '<h3>',
        'after_title' => '</h3>',
    ));
}
add_action('widgets_init', 'custom_widget_areas');

12. Add Google Fonts

Enqueue Google Fonts in your theme.

function add_google_fonts() {
    wp_enqueue_style('google-fonts', 'https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap', false);
}
add_action('wp_enqueue_scripts', 'add_google_fonts');

13. Redirect 404 Pages to Home

Avoid ugly 404 pages by redirecting to the homepage.

function redirect_404_to_home() {
    if (is_404()) {
        wp_redirect(home_url());
        exit;
    }
}
add_action('template_redirect', 'redirect_404_to_home');

14. Disable WordPress Emoji Support

Remove emoji scripts and styles for faster loading.

remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');

15. Add Excerpt Support to Pages

Enable excerpts for WordPress pages.

function add_excerpt_to_pages() {
    add_post_type_support('page', 'excerpt');
}
add_action('init', 'add_excerpt_to_pages');

16. Create a Custom Post Type

Add a “Portfolio” post type.

function custom_post_type_portfolio() {
    register_post_type('portfolio', array(
        'labels' => array(
            'name' => 'Portfolio',
            'singular_name' => 'Portfolio Item',
        ),
        'public' => true,
        'has_archive' => true,
        'supports' => array('title', 'editor', 'thumbnail'),
    ));
}
add_action('init', 'custom_post_type_portfolio');

17. Disable Comments on Media Attachments

Prevent comments on media uploads.

function disable_media_comments($open, $post_id) {
    $post = get_post($post_id);
    if ($post->post_type == 'attachment') {
        return false;
    }
    return $open;
}
add_filter('comments_open', 'disable_media_comments', 10, 2);

18. Remove the WordPress Admin Footer Text

Replace the footer text in the admin panel.

function custom_admin_footer() {
    return 'Powered by My Custom Theme';
}
add_filter('admin_footer_text', 'custom_admin_footer');

19. Customize the Read More Link

Change the “Read More” link text.

function modify_read_more_link() {
    return '<a href="' . get_permalink() . '">Continue Reading...</a>';
}
add_filter('the_content_more_link', 'modify_read_more_link');

20. Disable WordPress Auto-Update Emails

Stop receiving emails for automatic updates.

add_filter('auto_core_update_send_email', '__return_false');

Final Notes:

  • Use these snippets with caution and test them thoroughly.
  • Always create a child theme or use a custom plugin to add snippets.
  • Backup your site before making changes.

Discover more from Be-smart

Subscribe to get the latest posts sent to your email.

Photo of author

Flora

Here are 20 actionable tips to write better and more efficient PHP functions

Published

Update

I am Flora, a www passionate dedicated to sharing insights and inspiration for living a fulfilling life. With a good background in www, I aim to empower others people to truly and genuinely acknowledge them when they do admirable things, big and small.

Leave a Reply