Using WordPress ‘publish_phone’ PHP action

The publish_phone WordPress PHP action is triggered after a post submitted by email is published.

Usage

add_action('publish_phone', 'your_custom_function');
function your_custom_function($post_ID) {
    // your custom code here
}

Parameters

  • $post_ID (int) – The post ID of the published post.

More information

See WordPress Developer Resources: publish_phone

Examples

Send an email notification when a post is published

This example sends an email notification to the admin when a post submitted by email is published.

add_action('publish_phone', 'send_email_notification');

function send_email_notification($post_ID) {
    $admin_email = get_option('admin_email');
    $post_title = get_the_title($post_ID);
    $subject = "New Post Published: " . $post_title;
    $message = "A new post submitted by email has been published. Post ID: " . $post_ID;
    wp_mail($admin_email, $subject, $message);
}

Log published email posts to a custom log file

This example logs the post ID of published email posts to a custom log file.

add_action('publish_phone', 'log_email_published_posts');

function log_email_published_posts($post_ID) {
    $log_file = fopen('email_published_posts.log', 'a');
    fwrite($log_file, "Post ID: " . $post_ID . " published at " . date("Y-m-d H:i:s") . "\n");
    fclose($log_file);
}

Update post meta after publishing email post

This example adds or updates a custom meta field ‘_email_published’ with a value of ‘yes’ when an email post is published.

add_action('publish_phone', 'update_email_published_meta');

function update_email_published_meta($post_ID) {
    update_post_meta($post_ID, '_email_published', 'yes');
}

Add a category to published email posts

This example adds a ‘Published by Email’ category to posts published via email.

add_action('publish_phone', 'add_email_published_category');

function add_email_published_category($post_ID) {
    $category_id = get_cat_ID('Published by Email');
    if (!$category_id) {
        $category_id = wp_create_category('Published by Email');
    }
    wp_set_post_categories($post_ID, array($category_id), true);
}

Change post status to ‘private’ after publishing email post

This example changes the post status to ‘private’ after a post submitted by email is published.

add_action('publish_phone', 'set_private_after_publish');

function set_private_after_publish($post_ID) {
    $post_data = array(
        'ID' => $post_ID,
        'post_status' => 'private',
    );
    wp_update_post($post_data);
}