includes/class-toptal-save-activator.php Now, let’s proceed with the plugin deactivation. It creates a new page using the wp_insert_post() function and saves the page ID to the database using add_option(). The activate() function is called when the plugin is activated. $saved_page_id = wp_insert_post( $saved_page_args ) Īdd_option( 'toptal_save_saved_page_id', $saved_page_id ) Let’s start with the activation process: _( 'Saved', 'toptal-save' ), We can do all of this in includes/class-toptal-save-activator.php and includes/class-toptal-save-deactivator.php. When our plugin is deactivated, we will get the “Saved” page ID from the database, and then delete the “Saved” page, removing any trace of the plugin itself. While creating that page, we will add a shortcode for our saved items into the content of that page.Īt the end, we’ll save the page get its ID and store it in the database, so we can access it later on deactivation of the plugin. When our plugin is activated, we’ll create a page named “Saved,” which will hold the user’s saved items in it. It’s important for our plugin to properly handle activation and deactivation. If you open up your WordPress Dashboard, and go to plugins, you’ll see that your plugin is listed there. You can use this page to generate a plugin codebase based on this WordPress Plugin Boilerplate:Įxtract it, and put it in your WordPress installation folder: wp-content/plugins/. It’s well-structured, object-oriented, and efficient. With a shortcode, the Saved page can be rendered wherever it is added. Delete Saved page on deactivation of the plugin.Create a Saved page on activation of the plugin.This is where visitors view the list of posts they’ve saved. The messages below will appear on-screen in response to a visitor’s interaction with the plugin or as labels on actionable items: If user is not logged in, save content to cookies.If user is logged in, save content to a custom user field.Provide an option to change the messages that appear on the visitor facing part of the plugin.Provide an option to enable the functionality only for logged in users.Offer users the option to decide whether they want to use our predefined styling or not.The ability to choose the type of posts where we want this button added.The ability for admins to add the “Save Item” button to the end of the content. ![]() The plugin we’re building will allow site visitors to save content to read later.įor registered users, we’ll store the list in the database, and for anonymous users, we’ll save the list using cookies.īelow is an outline of the features and functionalities that our plugin will provide. Start with a plan.įirst, let’s list the features our plugin will have and outline exactly what it needs to do. If you want to review the final source code as you read along, you can find it here. In this tutorial, you’ll learn how to build a simple WordPress plugin the right way. ![]() ![]() Whether you’re building one from scratch, or based off a boilerplate, following well-documented best practices is absolutely essential. Building a great WordPress plugin begins with careful planning.
0 Comments
Leave a Reply. |