Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
I fail to see how this question is off topic? –  Unkwntech Jun 27 '11 at 6:50
add comment

migrated from stackoverflow.com Jun 27 '11 at 7:00

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
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 '11 at 5:33
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.