Search code examples
phpwordpressfunctionshortcode

PHP how to put a function in a return


So! I am trying to make a simple shortcode to insert a table where I want it and I have everything working except I don't understand how to put the foreach loop in the return.

Basically I need all of this easily storeable in the dataTypeAllowed function so that it can be called for the shortcode add_shortcode('data_allowed', 'dataTypeAllowed');

Right here I tried just dropping in the foreach right into the return but of course I'm getting an error.

<?php

// Data Types Array
        $data_types_array = array( // the order: name, common reference, slug, ACF value, ACF label
        array('Personally Identifiable Information', 'PII', 'pii', $pii['value'], $pii['label']), 
        array('Protected Health Information', 'PHI', 'phi', $phi['value'], $phi['label']),
        );


function dataTypeAllowed() {
    
        return '<table>
                    <tr>
                        <th>Permission</th><th>Data Type</th><th>Common Reference</th>
                    </tr>' .
            
                    foreach($data_types_array as $data_type){
                echo '<tr>
                        <td' . $small_center . '>' . 
                            (($data_type[3] == 1) ? $allowed : '') . 
                            (($data_type[3] > 1 ) ? $questionable : '') . 
                            (($data_type[3] == 0) ? $not_allowed : '') . 
                        '</td>
                        <td> <a href="data-type/'. $data_type[2] . '">' . $data_type[0] . '</a>' .
                            (($data_type[3] > 1 ) ? '<br> <span style="font-size:11px">' . $data_type[4] . '</span>' : '') .
                        '</td>
                        <td>' . $data_type[1] . '</td>
                    </tr>'; } . 
                    '</table>';
}
    add_shortcode('data_allowed', 'dataTypeAllowed');

?>

Alternatively I thought I could wrap it all up in a variable but it also gives an error.

$foreach_data = foreach($data_types_array as $data_type){
                echo '<tr>
                        <td' . $small_center . '>' . 
                            (($data_type[3] == 1) ? $allowed : '') . 
                            (($data_type[3] > 1 ) ? $questionable : '') . 
                            (($data_type[3] == 0) ? $not_allowed : '') . 
                        '</td>
                        <td> <a href="data-type/'. $data_type[2] . '">' . $data_type[0] . '</a>' .
                            (($data_type[3] > 1 ) ? '<br> <span style="font-size:11px">' . $data_type[4] . '</span>' : '') .
                        '</td>
                        <td>' . $data_type[1] . '</td>
                    </tr>'; };



function dataTypeAllowed() {
    
        return '<table>
                    <tr>
                        <th>Permission</th><th>Data Type</th><th>Common Reference</th>
                    </tr>' .
                        $foreach_data
                     . 
                    '</table>';
}
    add_shortcode('data_allowed', 'dataTypeAllowed');

?>

Any suggestions?

edit 2:

Hmm I used your suggestion and cleaned up the code but it is only returning the table header row <table><tr><th>Permission</th><th>Data Type</th><th>Common Reference</th></tr>

Here is the block:

$allowed = '<img';
$not_allowed = 'img';
$questionable = 'img';

$data_types_array = array( // the order: name, common reference, slug, ACF value, ACF label
        array('Personally Identifiable Information', 'PII', 'pii', $pii['value'], $pii['label']), 
        array('Protected Health Information', 'PHI', 'phi', $phi['value'], $phi['label']));

function dataTypeAllowed(){
    $response = "";
    $response .= "<table>
        <tr>
            <th>Permission</th><th>Data Type</th><th>Common Reference</th>
        </tr>";
    
    foreach ($data_types_array as $data_type) {
        $response .= "<tr><td  align='center' width='60px' >";
        $response .= ($data_type[3] == 1) ? $allowed : '';
        $response .= ($data_type[3] > 1) ? $questionable : '';
        $response .= ($data_type[3] == 0) ? $not_allowed : '';
        $response .= "</td><td> <a href='data-type/'. $data_type[2] . ''>  $data_type[0] </a>";
        $response .= ($data_type[3] > 1) ? "<br> <span style='font-size:11px'> $data_type[4] </span>" : '';
        $response .= "</td><td> $data_type[1] </td> </tr>";
    }
    $response .= "</table>";
    return $response;
}
add_shortcode('data_allowed', 'dataTypeAllowed');


Solution

  • You need to do something like that:

    function dataTypeAllowed()
    {
        $response = "";
        $response .= "
        <table>
            <tr>
                <th>Permission</th><th>Data Type</th><th>Common Reference</th>
            </tr>
        ";
        foreach ($data_types_array as $data_type) {
            $response .= "<tr>
                    <td $small_center >";
            $response .= ($data_type[3] == 1) ? $allowed : '';
            $response .= ($data_type[3] > 1) ? $questionable : '';
            $response .= ($data_type[3] == 0) ? $not_allowed : '';
            $response .= "</td>
                    <td> <a href='data-type/'. $data_type[2] . ''>  $data_type[0] </a>";
            $response .= ($data_type[3] > 1) ? "<br> <span style='font-size:11px'> $data_type[4] </span>" : '';
            $response .= "</td>
                    <td> $data_type[1] </td>
                </tr>";
        }
        $response .= "</table>";
        return $response;
    }
    add_shortcode('data_allowed', 'dataTypeAllowed');
    

    And also you need to define all variables in your dataTypeAllowed() function. ($data_types_array, $small_center, $allowed, $questionable, $not_allowed)