0

I added this snippet in my functions.php file but it doesn't execute the categories from foreach. It only shows the keywords field and it works fine but it's not showing the categories names.

function jobsform_function($atts) {  
    return '
      <form class="home-job-search" method="GET" action="https://website.com/jobs/jobs/">
        <div class="home-keywords">
          <input type="text" id="search_keywords" name="search_keywords" placeholder="Enter Keywords" />
        </div>
        <div class="home-categories">
          <select id="search_category" name="search_category">
              <?php foreach ( get_job_listing_categories() as $cat ) : ?>
                 <option value="<?php echo esc_attr( $cat->term_id ); ?>">
                    <?php echo esc_html( $cat->name ); ?>
                 </option>
              <?php endforeach; ?>
          </select>
        </div>
        <div class="home-search-button">
          <input type="submit" value="Search" />
        </div>
      </form>';
}
add_shortcode('jobsform', 'jobsform_function');

the HTML is not reading the foreach query

<form class="home-job-search" method="GET" action="https://design.muntomedia.com/jobs/jobs/">
            <div class="home-keywords">
                <input type="text" id="search_keywords" name="search_keywords" placeholder="Enter Keywords">
            </div>
            <div class="home-categories">
                <select id="search_category" name="search_category">
                    <!--?php foreach ( get_job_listing_categories() as $cat ) : ?-->
                        <option value="<?php echo esc_attr( $cat->term_id ); ?>">
                            <!--?php echo esc_html( $cat--->name ); ?>;
                        </option>
                    <!--?php endforeach; ?-->;
                </select>
            </div>
            <div class="home-search-button">
                <input type="submit" value="Search">
            </div>
        </form>

this is the result search form Any suggestions please?

3
  • Can you add the relevant part of the rendered HTML to your question?
    – kero
    Dec 15, 2020 at 9:27
  • I updated the post with the HTML Code Dec 15, 2020 at 10:16
  • 2
    The problem is the structure return '<somehtml><?php .. ?><morehtml>'; which is not possible. You're already inside PHP, no need to for the open and close tags.
    – kero
    Dec 15, 2020 at 10:24

1 Answer 1

2

You are returning the shortcode content as a big string, your function get_job_listing_categories is not properly being called.

Also i would recommend use PHP output buffering which is great for WordPress shortcodes.

Try below code:

function jobsform_function($atts) {
    ob_start();
    ?>
      <form class="home-job-search" method="GET" action="https://website.com/jobs/jobs/">
        <div class="home-keywords">
          <input type="text" id="search_keywords" name="search_keywords" placeholder="Enter Keywords" />
        </div>
        <div class="home-categories">
          <select id="search_category" name="search_category">
              <?php foreach (get_job_listing_categories() as $cat): ?>
                 <option value="<?php echo esc_attr($cat->term_id); ?>">
                    <?php echo esc_html($cat->name); ?>
                 </option>
              <?php endforeach;?>
          </select>
        </div>
        <div class="home-search-button">
          <input type="submit" value="Search" />
        </div>
      </form>
<?php
    $output = ob_get_contents();
    ob_end_clean();
    return $output;
}
add_shortcode('jobsform', 'jobsform_function');
1

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.