If you are creating a WordPress site for a client you could install a load of useful plugins for them, only for them to go in deactivate the plugin and break the site.
Some of the WordPress plugins can be essential to the working of your website, things like the Akismet plugin, Yoast SEO WordPress plugin or even a commenting system like Disqus. You would never want your users to deactivate any of these plugins as they are essential to the workings of your website.
Here is a WordPress snippet that will allow you to install the plugin and once it is activated it will be removed from the plugin screen so it can not be deactivated. The plugin will still be active and can be used as normal but it means that users can not deactivate these plugins.
When you are developing any website you will always have different environments for your website, the number of environments you need will depend on the size of the project and how many people are involved in the project.
The main environments you will find in any website project are:
- Development Server - The developers local machine to development the website.
- Testing Server - Once development is finished this will be where all testing takes place.
- User Acceptance Testing Server - Where the client will review the changes to the website.
- Production Server - The live website.
The two environments you will always need to have is a development environment and a production environment. You will normally only have these two environments if the client doesn't need to view the site before it goes live. This means that once the developer has finished working on the site they can simply put this onto the production server. These are normally for the very basic of projects where the developer can simply test on their local development server before going live.
The reason for comments being turned on a blog is to add more in-depth discussion to a certain post. But some people comment just to get a link back to their own site, these comments will consist of things like "nice post", "thanks", that add nothing to the discussion of the post.
Most of the time blog owners will use moderation to delete these types of comments from appearing on the site, but if you have a popular site this can take up a lot of time. If you are getting annoyed with having to delete all comments like this then we can place a minimal character count on your comments.
For this you can use the filter preprocess_comment to check the comment before it is saved to the database. Within this filter you can check the character count of the comment by using the strlen() function.
There are going to be certain pages that you want to force SSL, so that the browser will redirect you to this page using a HTTPS connection so the data that is posted from this page is encrypted and secure.
This might be for any custom login pages you create in your application, or a credit card payment page. Normally you will have to write a script to force this page to be in SSL, but if you are making your application with WordPress you can use WordPress to redirect the browser to HTTPS by using the force_ssl filter.
This filter will run on every page and is used to decide if WordPress should redirect to HTTPS. Returning true from this function will automatically redirect the user to HTTPS and allow you to collect secure data from this page.
To display a sidebar in your WordPress site you first need to register the sidebar so that WordPress can assign widgets to it. When a sidebar is registered it will appear in the widget dashboard so that you can assign widgets to each sidebar.
When you have widgets assigned to your sidebar you need to add some code to your theme so it can be displayed on your site. To display a sidebar in your WordPress theme you need to use the function dynamic_sidebar(), this will allow you to pass in a sidebar id to display the contents of the sidebar.
There are times when you will want to check if the sidebar has widgets assigned to it before you include it on the page, this is commonly for styling them correctly. For example if you are creating a grid layout with your sidebars inside then you need to know which sidebars have widgets before you and them to the page.
The following code will use the WordPress function is_active_sidebar() to decide which sidebars have active widgets so we know what layout to use to display the two sidebars. The is_active_sidebar() function takes one parameter which is the id you give to the sidebar when it is registered, and it will return a boolean value, TRUE if the sidebar has widgets assigned and FALSE if the sidebar has no widgets assigned to it.
In a previous code snippet we found about how you can define new custom images sizes to use on your WordPress theme.
This will use the WordPress built in function called add_image_size().
add_image_size( 'custom-image-size1', 300, 900, true );
This function takes 4 parameters:
- Image Size Name
- Width in pixels
- Height in pixels
- Crop the image to the size.
Add this function into your functions.php file and everytime WordPress uploads a new image it will resize it to be the size you defined in this function.
The way you can use this in your theme is by passing the image size name into the_post_thumbnail() function, like this.
the_post_thumbnail( 'custom-image-size1' );
When you create a new user in the WordPress user manager they will be able to login to your site by going to the default WordPress login page /wp-login.php. Depending on the role you have assigned to this user they will see different options in the admin area.
Even if the user has the lowest level of role a Subscriber assigned to them they will be able to log into the WordPress admin area but all they will be able to do is edit their own profile. But if your not doing anything with this profile information then there is no need for them to login to edit the profile.
If you don't want other users to be able to log in to your WordPress admin area you can restrict access depending on a certain role.
In this example we are just going to allow admin users access to login to the admin area. We are going to use the WordPress action admin_init which will run when you access any admin page, from here we can check the access right of the user and redirect if we need to.
The WordPress login page has a checkbox that allows you to always be remembered when you login to WordPress. If you check this box and login successfully to WordPress a cookie will be saved on your computer to let WordPress know that you can bypass the login process.
This makes it really for people who use your site regularly as they won't have to login each time to have access to the admin area of WordPress.
By default the checkbox is always unchecked, so you have to select it each time. I prefer this to checkbox to be selected each time so I don't have to keep clicking on the login page.
Here is a snippet you can add to your functions.php file which will make sure that the checkbox is always checked when you go to the login page.
When you have a WordPress site you can assign different posts to categories, this allows you to group your posts together.
For example on this site I have created categories for tutorials and snippets, so if people are looking to create a complete feature you can look in the tutorials category, but if you just want some useful coding tips you can just in the snippets section.
When you create a category you are then able to navigate to this category to view all the posts which are grouped in the category.
There are times when you want to get a list of all the categories of your WordPress site, this is commonly found on a archive page. But I've recently created a mobile version of a site where a dropdown box was needed with a list of all the categories on the site.
With WordPress this is very easy to do you can just use one of the built in functions to get all the categories for the site. The WordPress function we can use is get_categories().
$categories = get_categories( $args );