Skip to main content
Added a nonce to the request
Source Link
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:

$nonce = wp_create_nonce( 'my-action' );
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',
        'my_nonce' => $nonce
    ],
    'headers'   => [
        'cookie' => implode( '; ', $cookies ),
    ]
];
return wp_remote_post( $url, $args );

EDIT: For increased security, I added a nonce to the request, which is checking in the long-running function via wp_verify_nonce.

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 );

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:

$nonce = wp_create_nonce( 'my-action' );
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',
        'my_nonce' => $nonce
    ],
    'headers'   => [
        'cookie' => implode( '; ', $cookies ),
    ]
];
return wp_remote_post( $url, $args );

EDIT: For increased security, I added a nonce to the request, which is checking in the long-running function via wp_verify_nonce.

Source Link
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 );