0

I am looping through my custom post types and creating a table inside the string. Once the loop is done I will echo the string so it will show the actual table.

Now I want to add a delete button for each custom post type, for this, I am using the Wordpress built-in function settings_fields('fahad_plugin_cpt_settings'); and submit_button('Delete'); This function is generating some HTML content which I want to store inside the table string I am creating.

Now the problem is the output of those methods are not being concatenated inside the string but generated outside the table string.

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">

I know my there is a problem with my way of writing code but I couldn't figure it out after googling so need help with some concept explanation

1

1 Answer 1

4

You can use output buffering like:

$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();

and use $output_settings variable in your code like

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.