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

221
Visualizações
Is it risky to use wp_update_post with AJAX to send large blocks of HTML?

I am creating a front end editor that uses AJAX to update the page content.

It updates by using JS to take chunks of HTML components and save to the page content, wrapped in <!-- wp:html --> tags.

The function is only available if user is logged in and front-end editor option (custom theme option) is selected.

The AJAX code is:

function scedPage(post_id) {
    var pageComs = ["<!-- wp:html -->"];
    jQuery('section component').each(function () {
        $this0 = jQuery(this)[0].outerHTML;
        pageComs.push($this0);
    
    });
    pageComs.push("<!-- /wp:html -->");
    pageHtml = pageComs.join(' ');
    console.log(pageHtml);
    jQuery.ajax({
        type: "POST",
        url: ajax_object.ajaxurl,
        async: true,
        data: {
            action: 'sced_page',
            post_content: pageHtml,
            postId: post_id
        },
        success: function (data) {
            location.reload();
        },
        error: function (error) {
            console.log(error)
        }
    });
};

The PHP is:

function sced_page() {
    $dev_sced = get_option('scale_opt_field2');
    if (current_user_can('editor') || current_user_can('administrator')) {
        if ($dev_sced !== "1") {
        } else if ($dev_sced == "1") {
            $post_id = $_POST['postId'];
            $post_content = $_POST['post_content'];
    
            $the_post = array();
            $the_post['ID'] = $post_id;
            $the_post['post_content'] = $post_content;
    
            $post_id = wp_update_post($the_post);
            wp_die();
            add_action( 'wp_ajax_sced_page', 'sced_page' );
        }
    }
};

Does posting large blocks of unescaped/unfiltered HTML pose a problem? I realise I'll need to add nonce in there.

Thank you for your help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Does posting large blocks of unescaped/unfiltered HTML pose a problem?

TLDR;

Yes, there's a chance that your code could be exploited by malicious users so I'd sanitize the user's input using wp_kses_posts() before saving it to the database (or before rendering the content on the website).


Since you're checking for user capabilities and expect that they're either an editor or an administrator, which are roles you usually assign to people you trust, in theory it might sound OK to just save whatever they submit into the database.

Experience though has taught me -and many others out there will surely say the same thing- that you should never trust user's input. Sanitize/escape data whenever possible. Make this your personal mantra as a developer and save your Future Self some trouble.

$post_content = wp_kses_post($_POST['post_content']);

Not related to your original question but wanted to point out a couple of things about your code if that's OK:

  • Your first if condition isn't doing anything. Is that incomplete code or ...?
  • This add_action( 'wp_ajax_sced_page', 'sced_page' ); is never going to be executed: nothing after wp_die(); is going to be run by the server.
  • You should check whether $_POST['postId'] and $_POST['post_content'] are set using isset() before attempting to access them.
  • Yes, using a nonce would be a good idea :P
about 4 years ago · Juan Pablo Isaza Relatório
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