简体   繁体   English

PHP函数回显包含文件

[英]PHP Function echo included file

I'm currently developing a wordpress plugin. 我目前正在开发一个wordpress插件。 I created a shortcode which displays the content of another html/php file this looks like this: 我创建了一个简码来显示另一个html / php文件的内容,如下所示:

function df_display_form()
{
    // Fetching some data with $wpdb

    // Display the data
    include_once plugin_dir_path(__FILE__) . 'markup/show-dynamic-form.php';
}

My problem is that the shortcode will be showed at the top of the page. 我的问题是,简码将显示在页面顶部。 So I googled this issus and found a solution . 所以我用谷歌搜索了这个问题并找到了解决方案 As it is writte there the problem is that return should be used instead of echo . 因为它是书面的,问题在于应该使用return而不是echo

So my question is how can I return the renderd content from the included file? 所以我的问题是如何从包含的文件中返回渲染的内容? (not echo). (不是回声)。

Try using an output buffer. 尝试使用输出缓冲区。 https://www.php.net/manual/en/function.ob-get-clean.php https://www.php.net/manual/en/function.ob-get-clean.php

function df_display_form()
{
    ob_start(); 
    // Fetching some data with $wpdb

    // Display the data
    include_once plugin_dir_path(__FILE__) . 'markup/show-dynamic-form.php';
    $out = ob_get_clean();
    return $out;
}

You have to use php fuction to display the form using shortcode. 您必须使用php函数以使用简码显示表单。

I think you have currently this type of code in show-dynamic-form.php file. 我认为您目前在show-dynamic-form.php文件中拥有这种类型的代码。

<?php

// Some codes

?>

<form>

Form elements

</form>

It will just echo your code to the top of your post or page. 它只会将您的代码回显到帖子或页面的顶部。 Correct way is: 正确的方法是:

<?php

    // Some codes

    $variable = '<form>';
    $variable .= 'form elements';
    $variable .= '</form>';

    return $variable;

?>

Try to use this way in your dynamic-form.php file and it should work. 尝试在您的dynamic-form.php文件中使用这种方式,它应该可以工作。

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

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