Skip to main content
4 of 4
replaced http://stackoverflow.com/ with https://stackoverflow.com/

Theory

It might have been hard to word for you, but is actually quite clear what you are trying to accomplish.

A shortcode is a good way to insert dynamic content into a page (or post), the point at which your idea fails is the notion of a "dynamic shortcode".

Use the shortcode to insert the form into your content and have the callback handle what happens after submission.

Familiarize yourself with how forms are handled in PHP and with the difference between HTTP-POST and HTTP-GET (Resource 2, Resource 3).

Practice

Take the following as a proof of concept, rather than a copy/paste-ready solution:

Shortcode: [wpse_161632_sports_selection_form]

PHP:

<?php

/*
Plugin Name: WPSE_161632
Plugin URI: http://wordpress.stackexchange.com/questions/161632/
Description: Shortcode form handler example
Version: 1.0
Author: Johannes Pilkahn
License: GPL3
*/

/*  Copyright 2014  Johannes Pilkahn

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 3, as
    published by the Free Software Foundation.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


if ( ! class_exists( 'WPSE_161632' ) ) :

class WPSE_161632 {

    /**
     * Form handler
     *
     * @since 1.0
     * @access public
     * @see constructor
     */
    public function form_handler( $atts = '' )
    {
        // only relevant if the SC is supposed to have extra parameters
        extract( shortcode_atts( array(
            'example_attribute' => 1 
        ), $atts ) );

        if ( ( isset( $_POST['types'] ) ) {
            foreach( $_POST['types'] as $type ) {
                // do something
            }
            $this->output( true );
        } else {
            $this->output( false );
        }
    }

    /**
     * Outputs the form
     *
     * @since 1.0
     * @access public
     * @param bool $output
     */
    public function output( $submitted )
    {
        $options = array(
            array(
                'value' => 'skateboarding'
                'label' => __( 'Skateboarding', 'your-text-domain' )
            ),
            array(
                'value' => 'basketball'
                'label' => __( 'Basketball', 'your-text-domain' )
            ),
            array(
                'value' => 'hockey'
                'label' => __( 'Hockey', 'your-text-domain' )
            )
        );

        if ( $submitted ) {
            echo __( 'The form has been submitted!', 'your-text-domain' );
        }

        echo '<form action="#" method="post">';

        foreach ( $options as $option ) {
            echo '<input type="checkbox" '
                . 'value="' . $option['value'] . '" '
                . 'name="types[]" '
                . 'id="types_' . $option['value'] . '"';

            if ( $submitted && in_array( $option['value'], $_POST['types'] ) ) {
                echo ' checked="checked"';
            }
        
            echo ' /><label for="types_' . $option['value'] . '">'
                . $option['label'] . '</label>';
        }
        echo '</form>';
    }

    /**
     * Constructor
     *
     * @since 1.0
     * @access public
     */
    public function __construct() {
        add_shortcode(
            'wpse_161632_sports_selection_form',
            array( $this, 'form_handler' )
        );
    }

}

endif; // class exists

$wpse_161632 = new WPSE_161632;

?>
Johannes Pille
  • 11.1k
  • 3
  • 42
  • 53