1

Background: Creating a dynamic shortcode based on user inputs. This shortcode should change the output of do_shortcode based on the fields the user selects.

Example: On a page, a user selects

"Show me: (X) Skateboarding (X) Basketball ( ) Hockey"

When they click submit, the shortcode to do this should show as:

[shortcode_name types="Skateboarding, Basketball"]

and since Hockey wasn't selected, Hockey wouldn't be included.

That's what I'm trying to accomplish, I'm just having a hard time locking down how to actually do it.

2

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;

?>
| improve this answer | |
0

on click of your "Insert Shortcode" button, you'll want to use JavaScript/jQuery to see which checkboxes are checked, from which you'll create the Shortcode. after the shortcode is created, use window.send_to_editor('[shortcode]').

something like this in all:

$("#insert-shortcode").on('click',function(ev) {
    ev.preventDefault();
    var show = '';
    $(".checkboxes").each(function() {
        if ($(this).is(":checked"))
            show += $(this).val() + ",";
    });
    window.send_to_editor('[shortcode show="' + show.substring(0,(show.length-1)) + '"]');
});

not tested, but something like that should work. of course, adjust the selectors to your specific names. I made an assumption on several things, so if I skipped over a portion that you need further explaining, just let us know.

| improve this answer | |
  • 1
    Shortcodes are interpreted by PHP on the server side before serving the page to the client. How would JS/jQuery (or any other form of client side scripting) inserting a shortcode accomplish anything? – Johannes Pille Sep 16 '14 at 22:49
  • @JohannesPille from what I understood from the OP, you were asking how to generate the shortcode. your answer is quite thorough. up voted. waiting to see which was the actual question. – Caleb Sep 16 '14 at 23:00
  • Caleb, please comment on the question if you are unclear what the actual question is. Also please familiarize yourself with the editor buttons and how code is formatted. I don't want to edit each and every one of your answers. Thank you. – kaiser Sep 17 '14 at 0:49
  • @kaiser I had no reason to doubt what the question was, until Johannes answered differently. and I'm responding on iOS app: doesn't seem to be very many buttons. will take a look on desktop when I can. – Caleb Sep 17 '14 at 0:56
  • Just intend code with 4 spaces. Then you got the right formatting. – kaiser Sep 17 '14 at 1:02

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.