1

I have tried creating a new class that holds an action which creates a shortcode and the shortcode is on my site but doesn't appear to be working. Can anyone tell me where I appear to be going wrong please?

The class is written in it's own PHP file located at site_URL/includes/frontend.registration-form.php

Dreamweaver isn't throwing any syntax errors?

    <?php



class RegistrationForm {

    function __construct (){
        $this->make_shortcode();
    }

    function make_shortcode (){
        add_shortcode ('Show Form', array($this, 'form_generate'));
    }

    function form_generate () {

        ob_start(); ?>

            <form class="form-horizontal">
                <fieldset>

                <!-- Form Name -->
                <legend>Form Name</legend>

                <!-- Text input-->
                <div class="form-group">
                  <label class="col-md-4 control-label" for="textinput">Name</label>  
                  <div class="col-md-4">
                  <input id="textinput" name="textinput" type="text" placeholder="Name" class="form-control input-md" required="">

                  </div>
                </div>

                <!-- Text input-->
                <div class="form-group">
                  <label class="col-md-4 control-label" for="textinput">Email Address</label>  
                  <div class="col-md-4">
                  <input id="textinput" name="textinput" type="text" placeholder="Email" class="form-control input-md" required="">

                  </div>
                </div>

                <!-- Password input-->
                <div class="form-group">
                  <label class="col-md-4 control-label" for="passwordinput">Password</label>
                  <div class="col-md-4">
                    <input id="passwordinput" name="passwordinput" type="password" placeholder="Password" class="form-control input-md" required="">

                  </div>
                </div>

                <!-- Text input-->
                <div class="form-group">
                  <label class="col-md-4 control-label" for="textinput">Store Name</label>  
                  <div class="col-md-4">
                  <input id="textinput" name="textinput" type="text" placeholder="Store Name" class="form-control input-md" required="">

                  </div>
                </div>

                </fieldset>
            </form>

    <?php $output = ob_clean();
    return ($output);

    }

} /*END OF CLASS*/

$new_form = new RegistrationForm;
return ($new_form);

?>
  • Is it included, initialized? E.g. via functions.php, because just putting it in a folder won't do it. – Nicolai May 30 '19 at 15:14
  • How do I initialize it? would that be a require_once call in the child theme functions.php file? – Dan Sutherland May 30 '19 at 15:23
  • That would work for example. Or you make it a plugin, but that's a whole other topic. – Nicolai May 30 '19 at 16:11
1

I don’t see any issue with the OOP code that would cause it not to work.

I think the problem is with the shortcode itself. I don’t believe shortcodes can have spaces. Show Form is not a valid shortcode name. [Show Form] would be parsed as as a shortcode named Show with an attribute Form.

The conventional format for this shortcode would be show_form.

  • Thank you for responding, I've updated that now but it still doesn't appear to make a difference. Do I have to require_once the class file anywhere as the php file with the class code is in a sub folder of the child theme? I appreciate that some of this functionality might be better loaded as a plugin but I wanted to try building it into a theme first. Thanks – Dan Sutherland May 30 '19 at 15:04
  • Shortcode API – Formal Syntax – Names, just to elaborate. – Nicolai May 30 '19 at 15:07
  • You do need to include it into your functions file, yes. The only PHP file WordPress will load automatically is functions.php. – Jacob Peattie May 31 '19 at 0:53

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.