This is my code

function ajax_layoutcontrol(){

    if( !check_ajax_referer( 'special-string','security')){
        return wp_send_json_error( 'Invalid nounce');
    }
    if (! current_user_can('manage_options')){
        return wp_send_json_error('You are not allowed to do this');
    }

    $orderwppl= $_POST['order']; // This is an array['phone', 'email']


        remove_shortcode( 'sideblock' );
        add_shortcode( 'sideblock', 'new_order' );

     function new_order(){

        foreach ($orderwppl as $item) {
            get_template_part( 'content', $item ); 
        }

     }

    wp_send_json_success( 'Post Saved');
    die();


}
add_action( 'wp_ajax_save_listing_order', 'ajax_layoutcontrol');

i am tring to change the shortcode

function old_order(){
        get_template_part( 'content', 'email' ); 
        get_template_part( 'content', 'phone' );
}

add_shortcode('sideblock', 'old_order');

But this is not working as ajax throws 500 error Please help

Is your $_Post returning the array as expected?

From what I'm seeing your add_action isn't calling the right function either...

Either change wpprolister_ajax_layoutcontrol in that line to ajax_layoutcontrol or rename the function to wpprolister_ajax_layoutcontrol

Another option may be to put a selectbox meta on the page and use that to call the correct format though. then you don't need to use POST data.

ie

function new_order() {
  $template_check = get_post_meta( $post->ID, 'template_check', true );
  get_template_part( 'content', $template_check ); 
}
  • Hi, sorry that typo, i have tested $_POST it is returning the array correctly. some thing is going wrong in removing the shortcode and updating it. sorry that ajax_layoutcontrol was a typo..it's still not working – Latheesh V M Villa Oct 1 at 22:43
  • why replace the shortcode? Just make a new one – rudtek Oct 1 at 22:46
  • Actually there is more stuff going on..There is a sortable list in the back-end and i am passing the list-id to $_POST by ajax and so foreach will change the shortcode accordingly( the template will change order). – Latheesh V M Villa Oct 1 at 22:52

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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