WordPress helpful functions/helpers: As a WordPress theme developer or WordPress blogger, sometimes we want to update certain things to enhance the user experience of our blog or website. On the other side, sometimes we want to remove certain things for the same reasons.
So in this post, I have compiled all the valuable WordPress helpers functions that you have to follow few easy steps, and with it, you can have control over everything you want to customize in WordPress with these useful helpers.
Let’s have a look at them; for the sake of easier navigation, please click on any of the title’s links below, and it will scroll and take you to that exact section:
WordPress Helpers:
- Add Post Category class to body_class
- Protect the WordPress wp-config.php file
- WordPress Memory Exhausted Error
- WordPress Get Current Page ID
- WordPress Disable All Comments
- Remove Images from Content
- Remove P tags around images
- Disable Help Menu for Admin Only
- Display Comment Count Using a Shortcode
- Add Class to Next & Previous post_link
- Make Mandatory Featured Images For Posts
- Display Content or Images on Last Post.
- Add body_class to Page Templates.
- Require Minimum Comment Length
- WordPress Remove Height Width Attributes
- Block
/wp-admin
for all Non-Admin Users - WordPress Roles List
- Change User Role Name
- Add and Remove WordPress User Role
- Display Post Author’s Name, Link, and Posts Link
- Display Author Avatar
- Fetch all Authors list
- Send Email Notification When User Profile’s updated.
- Remove Admin Color Scheme Options From User Profile
- Display Gravatar Image as Favicon
Post Category to body_class
Are you looking for a code snippet that will add a category to body_class on every post page? Then here is the simple function that does for you:
First of all, copy the below codes and paste them into your WordPress theme’s functions.php file. Later save and check the result.
Code for Add Category to body_class
Protect the WordPress wp-config.php file.
As you know, the wp-config.php file stores our database name, username, password, and other settings…
…and if the hacker gets access to the wp-config.php
file, It can be very dangerous for our website.
So if you want to secure your website by protecting wp-config.php
access with .htaccess?
First of all, open your website directory via Cpanel or FileZilla and make sure you have enabled it to show hidden files.
After that:
Open the .htaccess file and copy the below code and paste it into your .htaccess file.
Code: To Protect wp-config.php file.
After copying and pasting, save the file and view the result by accessing the wp-config.php file.
That’s it.
WordPress Memory Exhausted Error
If you want to fix WordPress Memory Exhausted Error without using any Plugins and increase the memory limit, let’s look at the following steps.
First of all, Copy the below-embedded code and paste it into your WordPress’s wp-config.php file.
Code for: Increase WordPress Limit.
After that:
Save the file and view the result in the browser.
WordPress Get Current Page ID
Sometimes we want to get the Current page ID or Post ID (outside of the loop), to perform certain operations in WordPress and for that many the time we have to either use a plugin or do some advanced logic; in the below code snippets, I have written simple code which will do that job for you, and you won’t need any plugin.
Let’s begin.
Copy the below-embedded code and paste it into your WordPress theme’s functions.php file.
Code to display Current Page or Post ID
After that:
Save the file and view the result in your browser.
Another Method:
It also works the same as the above.
Just the difference from global $post
and global $wp_query
the above code is, it ($wp_query) can display tons of things if you want.
That’s it.
Read also: Get Current Page Slug
WordPress Disable All Comments
If you want to disable comments on all posts without having to go to every post, this is the code you were looking for.
First, Copy the below-embedded code and paste it into your websites’ SQL query with phpMyAdmin.
Code to Disable Comments:
After that:
Check the result by taking a look at some posts.
That’s it.
Remove Content’s images
If you want to make your post’s content text only by automatically removing all the images from the posts? Then, this is the guide you were looking for.
Here…We have written a simple function that will quickly remove any images from content (the_content();
function).
Just follow the below steps:
Copy the below-embedded code and paste it into your WordPress single posts page (single.php) or on the pages where on you want to remove images.
Code to get rid images from the_content();
After that:
Save the file and check the result in the browser.
Remove Paragraph tags from Around Images.
Sometimes you’ll see there are P tags around your attached media inside the post, and you know, sometimes it occupied the margin and line-height, So if you are looking for a way to remove the <p> tag from around Images in the_content function, then follow the below step.
Copy the below-embedded code snippets and paste them into your functions.php file.
Code to Remove Attached images surrounding <P> Tags.
After that:
Save the file and check the result in your browser.
That’s it.
Read also: Remove Empty ‘P’ and ‘BR’ Tags from Contact Form 7
Disable the Help Menu for Admin Only
Do you want to disable the help menu located on the right side on the top bar for the admin only?
Then you are at the right place.
First of all, copy the below code and paste it into your theme’s functions.php file.
Code Snippets to Disable Help Menu For Admin users
After that:
Save the file and view the result by login into the dashboard with the admin credential.
That’s it.
Display Comment Count Using a Shortcode
Sometimes we want to display the number of comments on the content of our posts or widgets for the specific posts…
…and for this, we need to build some shortcode.
To get the total comments to count, we can use the following code snippets; let check how we can do that:
Copy the below-embedded code and paste it into your WordPress theme’s functions.php file.
Snippets: Generate Shortcode for Total Comments:
After that:
Save the file and view the result by copy and paste the below shortcode into your posts or widgets…
…then change the tocomment id
the post’s id of which you want to display the number of comments.
Show Comments Count Code:
That’s it.
Add Class to Next & Previous post_link
Do you want to add CSS Class to the Next and Previous post_link function with just simple code?
Then you land at the right place.
Here:
We have written a simple function with a step-by-step guide that will help you add Class in Next and Previous post_link easily.
Let’s get started:
First of all, copy the below-embedded code and paste it into your WordPress theme’s functions.php file.
Code Snippets for Add Custom Class to Next & Previous link.
After that:
Change the class name to whatever you want, then save and view the result in your browser.
That’s it.
If you have any questions or opinions about this post, let us know in the comment section.
Require featured image before publishing post
If you are looking for a way to make the featured image in your post require/mandatory? Without attaching, one cannot publish the center; the following code snippet will help you avoid posts without a featured image.
First, you need to copy the below function code and paste it into your WordPress theme’s functions.php file.
Code Snippets for WP Require Featured Image
After that:
Save the file and check the result by creating and publishing posts without an image…
…So if it gives the error “You must select Featured Image. Your Post is saved, but it can not be published“ then it’s working.
That’s it.
Display Content or Images on Last Post.
Do you want to display some content or images on the last post’s page without using having to install another plugin, then the following code snippet may help you to do that:
First of all, you need to copy the Below embedded code snippets into your WordPress theme’s posts loop page file.
Code snippets for Display Content on the Last post
After that:
Just change the content of the above code to whatever you want to display.
Then save the file and see the results in the browser.
Add body_class to Page Templates.
If you want to add the custom class in body_class function to page templates in WordPress with just simple code? Then let’s look to the following steps and add a new class in the body tag and customize the way we want.
First, you need to copy the below code and paste it into your WordPress theme’s functions.php file.
Code snippets for WordPress add a body class
After that:
Change the template file name in line no: 10 to your’s template name and change the CSS class name on line no: 11 to whatever you want.
For Example:
We have created the template for videos and named it the videos.php file. Now we want to add a custom body_class called “videos” on that template page.
So we would modify our code, and it would look like the below snippets:
That’s it.
Require Minimum Comment Length
If you want if someone to comment on your blog to at least type a few characters, to make the conversational flow or improve the SEO as the more length one post has, the better it will rank…
…Then, adding the comment minimum character length in the comment would help you a lot. So let’s have a look at how we can do this.
First of all, you need to copy and paste the below-embedded code into your WordPress theme’s functions.php file.
Code for Minimum Comment Length
After that:
Save the file and view the result by commenting.
WordPress Remove Height Width Attributes
Are you looking for a guide that will help you remove width and height attributes from WordPress post images easily? Then this is the only guide you were looking for.
Let’s look at how we can do that.
The only thing you need to do is just copy and paste the below-embedded code into your WordPress theme’s functions.php file.
Code to Remove Width & Height of Images
After that:
Save the file and check the result by inserting images in posts.
That’s it.
Block /wp-admin
for all Non-Admin Users
We often don’t want any of the users (Subscriber, Editor, Author, Contributor) who logged in to our WordPress blog or website to access WordPress Dashboard AKA WP-Admin.
So our content will be safe and will prevent some hacking attacks on our website.
To block wp-admin for non-admins, you need to use some function code embedded in this post.
Just follow the below guides.
We have posted a simple function code below that will help you easily block the wp-admin area for all users (Editor, Subscriber, Author, Contributor).
Just copy the below code and paste it into your theme’s functions.php file.
Code For Restrict All Users Access in WP-ADMIN
That’s it.
WordPress Roles List
Do you want to fetch the list of all available WordPress Roles to display so the users can go to one of the authors’ posts and check their posts? Then the below code snippets would help in this case:
To display the list of all WordPress roles, you need to copy the below-embedded code and paste it wherever you want…
…such as page.php, archive.php, search.php, single.php etc.
After that:
Save the file and view the result in your browser.
That’s it.
Read also: Change User Role Name, Add or Remove User Role
Change User Role Name
If you want to change the user role name in your WordPress blog or website without using a plugin, this customization guide will help you.
First, we need to copy the below-embedded code and paste it into your WordPress functions.php file.
After that:
Just change the user role (from the subscriber, editor, administrator, author, etc.) to which you want to give a different name…
…then, write the new name that you want to assign to that user role.
Example: In the below codes, we have changed the user role name of “Subscriber” to “Customer“.
That’s it.
Add and Remove WordPress User Role
If you are looking for a way to add a custom role and remove the existing role…and this all without using a single plugin, then this is the custom WordPress helpers you’ll love to use.
Whether you want to add or remove a role from WordPress, you need to copy the below code snippets and paste them into your WordPress theme’s functions.php file.
After that:
Save the file and view the result by login into WordPress Dashboard > Users > Add New.
Add Custom User Role WordPress
Remove User Role WordPress
Below we have embedded two code snippets which from 1 is for removing single user roles and 2nd code for removing multiple user roles.
Following are the default user’s role list and that you can use to remove:
- Administrator
- Editor
- Author
- Contributor
- Subscriber
Code for: Remove one user role.
We removed the “editor” role from the users’ role list, but you can remove anyone by changing the role name.
Code for: Remove multiple user roles.
That’s it.
I hope the above code helped you. If you have any questions or opinions about this guide, let me know in the comment below.
Display Post Author’s Name, Link, and Posts Link
If you are looking for the simple code that will help you to display Author Name easily, Author Link, Author Post’s Link, or Author Posts, Then I got you.
Here we will give you some code snippets that can easily display Author Name, Author Posts, Author Link, Author ID, etc.
Just follow the below guide.
Display Author’s Name
Look in the default theme’s index.php file for the proper template tag:
1 | <code><?php the_author() ?></code> |
and an excellent place to put it.
Display Author’s ‘Public’ Name
Displays the value in the user’s Display name publicly as the field.
1 | <p>This post was written by <?php the_author(); ?></p> |
Get Author link
1 | <p>Written by: |
2 | <?php echo get_the_author_link(); ?></p> |
The Author’s Post
1 | <p><?php the_author(); ?> has blogged <?php the_author_posts(); ?></pre> |
2 | </div> |
3 | <pre>posts</p> |
The Author’s Post Link
1 | <p>Other posts by <?php the_author_posts_link(); ?></p> |
for more info, check below URL
http://codex.wordpress.org/Template_Tags/the_author
Display Author Avatar
To display the Author Avatar (Gravatar) within Posts or Pages, follow the below steps:
To add a post/page author avatar, you need to copy the below code and paste it within your single.php (Posts) file or page.php file (Pages).
After that:
Save the file and see the result.
Code for WordPress: Get Author Avatar
That’s it.
Fetch all Authors list
If you are looking for a guide to fetch all the Authors’ lists posted in your blogs, use the following code snippets.
Please copy the code below and put it into the file where you want to list all authors.
Send Email Notification When User Profile’s updated.
Sometimes we want to send an automatic notification to the user whose profile details we recently updated; to do that, we can follow the guide:
Just do the following steps.
First, copy the below code that we have embedded and paste it into your WordPress Theme’s functions.php file.
After that:
Save the file and see the result by changing any user’s name, social links, biographical info, etc., then click on the Update Profile button.
Code for Send Email Profile Updates.
That’s it.
Remove Admin Color Scheme Options From User Profile
Sometimes, we don’t want to use have the Admin Color Scheme Options from the user profile; to remove that, we can follow the below guide:
Just follow this step-by-step guide.
First, You need to copy the below-embedded code into your WordPress theme’s functions.php file.
After that:
Save the file and see the result by logging in to your WordPress Dashboard > Users > Your Profile.
Code for hiding/Removes the Color scheme of the user profile.
I have one more custom function that can also do the same thing.
Another Code for hiding/Remove Color scheme of user profile
That’s it.
Display Gravatar Image as Favicon
Have you ever wondered about using the Gravatar image as the favicon in your WordPress website or blog? Then this guide will help you to use it without having to install any plugin?
As you know, Using the gravatar image as the favicon is the best option for the person who uses their website logo or own image in their Gravatar profile.
Let’s start it.
To use Gravatar as the favicon, you need to copy the below code and paste it into your theme’s functions.php file.
After that:
Save the file and view the result in your browser.
Code to Display Gravatar as Image
That’s it.
That’s all we have for now, and we will keep updating this guide with all the latest and useful common WordPress helpers in the future. Let us know in the comment section if any of the above snippets helped you customize the WordPress the way you wanted.