Add custom functionality to your plugin. First, add four functions for the customize-login settings, and then another three functions for the customize-admin settings. This completes the core part of the plugin's functionality, but more features can always be added later if desired.
- [Instructor] In this tutorial, we're going to add some custom functionality to our plugin. In previous videos, we added all the code necessary to get the plugin to this point. We've added the menu item, plugin page, registered all settings, to find all callback functions, and made some other optimizations to make sure that everything is safe and secure. So now we want to add the actual functionality that will make this plugin do something other than just remember settings. For this plugin, we have a section of options for the user to customize the login page, and another set of options for the user to customize the admin area.
So now we want to add the functionality for each of these seven features. Let's do so, one at a time, beginning with the Custom URL feature. First, let's create a new file. We're going to put this file in the includes folder. I'm going to name it core-functions.php. This file will be available to all admin and public-facing pages, as explained in the previous video, creating the plugin directory and files.
The includes directory is a great location to put core plugin functions that might be used on either side of WordPress. As we've done for other PHP files, we want to add our disable direct access snippet, which we can grab from the main plugin file. So we copy that code, and add it to our new file. Remember, this file won't do anything unless we include it.
So we want to include it in the main plugin file. To do so, we copy one of these lines and paste it outside of the if is_admin condition. We need to change the path and file name to match our core functions file. The file is in the includes folder. And the file name is core-functions.php.
And we'll add a comment to help keep things organized. Notice that these files are included only in the admin area, because of the is_admin condition, but now we want to include our new file in both the admin and public parts of the site. So we add our code outside of the is_admin condition.
At this point, our new file is included and we are ready to add our first custom function. Again, this option enables the user to customize the URL for the logo link on the login page. So the user can change the URL to whatever they want. To enable this feature, we can grab the code from the exercise files for this video. Now we can add the code to our new core functions file. This function customizes the URL of the login page logo, which we can verify by clicking on the logo.
Let's refresh the page. And now it takes us to our custom URL example.com. Let's look at what this new code is doing. First, notice that the login header URL is passed to the function as a variable. Right here. Next, we use get_option to get the plugin options. You can learn more about this in the previous video.
Then we conditionally check if the custom URL option is set and not empty. If so, then we use it to set the value of the URL variable. Notice that we sanitize the URL with the escape URL function. And lastly, we return the URL variable back to WordPress. Notice the hook that this function is registered with, login_headerurl. Next is the plugin's second feature which enables us to customize the title of the login link.
So the user can change the title to whatever is desired. To enable this feature, we can grab the code from the exercise files and add it to our new core functions file. This function customizes the title of the logo link. So let's refresh the page, and then hover over the logo, and there's the magic, right there. How does this function work? Well, it's pretty boring, really. First, the title variable is passed to the function here.
Then we get the options, as usual, and then we check the option to make sure that it's set and not empty. If so, then we use it to set the value of the title variable, and we sanitize it with the escape attribute function, and then lastly, we return the title variable back to WordPress. Notice the hook we are using here, login_headertitle.
It is designed for this exact purpose. So far, we added two of the seven functions that we need to add, Custom URL and Custom Title. In similar fashion, we want to add functions for the remaining five settings, Custom Style, Custom Message, Custom Footer, Custom Toolbar, and Custom Scheme. Let's quickly add these five functions, which are available in the exercise files. This is the code for the custom style function.
So we grab the code and add it to the core functions file and remember to save changes. This function basically is the same as the others. The main difference is that here, we are using the wp_kses_post function to sanitize any HTML that the user may want to add. Also notice the hook we are using, login_message. This hook is designed for adding a custom message to the login page, so it's just perfect for our needs.
Now let's verify that the code is working. And yes, there is the custom message. It is centered nicely, thanks to the custom style sheet that we added in the previous function. At this point, we've added functions for all of the options in the first section. Now let's add the three remaining functions for the second section. This section provides options for the user to customize the admin area. So the next function that we want to add is for the custom footer option.
Again, we grab the code from the exercise files and add it to our core functions file. We've already seen most of this code before. The main thing to note here is the hook that we are using, admin_footer_text, which is ideal for customizing the admin footer text. To see this function in action, we can check out the footer of any page in the admin area. Thanks to our new function, the admin footer now displays our custom message.
Next, we have the Custom Toolbar option which enables the user to simplify the toolbar. WordPress makes the toolbar easy to customize. After we add the code for this option, these two items will be removed from the toolbar, the comments link, and the new post link. To enable this, we grab the code and then add it to our core functions file. How does it work? Well, first it sets the toolbar variable to false as the default value.
Then it gets the plugin options and checks that the Customize Toolbar option is enabled. If so, then it uses the remove_menu function to remove the comments and the new content items from the toolbar. The hook we are using is here. Now let's jump back to the admin area to watch the menu items disappear. Here are the menu items that will be removed, thanks to our new function.
So let's refresh the page. And notice that with our function in place, the two items have been removed from the toolbar. And so if we disable the option and save the changes, we see the two items return to the toolbar. So this function is all set, and there's one more to go. The last function that we want to add is for the Custom Scheme option.
This enables the user to select a default admin color scheme for newly registered users. From the exercise files, here's the code that we want to add. And then we can add the code to our core functions file, like so, remember to save changes. In a nutshell, this function gets the selected color scheme from the plugin options and then updates the user's color scheme option using wp_update_user.
Note that this function is registered with the user_register hook, so it applies only to newly registered users. To verify that the new function is working properly, we can create a new user, and then view the user's profile screen and check the results. And here we can see the color scheme is now set to Ectoplasm for this new user, which is what we chose here for the option. So we're all set, and now we have completed adding all custom functions to our example function.
- WordPress APIs
- Action and filter hooks
- Activating and deactivating plugins
- Plugin security
- Creating the directory and files
- Adding menus and the settings page
- Inserting custom functionality
- Testing and debugging WordPress plugins
- Creating widgets
- Managing users and roles
- Adding custom post types and taxonomies
- Working with custom fields and database queries
- Using APIs: Transients, HTTP, and REST