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.
publish_phone action
So, you want to know about the publish_phone action, huh? This action fires after a post submitted by email is published. To use the publish_phone action, you first have to register it using add_action. You can put this code into the functions.php of your activated theme or in a custom WordPress Plugin.
We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.
In the example below, we have defined a function weplugins_execute_on_publish_phone_event which takes 1 parameter and we registered it using add_action. The first parameter publish_phone is the name of the hook, the second parameter weplugins_execute_on_publish_phone_event is the name of the function to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed to the registered function.
Sometimes, you have to remove a registered hook, so you can use remove_action to remove the publish_phone action.
Parameters
- $post_ID: (int) The post ID.
Below, the 1 parameter is required to use this hook.
Live Example
Below is an example of how you can use this hook.
function weplugins_execute_on_publish_phone_event($post_ID) { // You can write code here to be executed when this action occurs in WordPress. // Use the parameters received in the function arguments & implement the required additional custom functionality according to your website requirements. } // add the action add_action("publish_phone", "weplugins_execute_on_publish_phone_event", 10, 1);
Example 1: Logging the Post ID
In this example, we log the Post ID to a custom log file whenever a post is published by email.
function weplugins_log_post_id_on_publish_phone($post_ID) { error_log("Post ID: " . $post_ID, 3, "/var/log/wordpress_post_id.log"); } add_action("publish_phone", "weplugins_log_post_id_on_publish_phone", 10, 1);
Example 2: Sending a Notification Email
This example sends a notification email to the admin whenever a post is published by email.
function weplugins_notify_admin_on_publish_phone($post_ID) { $admin_email = get_option('admin_email'); wp_mail($admin_email, "New Post Published", "A new post has been published with Post ID: " . $post_ID); } add_action("publish_phone", "weplugins_notify_admin_on_publish_phone", 10, 1);
Example 3: Updating Post Meta
In this example, we update a custom post meta field whenever a post is published by email.
function weplugins_update_post_meta_on_publish_phone($post_ID) { update_post_meta($post_ID, '_published_by_phone', true); } add_action("publish_phone", "weplugins_update_post_meta_on_publish_phone", 10, 1);
Removing the Hook
To remove a hook callback, use the example below.
remove_action("publish_phone", "weplugins_execute_on_publish_phone_event", 10, 1);
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
Contact Us
If you’re having any trouble using this hook or need customizations, please contact us and we’d be happy to assist you.
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.