I'm new to Wordpress, and I am trying to make a condition to run something in save_post if a checkbox is enabled.

Right now my code is like this:

add_action( 'save_post', 'ac_verifica_links' );

function ac_verifica_links($post_id) {
    remove_action( 'save_post', 'ac_verifica_links' );
    update_post_meta( $post_id, 'ac_desabilitar_links', $_POST['ac_desabilitar_links']);
    if ($_POST['ac_desabilitar_links'] != "sim") {
        // do something
    add_action( 'save_post', 'ac_verifica_links' );

add_action( 'add_meta_boxes', 'ac_adiciona_checkbox' );
function ac_adiciona_checkbox() {
    add_meta_box('ac_desabilitar_links_id','Desabilitar Links?', 'ac_desabilitar_links_callback', 'post', 'normal', 'high');
function ac_desabilitar_links_callback( $post ) {
    global $post;
    $desabilitarLinks = get_post_meta( $post->ID, 'ac_desabilitar_links', true );

    <input type="checkbox" name="ac_desabilitar_links" value="sim" <?php echo (($desabilitarLinks == 'sim') ? 'checked="checked"': '');?>/> Sim

The meta post checkbox work as normal, saving correctly and all. But, for some reason, the conditional if ($_POST['ac_desabilitar_links'] != "sim") always returns true.

What exactly am I doing wrong here? I'm having a hard time debugging this, as gutenberg doesn't print anything from save_post.

Thanks for your time!

  • Did you check that $_POST['ac_desabilitar_links'] exists and is not empty? Use xdebug.
    – Lovor
    Dec 15, 2022 at 23:43


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.