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');

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.


function new_order() {
  $template_check = get_post_meta( $post->ID, 'template_check', true );
  get_template_part( 'content', $template_check ); 
| improve this answer | |
  • 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 '18 at 22:43
  • why replace the shortcode? Just make a new one – rudtek Oct 1 '18 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 '18 at 22:52

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.