7 useful code snippets for your Wordpress theme

Bogdan Bendziukov
3 min readJan 31, 2023

--

Wordpress is an awesome CMS, and what makes it even more awesome is the ability to change the code without modifying the core, thanks to hooks and filters. You just need to use the right hook or filter and add it to your theme’s functions.php file. Here are 7 must-have functions in your functions.php file. I use wp_prefix_ to make functions’ names unique, but you should use your own prefix (usually, it’s your theme’s name).

Photo by James Harrison on Unsplash

Add custom class to reply link

If you need to apply an extra CSS class to the comment’s reply link (like your button’s class):

Remove a website field and cookie checkbox from comment form

By default Wordpress has a website field in the comments form. Here’s how to remove it:

Also, you can remove that “Save my name…” checkbox:

Set custom excerpt length and ellipsis

Default Wordpress excerpt length is equal to 55 words. You can change that value (for example to 30 words):

To change the default ellipsis of the excerpt […] and add the read-more link use this code:

Additionally, if you want to set different excerpt lengths to different post types you can do like this:

Add excerpt support to pages

If you want your pages to have an excerpt field (like posts), you can enable it like this:

Then you can display the excerpt using the_excerpt() function.

Show all posts without pagination

If you don’t want to paginate your posts, you can show all of them at once:

First, we check if the query is the main query, then if we are on the category or on the home page. If all is true — set posts_per_page argument to -1 (means all posts).

Add a noscript notice

Rarely, users might disable javascript in the browser or for a specific page. To remind them they’re viewing your site with disabled javascript you need to add a <noscript> tag to the <head> section:

Show menu item’s description

If your theme doesn’t show descriptions for menu items, you can output them like this:

Thanks for reading, I hope you will find those code snippets useful for your websites.

--

--

Bogdan Bendziukov

I'm a web developer from Kyiv 💛💙. A WordPress enthusiast for 10 years. Writing tips and thoughts from my dev experience .