Using $_GET and $_POST in custom WordPress plugin

时间:2018-09-18 19:32:13

标签: php wordpress

I'm new to developing plugins for WordPress, I've been doing it for Magento 2 and thought it would be similar, so here I am.

I'm consuming a RESTful API to display information about some courses, it retrieves me the info on all courses so now I have to filter some attributes, for example "city", for this my user has an HTML form to select which city he is in, but when I try to use the "city" field from the form it returns as null, it doesn't matter if I use post or get method.

Which one is the correct form of getting this attributes in WordPress?

this is an example of the form page:

    <?php
/* 
 * Formulario de filtros
 */
// create curl resource

require_once('../../../../wp-config.php');

$ciudad = $_POST['ciudad'];

var_dump($ciudad);

$accessToken = '---';
$apiUrl = 'url';

$curl = curl_init($apiUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$accessToken]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($curl);
$cursos = json_decode($json);
$arr = 0;

foreach($cursos as $k => $cur)
{
    $sede = $cur->Sede;
    if($sede === 'MEDELLIN' ){
    $codigo[] = substr($cur->Programa, 0, 4);
    $programa[] = $cur->Programa;
    $precio[] = $cur->Valor_Contado;
    }
}
$cod = array_unique($codigo);
var_dump($cod);
foreach ($cod as $c){
    echo "Programa: ".$c."<br>";
}

?>
<div class="filtrosContainer">
    <form class="filtro-cursos" method="post">
<div class="vc_row wpb_row vc_row-fluid edgtf-section edgtf-content-aligment-left"><div class="clearfix edgtf-full-section-inner"><div class="wpb_column vc_column_container vc_col-sm-6"><div class="vc_column-inner "><div class="wpb_wrapper">
    <div class="wpb_text_column wpb_content_element ">
        <div class="wpb_wrapper">
                    <label>Metodología</label>
                    <select name="metodologia" id="metodologia" class="metodologia">
                        <option selected>--Seleccione--</option>
                        <option value="PRESENCIAL" required>Presencial</option>
                        <option value="VIRTUAL">Virtual</option>
                    </select>
                    <div id="ciudadDiv">
                    <label>Ciudad</label>
                    <select name="ciudad" id="ciudad">
                        <option value="none" selected>--Seleccione--</option>
                        <option value="MEDELLIN" required>Medellín</option>
                        <option value="BOGOTA">Bogotá</option>
                        <option value="BARRANQUILLA">Barranquilla</option>
                        <option value="BUCARAMANGA">Bucaramanga</option>
                        <option value="MANIZALES">Manizales</option>
                        <option value="CALI">Cali</option>
                    </select></div>
                    <label>Programa</label>
                    <select name="programa" id="programa">
                        <?php 
                        if($ciudad === "MEDELLIN"){ ?>
                            <option value="none" selected>--Seleccione--</option>
                            <option value="none" selected>PreUdeA</option>
                            <option value="none" selected>PreUnal</option>
                            <option value="none" selected>PreICFES Saber 11°</option>
                            <option value="none" selected>PreMédico</option>
                            <option value="none" selected>PreUdeA</option>
                       <?php }else{
                           echo $ciudad;
                       }
                                ?>
                    </select>
        </div>
    </div>
</div></div></div><div class="wpb_column vc_column_container vc_col-sm-6"><div class="vc_column-inner "><div class="wpb_wrapper">
    <div class="wpb_text_column wpb_content_element ">
        <div class="wpb_wrapper">
                    <label>¿Cuando presentarás el examen?</label>
                <select name="semestre" id="semestre">
                        <option value="none" selected>--Seleccione--</option>
                        <option value="2019-1" selected>2019-1</option>
                        <option value="2019-2" selected>2019-2</option>
                    </select>
                    <div id="jornadaDiv">
                        <label>Jornada</label>
                <select name="jornada" id="jornada">
                        <option value="none" selected>--Seleccione--</option>
                        <option value="SEMANA" selected>Semana</option>
                        <option value="FINDE" selected>Fin de semana</option>
                    </select>
                    </div>
                <button type="submit" name="submit" value="Filtrar">Filtra tu programa</button>
        </div>
    </div>
</div></div></div></div></div>
</form>
</div>

1 个答案:

答案 0 :(得分:0)

经过研究并尝试了几种方法,我在自定义插件的主PHP文件中一起尝试了所有代码,而不是使用require_once。

我有了下一个结构: wp-content >> plugins >> myplugin >> myplugin.php

并且在所说的php文件中有一个require_once用于: wp-content >> plugins >> myplugin >> php >> form.php

似乎您不能在主要php文件之外的文件中使用$ _POST,$ _ GET或evev cookie,因此我不得不在其中处理所有代码,而忘了mu php文件夹和eny MVC结构。