简体   繁体   English

PHP在Wordpress中的shortcode函数内部

[英]PHP inside shortcode function in Wordpress

I'm using one of the plugins inside Wordpress which needs to be rendered with PHP because I want to use it as shortcode inside WPbakery. 我使用的是Wordpress中需要使用PHP呈现的插件之一,因为我想将其用作WPbakery中的短代码。

Developer instructions for displaying in custom location is this line of code: 此行代码是开发人员在自定义位置显示的说明:

<?php wccf_print_product_field(array('key' => ‘mykey’)); ?>

I tried to add this in shortcode function like: 我试图将其添加到短代码功能中,例如:

function newshortcode1() {
wccf_print_product_field(array('key' => ‘mykey’));
}

add_shortcode( 'newshortcode', 'newshortcode1' );

But sadly this doesn't work no matter how I change this code. 但是可悲的是,无论我如何更改此代码,这都不起作用。

I'm using [newshortcode] insite post to display it. 我正在使用[newshortcode]现场帖子进行显示。

Any suggestions what I'm doing wrong here? 有什么建议我在这里做错了吗?

You are using ' (apostrophes) instead of ' (Single quotes). 您正在使用' (撇号)代替' (单引号)。

So, update from: 因此,从以下更新:

wccf_print_product_field(array('key' => ‘mykey‘)); 

to: 至:

wccf_print_product_field(array('key' => 'mykey'));

Firstly a shortcode function needs to return the desired output (It essentially replaces the shortcode text in the content). 首先,简码功能需要返回所需的输出(它实质上是替换内容中的简码文本)。 IF wccf_print_product_field returned the desired output, then you could do 如果wccf_print_product_field返回了所需的输出,则可以执行

return wccf_print_product_field

HOWEVER I strongly suspect that wccf_print_product_field prints or echo's the output directly when called. 但是,我强烈怀疑wccf_print_product_field在调用时直接打印或回显输出。 So it would be doing it when wordpress calls apply_filters to the page content, NOT returning it to the shortcode text. 因此,当wordpress对页面内容调用apply_filters而不是将其返回到短代码文本时,它将这样做。

So if you really must use it in a shortcode (if there is no other function that just returns the product field), then you will have to trap the output so you can return it in the shortcode. 因此,如果您真的必须在简码中使用它(如果没有其他函数仅返回乘积字段),那么您将必须捕获输出,以便可以在简码中返回它。 Something like: 就像是:

function newshortcode1() {
ob_start();  /* catch the output, so we can control where it appears in the text  */
wccf_print_product_field(array('key' => ‘mykey’));
$output .= ob_get_clean();
return $output;

If you view source on the page with the shortcode as it is now, I'd bet that you see your product field somewhere earlier in the page where it shouldn't be, possibly right at the start. 如果您现在在页面上使用简码查看源代码,那么我敢打赌,您会在页面的较早位置看到您的产品字段,该字段不应该出现在起始位置。

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

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