WordPress Shortcodes

Here it is a little schema to recap the creation of a wp shortcode

But how we can submit data through shortcodes?

Here it is.

you need to register two ajax function:

add_action('wp_ajax_nopriv_{save_your_posttype_function}', 'save_your_posttype_function'); // regular website visitor
add_action('wp_ajax_{save_your_posttype_function}', 'save_your_posttype_function'); // admin user

The submit action inside your shortcode must be something like this:

action="/wp-admin/admin-ajax.php?action=save_your_posttype_function" method="post"

and finally you can implement that function:

function slb_save_subscription() {
...
//after saving your data you have to return a json
if( $data_saved ):
  $result['status']=1;
  $result['message']='Data saved';
endif;

// encode result as json string
$json_result = json_encode( $result );

// return result
die( $json_result );

// stop all other processing
exit;

}
0 Comments

Add Your Comment: