Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Shortcodes Not Working #395

Open
ghost opened this issue Sep 2, 2016 · 5 comments
Open

Add Shortcodes Not Working #395

ghost opened this issue Sep 2, 2016 · 5 comments

Comments

@ghost
Copy link

ghost commented Sep 2, 2016

How i can add public shortcode.

I read the way of #262 but not works for me.
ShortCode adding - and dumping in global $shortcodes of wp - but not parsing.

in main class

    private function define_public_hooks() {

        $plugin_public = new Zuzas_rating_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' );

                 $this->loader->add_shortcode('rating', $plugin_public, 'rating');

    }

in public class

    public function enqueue_scripts() {

        /**
         * This function is provided for demonstration purposes only.
         *
         * An instance of this class should be passed to the run() function
         * defined in Zuzas_rating_Loader as all of the hooks are defined
         * in that particular class.
         *
         * The Zuzas_rating_Loader will then create the relationship
         * between the defined hooks and the functions defined in this
         * class.
         */

        wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/zuzas_rating-public.js', array( 'jquery' ), $this->version, false );

    }

    public function rating($atts, $content = ""){
        return 'blabla';
    }

in loader

    /**
     * The array of actions registered with WordPress.
     *
     * @since    1.0.0
     * @access   protected
     * @var      array    $shortcodes    The actions registered with WordPress to fire when the plugin loads.
     */
    protected $shortcodes;
...
    public function __construct() {

        $this->actions = array();
        $this->filters = array();
        $this->shortcodes = array();

    }
...
    /**
     * Add a new shortcode to the collection to be registered with WordPress
     *
     * @since     1.0.0
     * @param     string        $tag           The name of the new shortcode.
     * @param     object        $component      A reference to the instance of the object on which the shortcode is defined.
     * @param     string        $callback       The name of the function that defines the shortcode.
     */
    public function add_shortcode( $tag, $component, $callback, $priority = 10, $accepted_args = 2 ) {
        $this->shortcodes = $this->add( $this->shortcodes, $tag, $component, $callback, $priority, $accepted_args );
    }
...
    public function run() {

        foreach ( $this->filters as $hook ) {
            add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
        }

        foreach ( $this->actions as $hook ) {
            add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
        }

        foreach ( $this->shortcodes as $hook ) {

            add_shortcode(  $hook['hook'], array( $hook['component'], $hook['callback'] ));
        }

    }

@slushman
Copy link

slushman commented Sep 2, 2016

Hey @Aios,
I just tried out your code in a quick test plugin and its working for me. Not sure why it wouldn't work for you. What you have above is working though.

@DevinVinson
Copy link
Owner

If you have a full public repo I can look at it.

@navotera
Copy link

yes why in loader this library does'not include loader for adding shortcode ?

@navotera
Copy link

navotera commented Apr 21, 2017

i fix the shorcodes code by this code :

 public function add_shortcode( $tag, $component, $callback, $priority = 10, $accepted_args = 1) {
        $this->shortcodes = $this->add( $this->shortcodes, $tag, $component, $callback, $priority, $accepted_args );
    }

@anthonycoffey
Copy link

Thanks for sharing @navotera !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants