0

I am new in WordPress, I want to write short code with parameters in function. That is in

add_shortcode('short code name','function name','parameter1,parameter2');

In add_shortcode(), I want to pass parameters and this must get in function used for creating short code. Here is the function

function function name('parameter1','parameter2') {

}

how is it possible?

1
  • I fail to see how this question is off topic?
    – Unkwntech
    Jun 27, 2011 at 6:50

2 Answers 2

1

According to the WordPress documentation, "a minimal example of the PHP code required to create shortcode with attributes" is

// [bartag foo="foo-value"]
function bartag_func( $atts ) {
    extract( shortcode_atts( array(
        'foo' => 'something',
        'bar' => 'something else',
    ), $atts ) );

    return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );

You can find more detailed information on creating shortcodes in the above referenced documentation.

1
  • i want to create short code dynamically with dynamic short code name and dynamic parameters.is it possible with the given code
    – user667030
    Jun 25, 2011 at 5:33
0

Like Keoki said, you can use the above the function and codex docs to help you. It is really just a matter of applying some programming skills to the concept.

For example, create a database table that holds the shortcode parameters, (shortcode name, parameter1, parameter2, etc.).

Then when the frontend is initialized, add your shortcodes dynamically.

Here is a rough concept:

add_action('init', 'dynamic_shortcodes');

    function dynamic_shortcodes() {
        global $wpdb;

        if(!is_admin()) {
            $sql = "SELECT shortcode_name, parameter1, parameter2 FROM {$wpdb->prefix}your_table_name";

            $result = $wpdb->get_results($sql);

            if($result) {
                foreach($result as $shortcode) {
                    add_shortcode($result->shortcode_name, 'my_dynamic_function');
                }
            }
        }
    }

The harder part is going to be writing the function for my_dynamic_function. Depending on what you want your shortcodes to do, the above code could help or maybe not. If it is completely dynamic functionality, then maybe not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.