No need for a routine here – use the WordPress hook system to “attach” a function that will run when a post is published:
// https://codex.wordpress.org/Plugin_API/Action_Reference/publish_post
function wpse_225371_post_published( $post_id, $post ) {
// Do your thang
}
add_action( 'post_published', 'wpse_225371_post_published', 10, 2 );
Related Posts:
- My customizer’s setting doesn’t set to the default and needed to click the control’s “Default” button before it’ll be set
- Function to return true if current page has child pages
- How do I change/modify the_post_thumbnail(); html output?
- Cleaning up WordPress to improve performance?
- List of all theme customizer control types?
- How to add a class to the attachment images
- How to draw media details for Isotope gallery using Photoswipe
- How to register images uploaded via FTP in media library?
- How to preserve PHP modifications while upgrading WordPress?
- How to correct schedule my event weekly with wp_schedule_event()
- Set the transport of the Customizer ‘header_image’ core setting to ‘postMessage’
- Change comments form title on a page by page basis
- Add custom image sizes to media uploader
- Setting up the child theme so as to enable right-to-left WordPress?
- delete an array element when its date expires
- How to add default images into theme customizer image control?
- Overriding a theme redux file in child theme
- Using wp_get_image_editor in a standalone script
- Change logo based on incoming domain
- How to break down importing of feeds
- PHP can I add line numbers to file_get_contents()
- Convert all images to PNG on file upload
- How to get specific image in media library with php
- Setting custom canonical urls
- Create another “Display Site Title and Tagline” checkbox, “Header Text Color” setting and control
- Converting HTML Template to WordPress Theme
- Set the background to a default image if there isn’t a specified “featured image”
- How to control WordPress image metadata (using Imagick)?
- How to check if a meta value has already been assigned to any user?
- Retrieve $_POST data submitted from external URL in WordPress(NOT API)
- How to pick the default selected value in wordpress dropdown?
- Modify custom field input data before creating a new post
- Compare the old get_theme_mod($name) to the new get_theme_mod($name) return value
- Scrape external webpage for first image and add it to new post
- Need help enqueueing webfonts
- Redirect to another page using contact form 7? [closed]
- How to obtain the current website URL in my theme?
- What exactly do this function declared into functions.php file of a WP theme?
- Clickable image link sends people to wrong URL
- Function Reference Documenting Template Tags for use in Custom Theme Templates?
- Create page template via functions.php?
- How to create a field in customize and show that in header.php?
- List post by title length
- Else/If Statement to Display Photo Descriptions
- How do i wrap woocomerce attribute in list?
- get_theme_mod outputs number when using WP_Customize_Cropped_Image_Control
- Image rotation issue (horizontal picture uploading as vertical) — Exif issue?
- Is there a hack for using is_page() within the function.php file?
- Adding a ‘style=’ bit to image_send_to_editor output
- Convert all uploaded PNG files to PNG-8 format
- Customize field names in backend profile edit page through function.php [duplicate]
- WP insert post Redirect after function has executed
- WP-Bakery – Add custom colors to buttons with template colors
- Can/should we delete wordpress cron jobs with no action?
- Retrieve alt text by referencing the img src
- Trying to update Woocommerce meta values
- not outputting medium size image URL
- How to use data URL in WordPress?
- update_user_meta as multiple value but with same meta key
- Having trouble creating two shortcodes, one for logged in user and one for visitors
- Delete post meta by serialized meta value
- Multiple cron jobs vs daily cron job doing multiple things? Which one yields better performance and scalability?
- post_exists stops working in a scheduled event
- Remove node from multisites?
- Automatically refresh page if widget is added to page?
- How do I get the featured post to share on Social Media and show on Individual Post pages?
- Retrieving specific images from Media Library
- Where can I find the declaration of `$_wp_theme_features`?
- Extend the WP_Customize_Image_Control class to change its non-frame $button_labels
- A Customizer checkbox control that sets the setting to “” or to “blank” and show() or hide() a color control
- Save the outputted image into the Media Library, with a different filename and extension
- Check if values exists DB
- Break a WordPress function to run in patches and re-continue
- WordPress causing all code to be displayed on line 1. Receiving multiple errors after cleaning cookies and cache
- How to add button to top of theme customizer?
- How can I clear the theme mod settings?
- Display a list of users with avatar filterable with alphabets
- Select area and checkbox data is not saving?
- HTML in PHP problem [closed]
- add if statement to the featured image
- How to remove the intro animation which appears during the load up of some WordPress themes? [closed]
- Remove default Image Sizes
- WP Custom tables query
- Understanding responsive imagery
- Add multiple HTML attributes to an Elementor button
- WooCommerce: write featured image dimensions to custom fields in product’
- Generate and upload screenshot as featured image for user-submitted post
- Background Tasks in a WP Cronjob?
- Remove all nav menu classes ( but keep useful ones… )
- Shortcode content output but not in correct place
- Automatically adding new post categories to menu
- get author_name from queried post
- How to unset a function from a action inside a class, that is called in another class
- Create a new account on site B with data from the purchase of site A
- Why is the current page loaded in the pop-up window and not the specified one?
- working code, not working – Plugin Dev
- Where do I implement this display of User Meta Data, and how to put it in a table?
- Replace Underscore (_) on Space ( )
- Load a Header in wordpress
- Trouble figuring out how to get my button to submit comment