Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress hook to edit the post meta data before displaying a post edit page

I'm trying to edit a field in the meta data of a post before it is being displayed on the screen.

I have been looking at the 'load-post.php' hook, but this is called before the post is loaded (if I've understood that correctly), so the post id and meta data are null. I've tried other hooks, but I haven't been able to make this work.

The following post meta field needs to be changed before it is displayed on the edit page.

$post_price = get_post_meta(get_the_ID(), 'price', TRUE);

Example: Price = 10 in the database, but I want it to be Price = 15 when it is displayed on the post edit page.

Any links, tips and ideas are much appreciated. :)

Edit:
My current solution:

add_action('load-post.php','calculate_price');
function calculate_price(){
    $post_id = $_GET['post'];
    //get price from post by post_id and do stuff
}

Is this the correct way?

like image 344
aboeka Avatar asked Nov 20 '25 07:11

aboeka


1 Answers

The best hook I found is load-post.php using $current_screen.

For Woocommerce product, that works :

add_action('load-post.php', "calculate_price" );

function calculate_price( ){
   global $current_screen;
   if( is_admin() && $current_screen->post_type === 'product' ){
       $post_id = (int) $_GET['post'];
       $post = get_post( $post_id );
       //Do something
   }
}
like image 110
J.BizMai Avatar answered Nov 22 '25 22:11

J.BizMai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!