Skip to main content
1 of 2
GeeC
  • 211
  • 2
  • 10

Thanks to the comment by Sally CJ, it turned out that the issue was that the user was not authenticated when invoking admin-post.php via wp_remote_post. The simplest solution was to ensure that the wp_remote_post had all the current cookies, as follows:

foreach ( $_COOKIE as $name => $value ) {
    $cookies[] = "$name=" . urlencode( is_array( $value ) ? serialize( $value ) : $value );
}
$url = admin_url( 'admin-post.php' );
$args = [
    'method'      => 'POST',
    'timeout'     => 50,
    'redirection' => 5,
    'blocking'    => true,
    'headers'     => [] ,
    'body'        => [
        'action' => 'gpc_nightly_job'
    ],
    'headers'   => [
        'cookie' => implode( '; ', $cookies ),
    ]
];
return wp_remote_post( $url, $args );
GeeC
  • 211
  • 2
  • 10