DevinVinson / WordPress-Plugin-Boilerplate

[WordPress] A foundation for WordPress Plugin Development that aims to provide a clear and consistent guide for building your plugins.
http://wppb.io
7.67k stars 2.25k forks source link

Add multiple shortcodes with a class #317

Closed dimitrilongo closed 9 years ago

dimitrilongo commented 9 years ago

To add multiple shortcode i use https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/262 it works really great. But i need to add more that 20 shortcodes.

in class-plugin-name.php

private function define_public_hooks() {

        $plugin_public = new Plugin_Name_Public( $this->get_plugin_name(), $this->get_version() );

        $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
        $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );

        $shortcodes_array = array(
                                '0' => array(
                                            'funct'     => 'shortcode_1',
                                            'id'        => 'shortcode_1',
                                        ),
                                '1' => array(
                                            'funct'     => 'shortcode_2',
                                            'id'        => 'shortcode_2',
                                        ),
        );
        foreach ( $shortcodes_array as $id => $data ) {

            $funct = $data['funct'];
            $id = $data['id'];

            $this->loader->add_shortcode( $id, $plugin_public, $funct );
        }
    }

and in class-plugin-name-public.php

public function shortcode_1($atts, $content = "") {

        return "shortcode_1 = $content";
    }

public function shortcode_2($atts, $content = "") {

        return "shortcode_2 = $content";
    }

so for 20 shortcodes i will have 20 functions in class-plugin-name-public.php some can be very long. i'd like to require a class with all shortcodes functions, but don't know how. because in class-plugin-name.php it calls function name located in in class-plugin-name-public.php Hope i'm clear

DevinVinson commented 9 years ago

You can add your class as a dependency to the main includes class. Then you would add the functions inside of it just like you would have if they were in the main public class.

dimitrilongo commented 9 years ago

thanks @DevinVinson i will try that. I close that subject