Archives: Snippets | Page 17

Add Custom Post Types to the Main RSS Feed

To add Custom Post Types to the site’s main RSS feed by default, add the following code to your theme’s functions.php file:

// ADD CUSTOM POST TYPES TO THE DEFAULT RSS FEED
function custom_feed_request( $vars ) {
 if (isset($vars['feed']) && !isset($vars['post_type']))
  $vars['post_type'] = array( 'post', 'site', 'plugin', 'theme', 'person' );
 return $vars;
}
add_filter( 'request', 'custom_feed_request' );

Remove DNS Prefetch in WordPress

Making WordPress faster is an ongoing process. One small step with minimal impact is removing DNS prefetching for the following addresses (if you want to remove them):

<link rel='dns-prefetch' href='//s.w.org'/>
<link rel='dns-prefetch' href='//fonts.googleapis.com'/>

In the page source it looks like this:

Removing DNS prefetch loaded automatically by WordPress

To remove these, add the following code to your theme’s functions.php file:

function remove_dns_prefetch () {
    remove_action( 'wp_head', 'wp_resource_hints', 2, 99 );
}
add_action( 'init', 'remove_dns_prefetch' );

If you are not familiar, here is a post explaining what DNS is.

Limit Text to N Lines with CSS

To limit text to a set number of lines in CSS, use -webkit-line-clamp. The text is cut after the last allowed line and an ellipsis (…) is added, which keeps card excerpts, product titles and descriptions the same height.

This is a CSS-only fix. The full text stays in the HTML, so screen readers and search engines still get all of it.

CSS to Limit Text to N Lines

Add this to your stylesheet (in WordPress, the child theme’s style.css or Appearance > Customize > Additional CSS) and change the number to the lines you want to show:

.text {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2; /* number of lines to show */
    overflow: hidden;
}

Example markup:

<div class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam consectetur venenatis blandit. Praesent vehicula, libero non pretium vulputate, lacus arcu facilisis lectus, sed feugiat tellus nulla eu dolor. Nulla porta bibendum lectus quis euismod.
</div>

How line-clamp Works

The three prefixed properties only work together. -webkit-line-clamp has no effect unless the element also has display: -webkit-box (or -webkit-inline-box) and -webkit-box-orient: vertical.

  • -webkit-line-clamp: 2 sets the maximum number of lines and draws the ellipsis at the end of the last one.
  • overflow: hidden hides the lines after the limit. Without it, the ellipsis appears but the remaining text is still visible.
  • text-overflow: ellipsis is not needed for the multi-line version. The ellipsis comes from the line clamp itself.

The prefix looks old, but it is the version that works. -webkit-line-clamp is supported in Chrome, Edge, Safari and Firefox (since Firefox 68), and MDN notes this combination of properties will continue to be supported.

The unprefixed line-clamp property exists in the CSS specification, but as of 2026 no stable browser release supports it. It is only available in preview builds or behind flags, so don’t use it on its own yet.

Limit Text to One Line with an Ellipsis

For a single line you don’t need line clamping. The classic text-overflow approach works in every browser:

.text-one-line {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

text-overflow does not create the overflow by itself. The text must be forced onto one line with white-space: nowrap, and the element needs a limited width (a block element, or an inline-block with a set width).

Common Problems

If the clamp doesn’t behave as expected, check these first:

  • The next line peeks out at the bottom. Vertical padding on the clamped element shows part of the hidden line, because overflow: hidden clips at the padding edge. Move the padding to a wrapper.
  • Nothing is clamped. Another rule is overriding display, for example a display: flex or display: block from the theme.
  • You need a character limit, not a line limit. CSS can’t count characters. Trim the text on the server instead: mb_strimwidth() in PHP cuts a string to a set width, and wp_trim_words() in WordPress cuts it to a number of words.

Line clamping pairs well with text-wrap: balance and pretty for headings on cards. For more CSS optimization tips, see Optimize CSS for Better Performance.

FAQs

Common questions about limiting lines of text with CSS:

Is there a CSS max-lines property?
Not one you can use today. The working method is -webkit-line-clamp combined with display: -webkit-box, -webkit-box-orient: vertical and overflow: hidden. The standard line-clamp property is not yet supported in stable browsers.
How do I limit text to two lines with an ellipsis?
Use the snippet above with -webkit-line-clamp: 2. The browser adds the ellipsis at the end of the second line automatically.
Does -webkit-line-clamp work in Firefox?
Yes. Firefox has supported the -webkit- prefixed version since Firefox 68, together with display: -webkit-box and -webkit-box-orient: vertical.
Can CSS limit text to a number of characters?
No. CSS works with lines and widths, not character counts. To cap characters or words, shorten the text in PHP or JavaScript before it is output.

Disable Payment Gateway for Specific Shipping Method in WooCommerce

A look at the WooCommerce checkout page and specifically how to disable a payment method for a customer based on the shipping method they selected. For example, disable “Cash on Delivery” for the “Local Pickup” shipping method.

WooCommerce - Disable a payment method for a specific shipping method

Here is a snippet that lets you do something like this (functions.php):

/**
 * @snippet       Disable a payment method for a specific shipping method
 * @author        Roee Yossef
 * @website       https://savvy.co.il
 */
  
function sv_gateway_disable_shipping_326( $available_gateways ) {
     
   if ( ! is_admin() ) {
        
      $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
        
      $chosen_shipping = $chosen_methods[0];
        
      if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
         unset( $available_gateways['cod'] );
      }
        
   }
     
   return $available_gateways;
     
}
add_filter( 'woocommerce_available_payment_gateways', 'sv_gateway_disable_shipping_326' );

Replace PayPal Logo in WooCommerce

If you use PayPal as a payment option on the WooCommerce checkout page, you can replace the PayPal logo displayed there. Add the following code to your theme’s functions.php file:

/**
 * Add custom gateway icons
 *
 * @param string $icon Image HTML.
 * @param string $gateway_id Gateway ID.
 *
 * @return string
 */
function sv_custom_wc_gateway_icons($icon, $gateway_id)
{
    // Example for PayPal:
    if ('paypal' == $gateway_id) {
        $icon = '<img src="' . get_stylesheet_directory_uri() . '/images/paypal.svg" alt="' . __('PayPal') . '" />';
    }
    return $icon;
}

add_filter('woocommerce_gateway_icon', 'sv_custom_wc_gateway_icons', 10, 2);

Change the path and filename to your new image and you are set!

Extend WordPress Auto Logout Period

The following code lets you extend how long it takes before WordPress automatically logs you out of the admin area. Add this code to your theme’s functions.php file:

function keep_me_logged_in_for_1_year( $expirein ) {
   return 31556926; // 1 year in seconds
}
add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year' );

For more security tips, see Hardening WordPress Security.

Restrict Admin Menu Items by Username in WordPress

You can hide specific admin menu items from a specific user in the WordPress dashboard. Replace username with the login of the user you want to hide those menu items from:

function remove_menus()
{
    global $menu;
    global $current_user;
    get_currentuserinfo();

    if($current_user->user_login == 'username')
    {
        $restricted = array(__('Posts'),
                            __('Media'),
                            __('Links'),
                            __('Pages'),
                            __('Comments'),
                            __('Appearance'),
                            __('Plugins'),
                            __('Users'),
                            __('Tools'),
                            __('Settings')
        );
        end ($menu);
        while (prev($menu)) {
            $value = explode(' ',$menu[key($menu)][0]);
            if(in_array($value[0] != NULL ? $value[0] : "" , $restricted)) {
                unset($menu[key($menu)]);
            }
        } // end while

    } // end if
}
add_action('admin_menu', 'remove_menus');

This uses a WordPress hook. Learn more in WordPress Hooks Explained.

Remove Dashboard Meta Boxes in WordPress

The following snippet lets you remove specific meta boxes from the main Dashboard page in WordPress. These are the default meta boxes shown on the main admin screen.

To remove or change which boxes are displayed, add this snippet to your theme’s functions.php file and choose which boxes to remove:

function my_custom_dashboard_widgets()
{
    global $wp_meta_boxes;

    // Main column (left):
    // Browser Update Required
    unset($wp_meta_boxes['dashboard']['normal']['high']['dashboard_browser_nag']);
    // PHP Update Required
    unset($wp_meta_boxes['dashboard']['normal']['high']['dashboard_php_nag']);
    // Right Now - Comments, Posts, Pages at a glance
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
    // Right Now
    unset($wp_meta_boxes['dashboard']['normal']['core']['network_dashboard_right_now']);
    // Activity
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']);
    // Site Health Status
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_site_health']);

    // Side Column (right):
    // WordPress Events and News
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    // Quick Draft, Your Recent Drafts
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);

    // Recent Comments
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);

    // Incoming Links
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);

    // Plugins - Popular, New and Recently updated WordPress Plugins
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);

    // Other WordPress News Feed
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);


    // Recent Drafts List
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);
}

add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');

This is the page we are talking about:

Removing default meta boxes from the main Dashboard page

For safe admin customizations, see What Are Child Themes and How to Use Them.

Display All Post Types in Category Archives

If you want WordPress category archive pages to display all posts regardless of post type, add the following code to your functions.php file:

function any_ptype_on_cat($request) {

    if ( isset($request['category_name']) )
        $request['post_type'] = 'any';

    return $request;
}
add_filter('request', 'any_ptype_on_cat');

For more on custom post types, see How to Create Custom Post Types.