无法将php代码放入短代码的返回中

无法将php代码放入短代码的返回中,php,wordpress,shortcode,Php,Wordpress,Shortcode,我需要在wordpress帖子中隐藏下载的URL。我已经找到了一个很好的脚本来实现这一点,但它不是一个插件。我已经安装了脚本并创建了一个包含它的函数。我根本不是php的专家 但是,脚本通常有一行代码来调用它: <a href="<?php downloadurl('http://yourdomainname.comdownloadables.zip','veryspecials'); ?>" >Your Downloadables</a> 我不能把它直接

我需要在wordpress帖子中隐藏下载的URL。我已经找到了一个很好的脚本来实现这一点,但它不是一个插件。我已经安装了脚本并创建了一个包含它的函数。我根本不是php的专家

但是,脚本通常有一行代码来调用它:

<a href="<?php downloadurl('http://yourdomainname.comdownloadables.zip','veryspecials'); ?>" >Your Downloadables</a>

我不能把它直接放在帖子里,所以我试着给它编一个短代码,但是我被卡住了。我的短码是:

function secshort_func($atts, $content = null) {
extract(shortcode_atts(array(
    "linkurl" => '#Download_Does_Not_Exist',
    "linktitle" => 'Download',
), $atts));
return '<a href="<?php downloadurl(' .$linkurl. ','veryspecials'); ?>" >' .$linktitle. '</a>';
}
add_shortcode( 'secdown', 'secshort_func' );
函数secshort\u func($atts,$content=null){
提取(短码)附件(数组)(
“linkurl”=>“#下载(不存在)”,
“linktitle”=>“下载”,
)美元(附件);;
返回“”;
}
添加_短代码('secdown','secshort_func');
我在尝试运行时遇到错误,通过消除过程,我知道这是返回代码的这一部分:

"<?php downloadurl(' .$linkurl. ','veryspecials'); ?>"
“”
在互联网上搜索解决方案并尝试了我能想到的一切之后,我完全陷入了困境


任何帮助都将不胜感激——我被困在这样一件小事上简直快疯了

尝试使用双外引号和转义内单引号,如下所示:

return "<a href=\'<?php downloadurl(\'" . $linkurl . "\',\'veryspecials\'); ?>\' >" .$linktitle. '</a>';
返回“;

一些观察结果和答案:

  • 格式化您的代码。这使故障排除更加容易。良好的缩进是巨大的(请参阅下面的格式化代码)
  • 不要使用晦涩/缩写的函数名。键入它们以便创建
  • 最好不要使用extract。有些人说这没问题,但也有可能,这很难排除,因为你不知道变量来自何处。最好显式设置变量。(在你的情况下,因为你只使用了一次,所以最简单的方法是在数组形式中引用它们-
    $atts['link\u url']
  • 您可以调用函数,但必须将其连接到字符串中(请参见下文)。您的代码(以及其他答案)将php传递到字符串中,而不是调用函数并将结果传递到字符串中
  • 带答案的格式化代码:

    // Use a clearer function name.  No need for  "func", that's implied
    function download_link_shortcode($atts, $content = NULL) {  
        // Declare $defaults in a separate variable to be clear, easy to read
        $defaults = array(
            "link_url"   => '#Download_Does_Not_Exist',
            "link_title" => 'Download',
        );
    
        // Merge the shortcode attributes
        $atts = shortcode_atts( $defaults, $atts );
    
        // Concatenate in the results of the `download` function call....
        return '<a href="' . downloadurl( $atts['link_url'], 'veryspecials' ) . '">' . $atts['link_title'] . '</a>';
    }
    
    add_shortcode( 'secdown', 'download_link_shortcode' );
    
    //使用更清晰的函数名。这意味着不需要“func”
    函数下载链接短代码($atts,$content=NULL){
    //在单独的变量中声明$defaults,使其清晰易读
    $defaults=数组(
    “链接url”=>“#下载不存在”,
    “链接标题”=>“下载”,
    );
    //合并短代码属性
    $atts=短码_atts($defaults,$atts);
    //连接“下载”函数调用的结果。。。。
    返回“”;
    }
    添加_短代码('secdown','download_link_短代码');
    
    您的答案无效。我们不希望php调用出现在字符串中,我们希望下载URL调用的结果出现在字符串中。