Get $_POST & $_REQUEST values before adding/updating post

Question

I’ve done this many times before, but it stopped working and I can’t figure out why and haven’t found a way to re-accomplish the same task.

GOAL: intercept submitted values from $_POST/$_REQUEST before saving/updating post in DB. Specifically for processing both default WP meta/tax as well as custom meta/tax.

QUESTION: How do I get the $_POST/$_REQUEST data for ALL fields when a post is created or updated in WordPress. Everything that USED TO WORK no longer does. I’d like $_POST & $_REQUEST data so that I can process front-end form submissions, admin panel submissions, and quick edits.


HOOKS TRIED:

Hooks ‘pre_post_update’ SHOULD work, but always returns empty array, or the generic a:1:{s:7:"_locale";s:4:"user";} that are part of the wp-admin URL query string.

Hook ‘post_updated’ is after submission and does give old/new values, but only for default WP meta/tax, not custom meta/tax, and it’s after processing so no $_POST or $_REQUEST data.

Hook ‘save_post’ is after saving, so not suitable for the task.


DUMP VARS:

For every single one of these hooks, I’m saving a dump for each of these:

$post_id
$post_data
$_POST
$_GET
$_REQUEST
'php://input'

SAMPLE CODE TRIED:

pre_post_update hook:

function PPU_intercept_post_save($post_id, $post_data){
    // passed vars first
    file_put_contents('vardump.txt', serialize($post_id));
    file_put_contents('vardump.txt', PHP_EOL.serialize($post_data), FILE_APPEND);
    // rest data second
    $json_data = json_decode(file_get_contents('php://input'), true);
    file_put_contents('vardump.txt', PHP_EOL.serialize($json_data), FILE_APPEND);
    // server vars last
    file_put_contents('vardump.txt', PHP_EOL.serialize($_POST), FILE_APPEND);
    file_put_contents('vardump.txt', PHP_EOL.serialize($_GET), FILE_APPEND);
    file_put_contents('vardump.txt', PHP_EOL.serialize($_REQUEST), FILE_APPEND);
}
add_action( 'pre_post_update', 'PPU_intercept_post_save', 10, 2 );

result:

// post_id
i:118;
// post_data
a:21:{s:11:"post_author";s:1:"1";s:9:"post_date";s:19:"2022-06-08 15:45:49";s:13:"post_date_gmt";s:19:"2022-06-08 20:45:49";s:12:"post_content";s:821:"SENSITIVE_REMOVED";s:21:"post_content_filtered";s:0:"";s:10:"post_title";s:21:"SENSITIVE_REMOVED";s:12:"post_excerpt";s:0:"";s:11:"post_status";s:7:"publish";s:9:"post_type";s:13:"SENSITIVE_REMOVED";s:14:"comment_status";s:4:"open";s:11:"ping_status";s:6:"closed";s:13:"post_password";s:0:"";s:9:"post_name";s:21:"SENSITIVE_REMOVED";s:7:"to_ping";s:0:"";s:6:"pinged";s:0:"";s:13:"post_modified";s:19:"2022-06-11 12:45:20";s:17:"post_modified_gmt";s:19:"2022-06-11 17:45:20";s:11:"post_parent";i:0;s:10:"menu_order";i:0;s:14:"post_mime_type";s:0:"";s:4:"guid";s:59:"SENSITIVE_REMOVED&p=118";}
// rest data
a:2:{s:2:"id";i:118;s:7:"content";s:821:"SENSITIVE_REMOVED";}
// POST
a:0:{}
// GET
a:1:{s:7:"_locale";s:4:"user";}
// REQUEST
a:1:{s:7:"_locale";s:4:"user";}

save_post hook:

function SP_intercept_post_save( $post_id, $post_data, $update ) {
    // passed vars first
    file_put_contents('vardump.txt', serialize($post_id));
    file_put_contents('vardump.txt', PHP_EOL.serialize($post_data), FILE_APPEND);
    file_put_contents('vardump.txt', PHP_EOL.serialize($update), FILE_APPEND);
    // rest data second
    $json_data = json_decode(file_get_contents('php://input'), true);
    file_put_contents('vardump.txt', PHP_EOL.serialize($json_data), FILE_APPEND);
    // server vars last
    file_put_contents('vardump.txt', PHP_EOL.serialize($_POST), FILE_APPEND);
    file_put_contents('vardump.txt', PHP_EOL.serialize($_GET), FILE_APPEND);
    file_put_contents('vardump.txt', PHP_EOL.serialize($_REQUEST), FILE_APPEND);
}
add_action( 'save_post', 'SP_intercept_post_save', 10, 3 );

result:

// post_id
i:118;
// post_data
O:7:"WP_Post":24:{s:2:"ID";i:118;s:11:"post_author";s:1:"1";s:9:"post_date";s:19:"2022-06-08 15:45:49";s:13:"post_date_gmt";s:19:"2022-06-08 20:45:49";s:12:"post_content";s:821:"SENSITIVE_REMOVED";s:10:"post_title";s:21:"SENSITIVE_REMOVED";s:12:"post_excerpt";s:0:"";s:11:"post_status";s:7:"publish";s:14:"comment_status";s:4:"open";s:11:"ping_status";s:6:"closed";s:13:"post_password";s:0:"";s:9:"post_name";s:21:"SENSITIVE_REMOVED";s:7:"to_ping";s:0:"";s:6:"pinged";s:0:"";s:13:"post_modified";s:19:"2022-06-11 13:04:34";s:17:"post_modified_gmt";s:19:"2022-06-11 18:04:34";s:21:"post_content_filtered";s:0:"";s:11:"post_parent";i:0;s:4:"guid";s:59:"SENSITIVE_REMOVED&p=118";s:10:"menu_order";i:0;s:9:"post_type";s:13:"SENSITIVE_REMOVED";s:14:"post_mime_type";s:0:"";s:13:"comment_count";s:1:"0";s:6:"filter";s:3:"raw";}
// update
b:1;
// rest data
a:2:{s:2:"id";i:118;s:7:"content";s:821:"SENSITIVE_REMOVED";}
// POST
a:0:{}
// GET
a:1:{s:7:"_locale";s:4:"user";}
// REQUEST
a:1:{s:7:"_locale";s:4:"user";}

post_updated hook:

function PU_intercept_post_save($post_id, $post_after, $post_before){
    // passed vars first
    file_put_contents('vardump.txt', serialize($post_id));
    file_put_contents('vardump.txt', PHP_EOL.serialize($post_after), FILE_APPEND);
    file_put_contents('vardump.txt', PHP_EOL.serialize($post_before), FILE_APPEND);
    // rest data second
    $json_data = json_decode(file_get_contents('php://input'), true);
    file_put_contents('vardump.txt', PHP_EOL.serialize($json_data), FILE_APPEND);
    // server vars last
    file_put_contents('vardump.txt', PHP_EOL.serialize($_POST), FILE_APPEND);
    file_put_contents('vardump.txt', PHP_EOL.serialize($_GET), FILE_APPEND);
    file_put_contents('vardump.txt', PHP_EOL.serialize($_REQUEST), FILE_APPEND);
}
add_action( 'post_updated', 'PU_intercept_post_save', 10, 3 );

result:

// post_id
i:118;
// post_after
O:7:"WP_Post":24:{s:2:"ID";i:118;s:11:"post_author";s:1:"1";s:9:"post_date";s:19:"2022-06-08 15:45:49";s:13:"post_date_gmt";s:19:"2022-06-08 20:45:49";s:12:"post_content";s:821:"SENSITIVE_REMOVED";s:10:"post_title";s:21:"SENSITIVE_REMOVED";s:12:"post_excerpt";s:0:"";s:11:"post_status";s:7:"publish";s:14:"comment_status";s:4:"open";s:11:"ping_status";s:6:"closed";s:13:"post_password";s:0:"";s:9:"post_name";s:21:"SENSITIVE_REMOVED";s:7:"to_ping";s:0:"";s:6:"pinged";s:0:"";s:13:"post_modified";s:19:"2022-06-11 13:14:58";s:17:"post_modified_gmt";s:19:"2022-06-11 18:14:58";s:21:"post_content_filtered";s:0:"";s:11:"post_parent";i:0;s:4:"guid";s:59:"SENSITIVE_REMOVED&p=118";s:10:"menu_order";i:0;s:9:"post_type";s:13:"SENSITIVE_REMOVED";s:14:"post_mime_type";s:0:"";s:13:"comment_count";s:1:"0";s:6:"filter";s:3:"raw";}
// post_before
O:7:"WP_Post":24:{s:2:"ID";i:118;s:11:"post_author";s:1:"1";s:9:"post_date";s:19:"2022-06-08 15:45:49";s:13:"post_date_gmt";s:19:"2022-06-08 20:45:49";s:12:"post_content";s:821:"SENSITIVE_REMOVED";s:10:"post_title";s:21:"SENSITIVE_REMOVED";s:12:"post_excerpt";s:0:"";s:11:"post_status";s:7:"publish";s:14:"comment_status";s:4:"open";s:11:"ping_status";s:6:"closed";s:13:"post_password";s:0:"";s:9:"post_name";s:21:"SENSITIVE_REMOVED";s:7:"to_ping";s:0:"";s:6:"pinged";s:0:"";s:13:"post_modified";s:19:"2022-06-11 13:04:34";s:17:"post_modified_gmt";s:19:"2022-06-11 18:04:34";s:21:"post_content_filtered";s:0:"";s:11:"post_parent";i:0;s:4:"guid";s:59:"SENSITIVE_REMOVED&p=118";s:10:"menu_order";i:0;s:9:"post_type";s:13:"SENSITIVE_REMOVED";s:14:"post_mime_type";s:0:"";s:13:"comment_count";s:1:"0";s:6:"filter";s:3:"raw";}
// rest data
a:2:{s:2:"id";i:118;s:7:"content";s:821:"SENSITIVE_REMOVED";}
// POST
a:0:{}
// GET
a:1:{s:7:"_locale";s:4:"user";}
// REQUEST
a:1:{s:7:"_locale";s:4:"user";}

0
patrick34 6 months 2022-06-11T13:37:23-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse