在返回函数中添加php快捷代码

时间:2012-08-27 作者:agis

我有这部分代码:

return \'<img class="myimage \'.$text_color.\' \'.$horizontal.$slide_fitscreen.\'" src="\'.$image.\'" alt="" \'.$slide_desc.\' />\';
我想做的是返回一个php短代码,但不在img 元素,因为我的短代码生成a 元素,所以我需要在img之后返回它

这是我的短代码:

<?php echo do_shortcode(\'[daisy]\'); ?>
我试过这个:

return \'<img class="myimage \'.$text_color.\' \'.$horizontal.$slide_fitscreen.\'" src="\'.$image.\'" alt="" \'.$slide_desc.\' />\'.do_shortcode(\'[daisy]\');
但不起作用,我收到:

Error (Parseerror): Syntax Error Unexpected Token <
我该怎么做?谢谢稍后编辑:以下是整个脚本:

<?php 
function custom_slidessc($atts, $content = null) {

$user_agent = $_SERVER["HTTP_USER_AGENT"];
if(strpos($user_agent,\'iPhone\') || strpos($user_agent,\'iPod\') || strpos($user_agent,\'Android\')){ $mobile = true; }else{ $mobile = false; }
$ipad = strpos($user_agent, \'iPad\') ? true : false;

$class = shortcode_atts( array(\'text_color\' => \'#ffffff\', \'image\' => \'\', \'video_vimeo\' => \'\', \'video_youtube\' => \'\', \'video_mp4\' => \'\', \'video_ogg\' => \'\', \'video_webm\' => \'\', \'video_poster\' => \'\', \'slide_desc\' => \'\', \'slide_horizontal\' => \'\', \'slide_fitscreen\' => \'\', \'slide_noresize\' => \'\'), $atts );

    /* Slide Description */
    if($class[\'slide_desc\'] != \'\'){ 
        $slide_desc = $class[\'slide_desc\']; 
        $slide_desc = "title=\\"".$slide_desc."\\"";
    }

    /* Slide Text/Cursor Color */
    if($class[\'text_color\'] == \'#ffffff\'){
        $text_color = \'text_white\'; 
    }else{
        $text_color = \'text_black\'; 
    }

    /* ------------------------------------*/
    /* -------->>> IMAGE SLIDE <<<---------*/
    /* ------------------------------------*/
    if($class[\'image\'] != \'\'){
        $image = $class[\'image\'];
        if($class[\'slide_horizontal\'] == \'true\'){ $horizontal = \'slide_horizontal \'; }else{ $horizontal = \'\'; }
        if($class[\'slide_horizontal\'] == \'true\' || $class[\'slide_fitscreen\'] == \'true\'){ $slide_fitscreen = \'slide_fitscreen\'; }else{ $slide_fitscreen = \'\'; }
        return \'<img class="myimage \'.$text_color.\' \'.$horizontal.$slide_fitscreen.\'" src="\'.$image.\'" alt="" \'.$slide_desc.\' />\';

    /* ------------------------------------*/
    /* ----->>> HTML5 VIDEO SLIDE <<<------*/
    /* ------------------------------------*/
    }elseif(($class[\'video_mp4\'] != \'\' or $class[\'video_ogg\'] != \'\' or $class[\'video_webm\'] != \'\') AND !$mobile){
        $video_mp4 = $class[\'video_mp4\'];
        $video_ogg = $class[\'video_ogg\'];
        $video_webm = $class[\'video_webm\'];
        if($class[\'video_poster\'] != \'\'){ $video_poster = "poster=\\"".$class[\'video_poster\']."\\""; }else{ $video_poster = ""; }
        return \'<div class="myimage myvideo \'.$text_color.\'" \'.$slide_desc.\'>\'.do_shortcode(\'[video mp4="\'.$video_mp4.\'" ogg="\'.$video_ogg.\'" webm="\'.$video_webm.\'" \'.$video_poster.\' preload="true"]\').\'</div>\';

    /* ------------------------------------*/
    /* ------->>> YOUTUBE SLIDE <<<--------*/
    /* ------------------------------------*/
    }elseif($class[\'video_youtube\'] != \'\'){
        $video_youtube = $class[\'video_youtube\'];
        if($class[\'slide_noresize\'] = \'true\'){ $video_noresize = \'height="360" width="640"\'; }else{ $video_noresize = \'height="100%" width="100%"\'; }

        $strText = preg_replace( \'/(http|ftp)+(s)?:(\\/\\/)((\\w|\\.)+)(\\/)?(\\S+)?/i\', \'link\', $video_youtube );
        if($strText == \'link\'){
            $array_link_explode = explode(\'v=\', $video_youtube); 
            $array_link_explode = explode(\'&\', $array_link_explode[1]); 
            $video_youtube =$array_link_explode[0];
        }
        $video_poster = $class[\'video_poster\'];

        if($mobile){
            return \'<div class="myimage myvideo myvideo_yt \'.$text_color.\'"  \'.$slide_desc.\'><div style="height: 100%; width: auto; margin: 0 auto; position: relative;"><iframe style="bottom: 0; margin: auto; position: absolute; top: 0; z-index: 9999999999;" type="text/html" \'.$video_noresize.\' src="http://www.youtube.com/embed/\'.$video_youtube.\'?wmode=opaque" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></div><img src="\'.$video_poster.\'" alt="" style="position:absolute;" /></div>\';
        }else if($ipad){
            if($video_poster){ $ipad_poster = \'<img src="\'.$video_poster.\'" alt="" style="position:absolute;" />\'; }else{ $ipad_poster = \'\'; }
            return \'<div class="myimage myvideo myvideo_yt \'.$text_color.\'"  \'.$slide_desc.\' style="height: 100%;"><div style="height: 100%; width: 640px; margin: 0 auto; position: relative;"><iframe class="youtube-player" style="position:absolute;top:0;bottom:0;margin:auto;z-index: 77777777;" type="text/html" \'.$video_noresize.\' src="http://www.youtube.com/embed/\'.$video_youtube.\'?wmode=opaque" frameborder="0"></iframe></div>\'.$ipad_poster.\'</div>\';
        }else if($video_poster != \'\'){
            return \'<div class="myimage myvideo myvideo_yt \'.$text_color.\'"  \'.$slide_desc.\' style="height: 100%;"><div style="height: 100%; width: 640px; margin: 0 auto; position: relative;"><iframe class="youtube-player" style="position:absolute;top:0;bottom:0;margin:auto;z-index: 77777777;" type="text/html" \'.$video_noresize.\' src="http://www.youtube.com/embed/\'.$video_youtube.\'?wmode=opaque" frameborder="0"></iframe></div><img src="\'.$video_poster.\'" alt="" style="position:absolute;" /></div>\';
        }else if(!$ipad){ //disable fullscreen for ipad
            return \'<div class="myimage myvideo myvideo_yt \'.$text_color.\'" \'.$slide_desc.\'><iframe class="youtube-player" type="text/html" width="100%" height="100%" src="http://www.youtube.com/embed/\'.$video_youtube.\'?wmode=opaque" frameborder="0"></iframe></div>\';
        }

    /* ------------------------------------*/
    /* -------->>> VIMEO SLIDE <<<---------*/
    /* ------------------------------------*/
    }elseif($class[\'video_vimeo\'] != \'\'){
        $video_vimeo = $class[\'video_vimeo\'];
        if($class[\'slide_noresize\'] = \'true\'){ $video_noresize = \'height="360" width="640"\'; }else{ $video_noresize = \'height="100%" width="100%"\'; }

        $strText = preg_replace( \'/(http|ftp)+(s)?:(\\/\\/)((\\w|\\.)+)(\\/)?(\\S+)?/i\', \'link\', $video_vimeo );
        if($strText == \'link\'){ 
            $array_link_explode = explode(\'.com/\', $video_vimeo); 
            $video_vimeo = $array_link_explode[1]; 
        }
        $video_poster = $class[\'video_poster\'];

        if($mobile){
            return \'<div class="myimage myvideo myvideo_yt \'.$text_color.\'"  \'.$slide_desc.\'><div style="height: 100%; width: auto; margin: 0 auto; position: relative;"><iframe style="bottom: 0; margin: auto; position: absolute; top: 0; z-index: 9999999999;" src="http://player.vimeo.com/video/\'.$video_vimeo.\'?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff" \'.$video_noresize.\' frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></div><img src="\'.$video_poster.\'" alt="" style="position:absolute;" /></div>\';
        }else if($ipad){
            return \'<div class="myimage myvideo myvideo_yt \'.$text_color.\'"  \'.$slide_desc.\' style="height: 100%;"><div style="height: 100%; width: 640px; margin: 0 auto; position: relative;"><iframe style="bottom: 0; margin: auto; position: absolute; top: 0; z-index: 77777777;" src="http://player.vimeo.com/video/\'.$video_vimeo.\'?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff" \'.$video_noresize.\' frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></div><img src="\'.$video_poster.\'" alt="" style="position:absolute;" /></div>\';
        }else if($video_poster != \'\'){
            return \'<div class="myimage myvideo myvideo_yt \'.$text_color.\'"  \'.$slide_desc.\' style="height: 100%;"><div style="height: 100%; width: 640px; margin: 0 auto; position: relative;"><iframe style="bottom: 0; margin: auto; position: absolute; top: 0; z-index: 77777777;" src="http://player.vimeo.com/video/\'.$video_vimeo.\'?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff" \'.$video_noresize.\' frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></div><img src="\'.$video_poster.\'" alt="" style="position:absolute;" /></div>\';
        }else if(!$ipad){ //disable fullscreen for ipad
            return \'<div class="myimage myvideo myvideo_yt \'.$text_color.\'" \'.$slide_desc.\'><iframe src="http://player.vimeo.com/video/\'.$video_vimeo.\'?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff" width="100%" height="100%" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></div>\';
        }
    }else{
        return false;
    }
}
add_shortcode("slide", "custom_slidessc");
?>

2 个回复
最合适的回答,由SO网友:fuxia 整理而成

请勿致电do_shortcode(). 请改用关联的处理程序。

如果要调用不带参数的短代码,则必须在处理程序声明中使参数为可选的:

function custom_slidessc($atts = array(), $content = null)
因此,您需要的是:

return \'<img>\' . custom_slidessc();

SO网友:Edward Caissie

do_shortcode 需要回显以将其输出打印到屏幕上,因此您需要找到一种解决方法。这里有一个想法:

// Start an output buffer to capture the echo
ob_start();

echo do_shortcode( \'[daisy]\' );

//Put the output into a usable variable */
$daisy_output = ob_get_contents();

//End the output buffer capture
ob_end_clean();

return \'<img class="myimage \' . $text_color . \' \' . $horizontal . $slide_fitscreen . \'" src="\' . $image . \'" alt="" \' . $slide_desc . \' />\' . $daisy_output;
可能有更好的方法,但至少这应该为您试图获得的输出提供概念证明。

结束

相关推荐

Shortcode perfomance solution

我有一个对性能至关重要的站点。在使用短代码时,我考虑了服务器解析和返回内容所需的时间,而不是允许一些HTML(<div class=\"redletter\">) 浏览TinyMCE编辑器。与此同时,我的代码是:// [redletter]Content[/redletter] add_shortcode(\'redletter\', \'redletter_do\'); function redletter_do($atts, $content = null) {