Get the content inside shortcode and apply external function to it?

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:

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

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

Related posts

Leave a Reply

1 comment

  1. 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.