简体   繁体   English

Wordpress简码中的PHP

[英]PHP within a Wordpress shortcode

I've got a PHP shortcode for Wordpress, let's call it [myshortcode]. 我有一个Wordpress的PHP短代码,我们称它为[myshortcode]。

The shortcode allows you to enter a download URL like this [myshortcode download="http://www.example.com/file.pdf"]. 简码允许您输入像这样的下载URL [myshortcode download =“ http://www.example.com/file.pdf”]。

I want to use this shortcode in a template file, but have the download url be a variable. 我想在模板文件中使用此短代码,但是将下载网址作为变量。 Is this possible, and if so, how do I do it? 这可能吗?如果可以,我该怎么做?

I tried these, but it doesn't work... 我尝试了这些,但不起作用...

<?php echo do_shortcode('[myshortcode download="<?php echo $variable['dllink']; ?>"]'); ?>
<?php echo do_shortcode('[myshortcode download="echo $variable['dllink'];"]'); ?>

Any ideas? 有任何想法吗?

<?php echo do_shortcode('[myshortcode download="'.$variable['dllink'].'"]'); ?>
<?php echo do_shortcode("[myshortcode download=\"{$variable['dllink']}\"]"); ?>

I suggest you read this properly to understand why those work and yours didn't ;-) 我建议您正确阅读此书,以了解为什么这些工作和您的工作不起作用;-)

I think it depends on where the variable is defined. 我认为这取决于在哪里定义变量。 For instance, if the shortcode is part of a plugin that allows the admin to set some options on a settings page, then you would want php to retrieve those options and echo the value into the shortcode. 例如,如果简码是允许管理员在设置页面上设置某些选项的插件的一部分,则您希望php检索这些选项并将值回显到简码中。

Another way to do it would be to use a form with a $_POST method to submit a value to a table in the database, and then to call that value and set it to the variable. 另一种方法是使用带有$ _POST方法的表单向数据库中的表提交值,然后调用该值并将其设置为变量。

In a recent plugin I wrote ( Recent Post Views ), I included a shortcode which retrieved options from the database and inserted those as variables: 在我写的最新插件( 最近发表的Views )中,我包含了一个短代码,该短代码从数据库中检索选项并将其作为变量插入:

//Get options from the settings page
$options = get_option('cb_rpv_options');
$select_bold = $options['select_bold'];
$select_italics = $options['select_italics'];
$select_list_style = $options['select_list_style'];

These variables changed the output of the shortcode, which is what you're trying to do. 这些变量更改了短代码的输出,这是您要尝试执行的操作。 The best thing to do may be to download a plugin with well commented code as an example and build off that. 最好的做法可能是下载带有注释良好的代码的插件作为示例,然后再进行构建。

I hope this is helpful, and I agree with the other post that you need to clean up your syntax too. 我希望这会有所帮助,并且我也同意另一篇文章,您也需要清理语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM