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 dividing my plugin code into multiple files and functions.

When I have a function and straight after it an action hook, or for example add_shortcode, should that line go straight after the function or in a plugin init function?

As an example, should I have something like this:

// Shortcodes.php file       
function myplugin_shortcode( $atts ) {    
    if ( !empty ($atts) ) {
        foreach ( $atts as $key => &$val ) {
            $val = html_entity_decode($val);
        }
    }
    myplugin_display_items( $atts );       
}

// Register shortcodes
add_shortcode( 'output-items', 'myplugin_shortcode');

OR a combination of the following two:

// Shortcodes.php file       
function myplugin_shortcode( $atts ) {    
    if ( !empty ($atts) ) {
        foreach ( $atts as $key => &$val ) {
            $val = html_entity_decode($val);
        }
    }
    myplugin_display_items( $atts );       
}

and

// Myplugin.php file
myplugin_init() {        
   // other initialisation code   
   add_shortcode( 'output-items', 'myplugin_shortcode');
}
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You do it like in your first example

// Shortcodes.php file       
function myplugin_shortcode( $atts ) {    
    if ( !empty ($atts) ) {
        foreach ( $atts as $key => &$val ) {
            $val = html_entity_decode($val);
        }
    }
    myplugin_display_items( $atts );       
}

// Register shortcodes
add_shortcode( 'output-items', 'myplugin_shortcode');
share|improve this answer
    
I agree. Unless you're wrapping your entire Plugin code inside a class or something, it's just fine to add callback/hook code pairs directly in the Plugin file, rather than inside another callback. –  Chip Bennett Aug 29 '12 at 13:31
    
Wouldn't it be handier to have all hook actions and callbacks in one place rather than having to go through a few files to hunt them down? –  drtanz Aug 29 '12 at 14:22
1  
Not sure I understand. Where you put the callbacks and add_shortcode calls is not really that relevant, as long as they're all included. Eg you can put them anywhere you like in your functions.php file, or in a plugin file, or in a file that's included by one of these files ... and the order is not relevant, unless I'm mistaken ... so you can still put them all in one place without putting the add_shortcode calls in an init –  byronyasgur Sep 3 '12 at 21:40
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.