WordPress-Plugin-Boilerplate icon indicating copy to clipboard operation
WordPress-Plugin-Boilerplate copied to clipboard

Shortcode not work

Open DK-Web opened this issue 6 years ago • 8 comments

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!

DK-Web avatar Jan 25 '19 22:01 DK-Web

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.

indygill avatar Feb 11 '19 22:02 indygill

@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.

adnoh avatar Oct 11 '19 09:10 adnoh

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

deshario avatar Jul 03 '20 08:07 deshario

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.

michelnovellino avatar Jul 10 '20 02:07 michelnovellino

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 :)

ekntrtmz avatar Sep 07 '20 14:09 ekntrtmz

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!

smarteseiten avatar Oct 10 '20 12:10 smarteseiten

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.

rajucs avatar Oct 26 '20 18:10 rajucs

Thank you! Works like a charm.

hurradieweltgehtunter avatar Jan 06 '21 13:01 hurradieweltgehtunter