0

I'm trying to update my specific page using button in custom page on admin menu. I've tried using ajax to execute function in updatepost.php but the content of page no change. can any one help me?

this is code for button

<div>
  <input id="btn_news" class="add_content" type="button" class="button-primary" value="<?php esc_attr_e('Save News', 'mytheme'); ?>" />
  <input id="btn_aboutus" class="add_content" type="button" class="button-primary" value="<?php esc_attr_e('Save About Us', 'mytheme'); ?>" />
  <input id="btn_contact" class="add_content" type="button" class="button-primary" value="<?php esc_attr_e('Save Contact', 'mytheme'); ?>" />
</div>

this code for ajax

$(".add_content").click(function(){
        var BtnValue = $(this).val();
        $.ajax({
        type : 'POST',
        url : templateUrl+'/includes/updatepost.php',
        data:{action:'BtnValue'},
        success:function(html) {
             alert('success');
           }
        });
    });

and this my updatepost.php

<?php
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
        case 'Save News':
            add_content(303);
        break;
        case 'Save About Us':
            add_content(75);
        break;
        case 'Save Contact':
            add_content(30);
        break;
        }
    }

    function add_content($id_num){
        $update_page = array(
          'ID'           => $id_num,
          'post_content' => 'This is the updated content.',
        );
        wp_update_post( $update_page );
    }
?>

thank you for your time and help.

  • Did you try action: BtnValue instead of action:'BtnValue' ? – Samuel Elh May 21 '16 at 11:54
  • thank you for response, sory i miss for this, but I've tried and the alert 'success' is appear but the content of page is still no change – Manyang May 21 '16 at 12:21
2

No no no! Never POST or link directly to a custom PHP file - WordPress won't be loaded, and to load it manually yourself means making huge assumptions about the file hierarchy.

Use the ajax API, which exists specifically for this reason:

$.ajax({
    url: "<?php echo esc_js( admin_url( 'admin-ajax.php' ) ) ?>",
    type: "POST",
    data: {
        action: "update_button",
        button:  $( this ).val()
    }
});

And for your PHP:

function wpse_227396_update_button() {
    switch ( filter_input( INPUT_POST, 'button' ) ) {
        case 'Save News':
            add_content(303);
            break;
        case 'Save About Us':
            add_content(75);
            break;
        case 'Save Contact':
            add_content(30);
            break;
    }
}

add_action( 'wp_ajax_nopriv_update_button', 'wpse_227396_update_button' );
add_action( 'wp_ajax_update_button',        'wpse_227396_update_button' );
| improve this answer | |
  • haha, you are right, I need to follow the api, Before I think I still can do it without call admin-ajax.php, because the alert 'success' still appear and I dont understand how to use admin-ajax.php. Thank you for ur help and teach me how to use admin-ajax.php in my problem. Now it's work – Manyang May 22 '16 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.