M
M
Maxim Gribov2021-09-09 11:05:11
WordPress
Maxim Gribov, 2021-09-09 11:05:11

Can I put php into a wordpress variable?

Hello. I'm stumped as I can't find any information on this topic. I would even be glad to answer "You can do this" or it's unrealistic.
There is a code, simple, that creates a post in WP based on the user's responses. Now you need to send responses to post_content that depend on the user's response when filling out the form (the form is sent by a POST request.
I created a test variable in which I want to place the code that includes php. The html tags are published, but there is a problem with inserting php.

$test = '
<? if(IsChecked('questions', $gkt)) { ?>
   <h3 align="center" style="border: 2px solid #f9c6a9; padding: 10px; margin: 0 -2px 0 -2px; background-color: #f9c6a9;">Тут текст:
</h3>
    <ul class="reddotsli">
   <? if(IsChecked('questions','q1_a1')) {
    echo ' <li>Первый ответ</li>';
    }
    ?>
<? if(IsChecked('questions','q1_a2')) {
    echo ' <li>Второй ответ';
    }
    ?>
    <? if(IsChecked('questions','q1_a3' || 'q1_a4')) {
    echo ' <p><span class="reddot"></span>Третий ответ</p>';
    }
    ?>
....................
';


$post_data = array(
    'post_title' => 'Ответы',
    'post_content' => $test,
    'post_status' => 'private',
    'post_author' => $user_ID,
    'post_category' => [1],
    'comment_status' => 'closed',
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-09-09
@cordmax

$post_content = '';

if ( IsChecked( 'questions', $gkt ) ) {
  $post_content .= '<h3 class="title">Заголовок:</h3>';
  $post_content .= '<ul class="list">';
}

if ( IsChecked( 'questions', 'q1_a1' ) ) {
  $post_content .= '<li>Первый ответ</li>';
}

if ( IsChecked( 'questions', 'q1_a2' ) ) {
  $post_content .= '<li>Второй ответ</li>';
}

if ( IsChecked( 'questions','q1_a3' ) || IsChecked( 'questions', 'q1_a4' ) ) {
  $post_content .= '<li>Третий ответ</li>';
}

if ( IsChecked( 'questions', $gkt ) ) {
  $post_content .= '</ul>';
}

I don’t know what your function does IsChecked(), but the data from the form comes to the $_POST variable , it needs to be checked
In WordPress, it’s customary to name functions in lowercase with an underscore is_checked()
And you need to rewrite it all using a repeater so that there is no this web of checks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question