WP nonce invalid

时间:2019-09-06 作者:user2642724

Have a quick script to search for untranslated pages, and then returning a translate link. (Polylang makes a new post for a translation, which is then linked to the original.)

All is working well, except the nonce:

$url = admin_url(\'post-new.php?post_type=product&from_post=\' . $ID . \'&new_lang=\' . $lang);

$nonce_url = wp_nonce_url($url);

Returns a beautifully formatted url, with an invalid nonce:

https://yaddayadda.com/wp-admin/post-new.php?post_type=product&from_post=2851&new_lang=nl&_wpnonce=fb63ac7002

The link in the admin panel reads exactly the same, but with a working nonce:

https://yaddayadda.com/wp-admin/post-new.php?post_type=product&from_post=2851&new_lang=nl&_wpnonce=c17b1a3a2a

I like to think Im alright with WP, but this is breaking my head. Does anyone have any sort of idea why it is generating invalid links??

Full code:

require_once(\'../../../wp-load.php\');

$lang = $_POST[\'lang\'];
if ($lang == \'nl\') { $language = \'Dutch\';}
elseif ($lang == \'pt\') { $language = \'Portuguese\';}
else { $language = \'Something is going awry, I dont know \' . $lang;}

echo \'<h2>Missing \' . $language . \' translations</h2>\';
// An array of all published WC_Product Objects
$products = wc_get_products( array( \'status\' => \'publish\', \'limit\' => -1,\'lang\' => \'en\', ) );

// Displaying the number of products in this array
echo \'<p>Total number of products published: \' . sizeof( $products ) . \'</p>\';

// Loop through products and save some data using WC_Product and stuff in array
$tobearray = [];
$count = 0;
foreach ( $products as $product ){
    $ID = $product->get_id();
    $title = $product->get_title();

    if (!pll_get_post($ID, $lang)) {

                  $url = admin_url(\'post-new.php?post_type=product&from_post=\' . $ID . \'&new_lang=\' . $lang);

                  $nonce_url = wp_nonce_url($url);
                      //wp_nonce_url( $url);  // Adding our nonce to the url with a unique id made from the expiry timestamp

        $addpostlink = \'<a href="\' . wp_nonce_url(\'/wp-admin/post-new.php?post_type=product&from_post=\' . $ID . \'&new_lang=\' . $lang) . \'" target="_blank"> click here</a>\';


        $tobearray[] = [\'ID\' => $ID, \'Title\' => $title, \'Link\' => $addpostlink ];
        $count++;
        }
}
echo \'<p>Number of Products that are missing their \' . $lang . \' translation: \' . $count . \'</p>\';
echo \'Posts that need a \' . $language . \' translation: \';

?>
<p>
    Click on the link in the table below to create a new translation.  After the link opens (its a bit slow, pls wait), just open the original English product to copy and paste the title and description so you can translate them.
</p>
<table border="1">
<tr><th>ID</th><th>Title</th><th>Link</th></tr> 
<?php foreach($tobearray as $row) {
  echo(\'<tr>\');
  echo(\'<td>\');
  echo(implode(\'</td><td>\', $row));
  echo(\'</td>\');
  echo(\'</tr>\');
} ?>
</table>
1 个回复
最合适的回答,由SO网友:user2642724 整理而成

找出问题所在。Polylang插件验证nonce,因此需要向其传递“new post translation”参数。

解决方案是:wp\\u nonce\\u url($link,“new post translation”);

它可能会帮助某人,但可能不会;)