1

I am having trouble with getting the $repLevel variable to pass between two php files in this plugin; the initial php file where the form itself will submit a selected option and send it to a processor page (2nd block of code), but how to get the result from the switch statement back to the initial plugin file to print/display the result is where I'm struggling.

This is the front-facing form that would appear on the site

function ifWrap($content) {
            //check if we're on the page selected in the settings
            if ( is_page(get_option('lp_actPage')) ) {
                $container = '';
                $container .= '<form method="post" action="'.plugin_dir_url(__FILE__).'inc/">';
                $container .= '<label for="rep-type">I want to contact my</label>';
                $container .= '<select name="repLevel" id="level">';
                $container .= '<option value="">Select a level</option>';
                $container .= '<option value="first">Local and county</option>';
                $container .= '<option value="second">State</option>';
                $container .= '<option value="third">National</option>';
                $container .= '</select>';
                $container .= '<input type="submit" name="repType" value="Find"/>';
                $container .= '</form>';
                return $content . $container;
            }
         
            return $content;
        }
add_filter('the_content', array($this, 'ifWrap',9,2));

This is the processing code containing the switch statement

<?php
    $path = preg_replace('/wp-content.*$/','',__DIR__);
    require_once($path. "wp-load.php");
    
    if(isset($_POST['repType'])){
            
        $repLevel = sanitize_text_field($_POST['repType']);
            
        switch ($repLevel){
            case 'first':
                echo "code for first choice";
                break;
            case 'second':
                echo "code for second choice";
                break;
            case 'third':
                echo "code for third choice";
                break;
            default:
            echo "please choose among the options in the dropdown";                 
        }
        return $repLevel;
    }
?>

Apologies if this is redundant, but I'm looking to return that $repLevel variable such that the result from the switch statement shows up below the select form on the front-side of the site. T-i-a for any insights

4
  • 1
    you should avoid making direct requests to PHP files in your plugin/theme for security reasons, you should never need to bootstrap WP like that. Make the request to the same URL with an empty action="" on the form, then look for the posted inputs from your theme to trigger processing instead. Also is your processing code incomplete? You're doing a return $repLevel but that doesn't make sense outside of a function, are you expecting it to print out $repLevel to the browser? If you want to then use that value on the frontend you can't use a standalone PHP file like that
    – Tom J Nowell
    Sep 23 at 13:46
  • Thank you for the insights on avoiding direct requests to php file within my plugin/theme folder; as this is being built in a sandboxed/test environment security is less a concern than achieving basic functionality. Per your suggestion to " look for the posted inputs from your theme to trigger processing instead," could you elaborate on that some? I'm not following your meaning. Sep 23 at 22:05
  • 1
    I usually use a hidden input, or an input that's always got a value, e.g. <input type="hidden" name="myform" value="rep"/> then in PHP if ( !empty( $_POST['myform'] ) ) { process form inputs and display result or errors } else { display empty form }
    – Tom J Nowell
    Sep 24 at 8:57
  • @TomJNowell thank you again for the insights 2 days ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.