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.66k stars 2.25k forks source link

Shortcode not work #504

Open DK-Web opened 5 years ago

DK-Web commented 5 years ago

Hi, I can not get the shortcodes to work. Have now tried all the comments and approaches, as well as tutorials and commits and changed everything several times, but it just will not work. Only the shortcode is displayed in the frontend.

Loader:

protected $shortcodes; 

public function __construct() {
        $this->actions = array();
        $this->filters = array();
        $this->shortcodes = array();
    }

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

public function run() {
...
foreach ( $this->shortcodes as $hook ) {
add_shortcode(  $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
}

}

Public:

public function define_hooks() {
        $this->loader->add_shortcode( 'testShortcode', $this->plugin_name, 'shortcode_function', $priority = 10, $accepted_args = 2 );
    }

    public function shortcode_function(){
        echo 'Test the plugin';
    }

Main Class:

private function define_public_hooks() {
            ...
            $this->loader->add_action( 'init', $plugin_public, 'define_hooks' );
        }

Does anyone find the mistake? Many thanks for your help!

indygill commented 5 years ago

Hey, here's how I have my shortcodes setup, I can confirm it works as expected.

In public/class-{plugin-name}-public.php

public function my_shortcode( $atts ) {
    return 'Hello World';
}

In includes/class-{plugin-name}-loader.php

protected $shortcodes;

public function __construct() {
    $this->actions    = array();
    $this->filters    = array();
    $this->shortcodes = array();
}

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

public function run() {
    foreach ( $this->shortcodes as $hook ) {
        add_shortcode( $hook['hook'], array( $hook['component'], $hook['callback'] ) );
    }
}

In includes/class-{plugin-name}.php

private function define_public_hooks() {
    $plugin_public = new Book_With_Me_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( 'my_shortcode_tag', $plugin_public, 'my_shortcode' );
}

Hope this helps.

adnoh commented 4 years ago

@indygill thank you. really helpfull if you start with this boilerplate or plugin development at all. should be part of the default boilerplate in my opinion or at least noted somewhere in which files you should look to extend the sample provided.

deshario commented 4 years ago

@indygill @DK-Web @adnoh Its seems like shortcode is not working[rendering] at backend or define_admin_hooks ... Is there something missing

michelnovellino commented 4 years ago

This not works

Hey, here's how I have my shortcodes setup, I can confirm it works as expected.

In public/class-{plugin-name}-public.php

public function my_shortcode( $atts ) {
    return 'Hello World';
}

In includes/class-{plugin-name}-loader.php

protected $shortcodes;

public function __construct() {
    $this->actions    = array();
    $this->filters    = array();
    $this->shortcodes = array();
}

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

public function run() {
    foreach ( $this->shortcodes as $hook ) {
        add_shortcode( $hook['hook'], array( $hook['component'], $hook['callback'] ) );
    }
}

In includes/class-{plugin-name}.php

private function define_public_hooks() {
    $plugin_public = new Book_With_Me_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( 'my_shortcode_tag', $plugin_public, 'my_shortcode' );
}

Hope this helps.

ekntrtmz commented 4 years ago

Hey, here's how I have my shortcodes setup, I can confirm it works as expected.

In public/class-{plugin-name}-public.php

public function my_shortcode( $atts ) {
    return 'Hello World';
}

In includes/class-{plugin-name}-loader.php

protected $shortcodes;

public function __construct() {
    $this->actions    = array();
    $this->filters    = array();
    $this->shortcodes = array();
}

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

public function run() {
    foreach ( $this->shortcodes as $hook ) {
        add_shortcode( $hook['hook'], array( $hook['component'], $hook['callback'] ) );
    }
}

In includes/class-{plugin-name}.php

private function define_public_hooks() {
    $plugin_public = new Book_With_Me_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( 'my_shortcode_tag', $plugin_public, 'my_shortcode' );
}

Hope this helps.

Thank you. This helps a lot :)

smarteseiten commented 3 years ago

Hey, here's how I have my shortcodes setup, I can confirm it works as expected.

In public/class-{plugin-name}-public.php

public function my_shortcode( $atts ) {
    return 'Hello World';
}

In includes/class-{plugin-name}-loader.php

protected $shortcodes;

public function __construct() {
    $this->actions    = array();
    $this->filters    = array();
    $this->shortcodes = array();
}

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

public function run() {
    foreach ( $this->shortcodes as $hook ) {
        add_shortcode( $hook['hook'], array( $hook['component'], $hook['callback'] ) );
    }
}

In includes/class-{plugin-name}.php

private function define_public_hooks() {
    $plugin_public = new Book_With_Me_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( 'my_shortcode_tag', $plugin_public, 'my_shortcode' );
}

Hope this helps.

Thank you, that did the trick!

rajucs commented 3 years ago

Hey, here's how I have my shortcodes setup, I can confirm it works as expected.

In public/class-{plugin-name}-public.php

public function my_shortcode( $atts ) {
    return 'Hello World';
}

In includes/class-{plugin-name}-loader.php

protected $shortcodes;

public function __construct() {
    $this->actions    = array();
    $this->filters    = array();
    $this->shortcodes = array();
}

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

public function run() {
    foreach ( $this->shortcodes as $hook ) {
        add_shortcode( $hook['hook'], array( $hook['component'], $hook['callback'] ) );
    }
}

In includes/class-{plugin-name}.php

private function define_public_hooks() {
    $plugin_public = new Book_With_Me_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( 'my_shortcode_tag', $plugin_public, 'my_shortcode' );
}

Hope this helps.

Thank you so much man worked like a charm with out got any error.

hurradieweltgehtunter commented 3 years ago

Thank you! Works like a charm.