Brandon Powell
Brandon Powell

Reputation: 105

Wordpress Shortcode Issus v3

I want to see if him doing this code correctly to developed a custom shortcode for contact form want to create for my website.

I have looked at wp codex want to use class so I came up with this code to if this would work.

<?php

class MyPlugin {
      // Conact Form shortcode

    public static function allb_contact_form( $atts, $content = " "  ) {
          //[contact_form]
          //get the attribute_escape
          $atts = shortcode_atts(
            array(),
            $atts,
            'contact_form'
          );

          //return HTML
          ob_start();
          include 'lib/inc/thmeplates/contact-form.php';
          return ob_get_clean();
    }
}
  add_shortcode( 'contact_form', array( $this , 'allb_contact_form' ) );

But it doesn't give me error messages that says. One guy told me to put the add_shortcode( 'contact_form', array( $this , 'allb_contact_form' ) ); at the outside of code.

Upvotes: 0

Views: 27

Answers (1)

yivi
yivi

Reputation: 47568

If you are intent on using a class, wherever you are declaring your plugin add

$myplugin = new MyPlugin();
add_shortcode( 'contact_form', [ $myplugin , 'allb_contact_form' ] );

And remove that add_shortcode from the class. It's not even valid code laying there.

Upvotes: 1

Related Questions