将php函数的输出连接成字符串

时间:2019-12-18 作者:fahad shaikh

我正在循环我的自定义帖子类型,并在字符串中创建一个表。循环完成后,我将回显字符串,以便它显示实际的表。

现在我想为每个自定义帖子类型添加一个删除按钮,为此,我使用Wordpress内置函数settings_fields(\'fahad_plugin_cpt_settings\');submit_button(\'Delete\'); 此函数生成一些HTML内容,我想将其存储在我正在创建的表字符串中。

现在的问题是,这些方法的输出不是在字符串内部串联,而是在表字符串外部生成。

Here is my code

<?php
                $options = get_option(\'fahad_plugin_cpt\') ?: array();

                $data = \'\';
                $data .= \'<table class="table">\';
                $data .= \'<thead>\';
                $data .= \'<tr>\';
                $data .= \'<th>ID</th>\';
                $data .= \'<th>Singular Name</th>\';
                $data .= \'<th>Plural Name</th>\';
                $data .= \'<th>Public</th>\';
                $data .= \'<th>Archive</th>\';
                $data .= \'<th>Actions</th>\';
                $data .= \'</tr>\';
                $data .= \'</thead>\';

                $data .= \'<tbody>\';
                foreach ($options as $option)
                    {
                        $public = isset($option[\'public\']) ? \'Yes\' : \'No\';
                        $has_archive = isset($option[\'has_archive\']) ? \'Yes\' : \'No\';

                        $data .= \'<tr>\';
                        $data .= \'<td>\'.$option[\'post_type\'].\'</td>\';
                        $data .= \'<td>\'.$option[\'singular_name\'].\'</td>\';
                        $data .= \'<td>\'.$option[\'plural_name\'].\'</td>\';
                        $data .= \'<td>\'.$public.\'</td>\';
                        $data .= \'<td>\'.$has_archive.\'</td>\';
                        $data .= \'<td>\';
                        //$data .= \'<button type="button" class="btn btn-success btn-sm">Edit</button>\';
                        $data .= \'<form method="post" action="options.php">\';
                        $data .= settings_fields(\'fahad_plugin_cpt_settings\');
                        $data .= submit_button(\'Delete\', \'btn btn-danger btn-sm\', \'submit\', false);
                        $data .= \'</form>\';
                        $data .= \'</td>\';
                        $data .= \'</tr>\';
                    }
                $data .= \'</tbody>\';
                $data .= \'</table>\';


            echo $data
            ?>

Actual String

\'<table class="table">
<thead>
    <tr>
        <th>ID</th>
        <th>Singular Name</th>
        <th>Plural Name</th>
        <th>Public</th>
        <th>Archive</th>
        <th>Actions</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>comicbook</td>
        <td>Comic Book</td>
        <td>Comic Books</td>
        <td>Yes</td>
        <td>Yes</td>
        <td>
            <form method="post" action="options.php"></form>
        </td>
    </tr>
    <tr>
        <td>product</td>
        <td>Product</td>
        <td>Products</td>
        <td>No</td>
        <td>No</td>
        <td>
            <form method="post" action="options.php"></form>
        </td>
    </tr>
</tbody>
\'

Output that should be concatenated inside the string

<input type="hidden" name="option_page" value="fahad_plugin_cpt_settings">
<input type="hidden" name="action" value="update">
<input type="hidden" id="_wpnonce" name="_wpnonce" value="465410aa16">
<input type="hidden" name="_wp_http_referer" value="/wordpress/wp-admin/admin.php?page=fahad_cpt">
<input type="submit" name="submit" id="submit" class="button btn btn-danger btn-sm" value="Delete">
<input type="hidden" name="option_page" value="fahad_plugin_cpt_settings">
<input type="hidden" name="action" value="update">
<input type="hidden" id="_wpnonce" name="_wpnonce" value="465410aa16">
<input type="hidden" name="_wp_http_referer" value="/wordpress/wp-admin/admin.php?page=fahad_cpt">
<input type="submit" name="submit" id="submit" class="button btn btn-danger btn-sm" value="Delete">
我知道我写代码的方式有问题,但我在google上查不出来,所以需要一些概念解释方面的帮助

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

可以使用如下输出缓冲:

$output_settings .= \'\';

ob_start();
settings_fields(\'fahad_plugin_cpt_settings\');
submit_button(\'Delete\', \'btn btn-danger btn-sm\', \'submit\', false);
$output_settings .= ob_get_clean();
和使用$output_settings 代码中的变量

// ...your code...
$data .= \'<form method="post" action="options.php">\';
$data .= $output_settings;
$data .= \'</form>\';
// ...your code...