This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
When working with WordPress, hooks are like a magical doorway to add your custom functionality. One of the hooks you might find yourself using is the admin_post action. It’s a nifty little hook that fires on an authenticated admin post request when no action is supplied. Let’s dive into some examples to see how you can put this to good use in your WordPress projects.
Example 1: Basic Usage of admin_post
First things first, you need to register this hook using add_action. Here’s a simple way to do that. This example demonstrates a function named execute_on_admin_post_event that’s triggered whenever the admin_post action fires.
function weplugins_execute_on_admin_post_event() { // Your code here will be executed when this action occurs. } // Add the action add_action("admin_post", "weplugins_execute_on_admin_post_event");
Example 2: Removing a Hook Callback
Sometimes, you might want to remove a registered hook. Here’s how you can use remove_action to remove the admin_post action. Make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
remove_action("admin_post", "weplugins_execute_on_admin_post_event");
Example 3: Custom Plugin Approach
At WePlugins, we always prefer creating a custom WordPress Plugin for using hooks. This way, nothing breaks when you update your WordPress Theme in the future. Here’s how you can structure your plugin to use the admin_post hook.
/* Plugin Name: WePlugins Admin Post Hook */ function weplugins_custom_admin_post_action() { // Code to execute on admin post } add_action("admin_post", "weplugins_custom_admin_post_action");
Contact Us
If you need any customization or run into any issues using this hook, feel free to contact us. We’re here to help!
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.