Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
WP Custom REST Route - Wordpress wp_rest Nonce seems to have no influence

So I am setting up an AJAX call in Wordpress on the front-end and I came across the wp_create_nonce('wp_rest'). I read some stuff about it but I can't manage to understand the use of it since I can simply not use it and it still works perfectly fine. Can anyone explain me how it works and maybe tell me if I am doing something wrong? Also in the end I'd like to have my custom REST route to only be accessable by my system and not by calling the REST URL manually and also by guests and logged in users. But afaik that's what the Nonces are for?

My PHP Code:

add_action('wp_enqueue_scripts', 'enqueue_scripts');
function enqueue_scripts(){
    wp_enqueue_script( 'ajax-script', plugins_url( 'custom.js', __FILE__ ), array('jquery'), false, true );
    
    // in JavaScript, object properties are accessed as ajax_object.ajax_url
    wp_localize_script( 'ajax-script', 'ajax_object', array( 
        'restURL'  => rest_url(),
        'restNonce' => wp_create_nonce('wp_rest')
    ));
}

add_action('rest_api_init', function(){
    register_rest_route('wc/v3/', '/updateCartData/', array(
    'methods' => 'GET',
    'callback' => 'restAPI_updateData_callback'
    ));
});

function restAPI_updateData_callback(){ 
    $response = array();
    
    array_push($response, $_GET['test_data']);
    
    echo json_encode($response);
    
    // Genutzt um die Kontrolle zurück an den AJAX Call zu geben (notwendig?)
    // Siehe: https://anhkarppinen.com/die-and-wp-die-wordpress/
    die;
}

My JS/AJAX Code:

function updateData(){
    $.ajax({
        type: 'GET',
        url: ajax_object.restURL + 'wc/v3/updateCartData',
        beforeSend: function(xhr){
            xhr.setRequestHeader('X-WP-Nonce', ajax_object.restNonce);
        },
        data: {
            'test_data': 'test'
        },
        success: function (response) {
            if(response){
                alert(response);
            }
        },
        permission_callback: '__return_true'
    });
}

The JS Method is called like this within the Cart of Woocommerce:

<input id="user" type="number" step="1" min="1" max="5" value="1" title="Benutzeranzahl" size="4" placeholder="1" inputmode="numeric" autocomplete="off" onchange="updateData()">
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda