0

I'm trying to create an array of posts based on a form in the events admin page + two arrays. The code runs fine, but for some reason, the is causing some sort of resource issue that I can't figure out:

    <?php
// Render the custom meta box content
function render_events_custom_meta_box() {
    global $post_type;

    if ('event' == $post_type) {
        echo "<div class='updated'>";
        echo '<p>';
        echo 'Vizsga időpontok létrehozása:';

        // Retrieve selected options
        $selected_language = isset($_GET['selected_language']) ? sanitize_text_field($_GET['selected_language']) : '';
        $selected_option = isset($_GET['selected_option']) ? sanitize_text_field($_GET['selected_option']) : '';
        $selected_date = isset($_GET['selected_date']) ? sanitize_text_field($_GET['selected_date']) : '';

        $insert_url = add_query_arg(
            array(
                'action' => 'insert_event_posts',
                'selected_language' => $selected_language,
                'selected_option' => $selected_option,
                'selected_date' => $selected_date,
            ),
            admin_url('admin-post.php')
        );

        echo "<form method='post' id='event-form' action='" . esc_url($insert_url) . "'>";
        // Define arrays
        $languages = array('Magyar', 'Német', 'Olasz');
        $options = array('egynyelvű', 'kétnyelvű', 'Rendészeti egynyelvű');
        $levels = array('B1', 'B2', 'C1');
        $types = array('Papir', 'Számítógépes vizsgatermi', 'Sajátgépes vizsgatermi', 'Számítógépes Online');

        // Language dropdown
        echo '<label for="selected_language">Language:</label>';
        echo '<select id="selected-language" name="selected_language">';
        foreach ($languages as $language) {
            echo '<option value="' . $language . '"' . ($selected_language == $language ? 'selected="selected"' : '') . '>' . $language . '</option>';
        }
        echo '</select>';
        echo '<br>';

        // Option dropdown
        echo '<label for="selected_option">Option:</label>';
        echo '<select name="selected_option" id="selected-option">';
        foreach ($options as $option) {
            echo '<option value="' . $option . '"' . selected($selected_option, $option, false) . '>' . $option . '</option>';
        }
        echo '</select>';
        echo '<br>';

        // Date picker
        echo '<label for="selected_date">Date:</label>';
        echo '<input type="date" name="selected_date" id="selected-date" value="' . $selected_date . '">';
        echo '<br>';
        echo "<button type='submit' class='button button-primary' id='submit-button' style='margin:0.25em 1em'>Nyomj meg</button>";
        echo '</form>';

        // Retrieve selected options (removed from here)

        

        echo '</div>';
        ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
    var dropdowns = ['selected-language', 'selected-option', 'selected-date'];

    dropdowns.forEach(function(dropdownId) {
        var dropdown = document.getElementById(dropdownId);

        dropdown.addEventListener('change', function() {
            var selectedLanguage = document.getElementById('selected-language').value;
            var selectedOption = document.getElementById('selected-option').value;
            var selectedDate = document.getElementById('selected-date').value;

            console.log(selectedLanguage);

            var insertUrl =
                '/wp-admin/edit.php?post_type=event&action=insert_event_posts' +
                '&selected_language=' + selectedLanguage +
                '&selected_option=' + selectedOption +
                '&selected_date=' + selectedDate;

            console.log(insertUrl);


            document.getElementById('event-form').action = insertUrl;
        });
    });
});
</script>
<?php
    }
}
add_action('admin_notices', 'render_events_custom_meta_box');


// Handle form submission and create new posts
function insert_event_posts() {
    // Retrieve selected options
    $selected_language = isset($_GET['selected_language']) ? sanitize_text_field($_GET['selected_language']) : '';
    $selected_option = isset($_GET['selected_option']) ? sanitize_text_field($_GET['selected_option']) : '';
    $selected_date = isset($_GET['selected_date']) ? sanitize_text_field($_GET['selected_date']) : '';

    // Define arrays
    $levels = array('B1', 'B2', 'C1');
    $types = array('Papir', 'Számítógépes vizsgatermi', 'Sajátgépes vizsgatermi', 'Számítógépes Online');

    // Create new posts for all variations
    foreach ($levels as $level) {
        foreach ($types as $type) {
            $post_title = $selected_language . ' ' . $selected_option . ' ' . $level . ' ' . $type;

            $post_data = array(
                'post_title' => $post_title,
                'post_type' => 'event',
                'post_status' => 'publish',
            );

            // Insert new post
            wp_insert_post($post_data);
        }
    }

    // Redirect to the page URL where the function was executed
    // wp_safe_redirect(wp_get_referer());
    // exit;
}
add_action('admin_post_insert_event_posts', 'insert_event_posts'); 


?>

If I comment out the script, the functions run, and the posts get created, but the Language and Option parameter are left out of the titles.

If I leave the script in, the error I get is:

The link you followed has expired. Please try again.

*And no posts are created at all.

This is an example of the ending url: /wp-admin/edit.php?post_type=event&action=insert_event_posts&selected_language=Német&selected_option=kétnyelvű&selected_date=2023-05-28

Might anyone have any ideas what might be causing the overload?

Thanks!

2
  • I see that none of your form inputs are inside the form tag except for the submit button, is that expected? It's very likely that the link has expired issue is unconnected to the missing values in the title, and much more likely that wp_safe_redirect(wp_get_referer()); is redirecting to a URL that has already expired, and that the referrer will never work as a value here for that reason. Also that ending URL has URL parameters, aka GET parameters, not POST parameters. If it were using POST then selected_language=Német would not appear in the URL, that's not how forms work
    – Tom J Nowell
    May 25 at 11:25
  • Hi Tom, Thanks for the input. I updated the code based on your suggestions, but the issue is unfortunately still apparent.
    – Pbalazs89
    May 25 at 11:59

1 Answer 1

0

Figured it out, the issue was the beginning of the url,

/wp-admin/admin-post.php?action=insert_event_posts

instead of:

/wp-admin/edit.php?post_type=event&action=insert_event_posts

Thanks for the help!

Your Answer

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

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