WordPress Shortcodes
Here it is a little schema to recap the creation of a wp shortcode
1. Hook
2. Shortcode registration
3. Shortcode content
1. Hook
add_action('init','callback_register_shortcode');
2. Shortcode registration
function callback_register_shortcode()
{
add_shortcode('shortcode_name','sc_content_function');
}
3. Shortcode content
function sc_content_function($args, $content="")
{
$output="<div> shortcode html content</div>";
echo $output;
}
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;
}