You can achieve this by using WordPress hooks and the save_post
action. You’ll need to add your custom script to your theme’s functions.php
file or create a custom plugin. Here’s how to do it:
For All Custom Post Types:
-
Open your theme’s
functions.php
file or create a custom plugin. -
Add the following code to register the
save_post
action hook:
add_action('save_post', 'your_custom_function', 10, 3);
function your_custom_function($post_id, $post, $update) {
// Check if this is a new post or an update
if ($update || 'publish' === $post->post_status) {
// Your script or code to be executed here
// You can use $post_id and $post to access the post's data
// Example: your_custom_script($post_id);
}
}
-
Replace
'your_custom_function'
with the name of your custom function that contains the script you want to execute. -
Inside the
your_custom_function
function, you can check whether the post is being updated or published as a new post. If it’s an update or a new post being published, your custom script or code will be executed. -
Be sure to replace the comment
// Your script or code to be executed here
with the actual code or script you want to run when any custom post type (CPT) record is published or updated. -
Save your
functions.php
file or activate your custom plugin.
Now, whenever a new custom post type record is published or updated, your custom script or code will be executed automatically for all custom post types.
For a Specific Custom Post Type:
If you want to execute the script only for a specific custom post type, you can add a conditional check within the your_custom_function
function like this:
add_action('save_post', 'your_custom_function', 10, 3);
function your_custom_function($post_id, $post, $update) {
// Check if this is a new post or an update
if ($update || 'publish' === $post->post_status) {
// Check if the post type is 'your_custom_post_type'
if ($post->post_type === 'your_custom_post_type') {
// Your script or code to be executed here
// You can use $post_id and $post to access the post's data
// Example: your_custom_script($post_id);
}
}
}
Replace 'your_custom_post_type'
with the actual name of your custom post type. With this code, the script will only execute when a new or updated post of the specified custom post type is published.
To apply the custom script to multiple custom post types using an array, you can modify the your_custom_function
function as follows:
add_action('save_post', 'your_custom_function', 10, 3);
function your_custom_function($post_id, $post, $update) {
// Check if this is a new post or an update
if ($update || 'publish' === $post->post_status) {
// Define an array of custom post types to target
$custom_post_types = array('custom_post_type1', 'custom_post_type2', 'custom_post_type3');
// Check if the post type is in the array
if (in_array($post->post_type, $custom_post_types)) {
// Your script or code to be executed here
// You can use $post_id and $post to access the post's data
// Example: your_custom_script($post_id);
}
}
}
In this code:
-
Replace
custom_post_type1
,custom_post_type2
,custom_post_type3
, etc., with the names of the custom post types you want to target. -
The
in_array
function is used to check if the post’s type ($post->post_type
) is in the specified array of custom post types. -
If the post type matches any of the custom post types in the array, your custom script or code will be executed when a new post or an update occurs for any of those specified post types.
By using this approach, you can easily apply the custom script to multiple custom post types by adding or removing them from the array as needed.