wpstarter / framework

The WpStarter Framework - Laravel Framework for WordPress
https://wpstarter.dev
MIT License
15 stars 4 forks source link

Transmit multiple parameters in shortcodes #52

Closed fabarea closed 1 year ago

fabarea commented 1 year ago

Suppose we have a shortcode with two parameters, "foo" and "title". Currently, we can retrieve their values by adding arguments to the shortcode method. However, this approach has limitations. The parameters must be provided in the exact order, which can be inconvenient. Additionally, if we have a large number of parameters, it becomes cumbersome to write them all.

How can we improve? An associative array or an object to pass the shortcode parameters. This allows us to specify the parameter names along with their values, making it more flexible and easier to manage? I'm sure Laravel / you have some nice idea.

[welcome-shortcode foo="bar" title="world"]

    // actually the name of the parameter does matter as they are filled by order.
    public function shortcode($foo = '', $title = '') 
    {
        return shortcode_view('welcome.shortcode', [], [])->add('section1', function () {
            return 'welcome to section 1';
        });
    }

Using the latest version of wpstarter: v1.4.15

as247 commented 1 year ago

You may the shortcode routing param via ws_request()->route('title')