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.
function t_shortcode($atts, $content = null){
$lang = strtolower($atts['lang']);

$content = "<div id='translator' class='translate_".$lang."'>".$content."</div>";
if($lang == $_SESSION['language']):

    return $content;
endif;

}

I'm trying to get the content between the shortcodes, apply a function to it in index.php and the return back the $content of the shortcode with the divs included.

the second function that i'd like to apply to the shorcode function is:

function display($shortcodecontent, $noofchars){
$content2 = mb_substr($shortcodecontent,0,$noofchars);
return $content2;
}

so that I can use: // 13 is the number of characters

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The following adds the shortcode [my_t_shortcode], which accepts an attribute 'lang', and applys the above mentioned function to the content.

//Add shortcodes
add_shortcode( 'my_t_shortcode', 'wpse41477_shortcode_handler' );

//It's good practise to make sure your functions are prefixed by something unique
function wpse41477_shortcode_handler( $atts, $content = null ) {
       //This will extract 'lang' attribute as $lang. You can supply default values too.
       extract( shortcode_atts( array(
          'lang' => 'default_lang',
          ), $atts ) );
    //The above lowercases all values.

    /* Apply external function. 'display' is too generic, 
     * give it a unique prefix to prevent a clash with another plugin/theme/WordPress 
     */
    $content = wpse41477_display($content,13);
    //why not do $content = mb_substr($content,0,13); instead?

    //Apply divs
    $content = "<div id='translator' class='translate_".$lang."'>".$content."</div>";

    //Not sure what the following is for, but I've left it in.
    if($lang == $_SESSION['language']):
        return $content;
    endif;
    }

And the custom function:

function wpse41477_display($shortcodecontent, $noofchars){
     $content2 = mb_substr($shortcodecontent,0,$noofchars);
return $content2;
}

If that all wpse41477_display does, I would strongly recommend that you include it directly in the shortcode handler (see the comments).

The above code is not tested.

share|improve this answer
    
The thing is that I need to use a code like that in the index.php <? php wpse41477_display(13) ?> so it would display 13 characters of the content and apply the div on it from the first function –  Mohamed Said Feb 7 '12 at 10:23
    
@MohamedSaid - do you mean to say you want to apply this shortcode only on index.php? Is there a reason why you want to use mb_substr rather than the the_excerpt() with the length set to 13? (See link, section Control Excerpt Length using Filters). –  Stephen Harris Feb 7 '12 at 15:37
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.