Wordpress event onAfterPostUpdated or onAfterPostCreated - wordpress

Is there any way to be registered to wordpress events?
I want to know when a post was updated or created in the system, and get its ID.
Is it possible?
Thanks

You can use the save post hook. It is triggered when the post is created and updated.
Here's a sample.
function post_hook( $post_id ) {
if ( wp_is_post_revision( $post_id ) ){
//Post is being updated
} else {
//Post is being created
}
}
add_action( 'save_post', 'post_hook' );
Here's a link from the codex.
Save Post WP

Related

Save ACF field when publish or update post

When I publish or update a post, I want to save the slug of the post in an ACF field nammed "plan_slug".
The code I wrote is:
add_action('future_to_publish', 'actions_published_post');
add_action('new_to_publish', 'actions_published_post');
add_action('draft_to_publish' ,'actions_published_post');
add_action('auto-draft_to_publish' ,'actions_published_post');
function actions_published_post($post) {
update_field('plan_slug', $post->post_name);
}
Nothing runs good! The field in post back-end is not fullfilled...
Can you, please, tell me what is wrong in my code.
Thank you in advance.
this should work
add_action( 'save_post', 'save_custom_field', 10, 2);
function save_custom_field( $post_id, $post ) {
update_post_meta( $post_id, 'plan_slug', $post->post_name );
}

Show Woocommerce order details on custom thank you page

Hi I created custom thank you page for my COD (cash on delivery) payment option. Redirect code below working.
add_action( 'template_redirect', 'thankyou_custom_payment_redirect');
function thankyou_custom_payment_redirect(){
if ( is_wc_endpoint_url( 'order-received' ) ) {
global $wp;
// Get the order ID
$order_id = intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Set HERE your Payment Gateway ID
if( $order->get_payment_method() == 'cod' ){
// Set HERE your custom URL path
wp_redirect( home_url( '/custom-page/' ) );
exit(); // always exit
}
}
}
Now in my /custom-page/ I want to display same details as in Woocommerce order-details.php. I created custom-page.php with(/* Template Name: COD Thank you page */) for my Thank you Page, copied code from original Woocommerce order-details.php but just loads blank page. Can someone help me to do this correctly please.
Thank you
Ensure that your new order-details.php has a URL and that that URL is added inside of WooCommerce > Settings > Advanced > Order Received. In there you would add the slug of that URL of your new order-details page.
There are also easier ways to create a custom thank you page using plugins such as this one: https://wordpress.org/plugins/yith-custom-thank-you-page-for-woocommerce/

How to retrieve query list of post_type in WordPress?

I am developing a plugin and need the list of post types existing in the WordPress backend, I checked the WP_Query page of WordPress codex but could not find the solution
You can try this code.
<?php
// hook into init late, so everything is registered
// you can also use get_post_types where ever. Any time after init is usually
fine.
add_action( 'init', 'wpse34410_init', 0, 99 );
function wpse34410_init()
{
$types = get_post_types( [], 'objects' );
foreach ( $types as $type ) {
if ( isset( $type->rewrite->slug ) ) {
// you'll probably want to do something else.
echo $type->rewrite->slug;
}
}
}
i think it will work for you

WordPress: on submitting post check if post is updated

In WordPress with the publish_{post_type} (http://codex.wordpress.org/Post_Status_Transitions) you can hook into the loop if a post with custom type is published. But publish_{post_type} is also triggers if a a post is updated.
I am basically looking for a way to check the old and new status, within the publish_post_type hook. Does anyone have a nifty idea as to how to accomplish that?
I am doing this:
function doStuff( $post_ID ) {
// do stuff
return $post_ID;
}
add_action( 'publish_ttplaned', 'doStuff' );
So basically I need to check the old and new status of the post within the fuction doStuff().
In case someone else is looking for the aswer. I ended up using Williams solution.
add_action( 'draft_to_publish', 'doStuff', 10, 1 );
add_action( 'pending_to_publish', 'doStuff', 10, 1 );
function doStuff( $post ) {
if ( get_post_type( $post ) == "my_custom_post_type" ){
// do stuff
}
}
This works since a new post starts out as a draft even if you publish it right away.
As #rnevius says, there's no hook in WordPress core called publish_post_type.
There is also publish_post - which I've just tested it to make sure it only runs when the post is first published, not when it's subsequently edited.
So you can do something like:
function my_function($post_ID, $post) {
if ("foo" == $post->post_type) {
// do something
}
}
add_action( 'publish_post', 'my_function', 10, 2 );
There is also the wp_transition_post_status(), which calls several actions:
transition_post_status with $new_status, $old_status, $post
or, if you want to do it the other way around:
{$old_status}_to_{$new_status} and passes a WP_Post object as the only parameter.

update custom post custom field value when post is saved in wp-admin

How to update custom post custom field value when post is saved in admin?
I have tried to use this in misc.php for admin section:
add_action('pre_post_update', 'do_something_with_a_post');
function do_something_with_a_post($id) {
global $post;
update_post_meta($id, 'ct_Course_Dur_text_d19c', 'test12');
)
But it is not working.
You may try this (Using save_post hook), paste this code in your functions.php file
function save_cpt_metadata($id, $post)
{
if($post->post_type != 'your_custom_post_type') {
return;
}
update_post_meta($id, 'ct_Course_Dur_text_d19c', sanitize_text_field( $_POST['your_custom_field'] ) );
}
add_action('save_post', 'save_cpt_metadata');
In this example sanitize_text_field( $_POST['your_custom_field'] ) is would be actually the cstom field on your form but you may use any hard coded data, replace the your_custom_post_type with your real custom post type.

Resources