最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Use $_GET inside a shortcode print empty Array

programmeradmin6浏览0评论

I am using Code Snippets plugins, that allow create shortcodes from the Wordpress Admin panel. I am using a simple shortcode to print some info and works great. Anyway, I want make this add some info if the parameter 'error' is in the url.

To make this, I am doing this:

add_shortcode( 'my_shortcode', function () {

    $out = '';

    print_r($_GET); //--> prints Array ( )
    if(isset($_GET['error'])){
        $out .='<div id="login_error">El usuario o contraseña no es correcto.</div>';   
    } 

    $out .='HTML info';

    return $out;
} );

Anyway, my URL is http://localhost/login/?error=true and I think this should be working. Some idea about this problem?

I am using Code Snippets plugins, that allow create shortcodes from the Wordpress Admin panel. I am using a simple shortcode to print some info and works great. Anyway, I want make this add some info if the parameter 'error' is in the url.

To make this, I am doing this:

add_shortcode( 'my_shortcode', function () {

    $out = '';

    print_r($_GET); //--> prints Array ( )
    if(isset($_GET['error'])){
        $out .='<div id="login_error">El usuario o contraseña no es correcto.</div>';   
    } 

    $out .='HTML info';

    return $out;
} );

Anyway, my URL is http://localhost/login/?error=true and I think this should be working. Some idea about this problem?

Share Improve this question asked Apr 9, 2020 at 14:59 user1422434user1422434 1133 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

$_GET is a glob var. which is triger/created when you're sending html form data og visit a link. When is send from form you can add the needet info in "action" atr. But from youre example i see onlu that you return from the function some string...did not see the part of adding it to the $_GET...

as sugested up you can use $_REQUEST["GET"] or $_SERVER['REQUEST_METHOD'] allso

lets roll back a sec. - you need to add some extra data to the get requst if have some specific condition ( 'error )and the what??? for example :

This directly will add data to url and redirect to da location, do proper escaping..this is example

function get_server_info(){

$server = array();
if(isset($_SERVER['REMOTE_ADDR'])){
 $server["ip"] = $_SERVER['REMOTE_ADDR'];
}else{ $server["ip"] = false; }

if(isset($_SERVER['HTTP_HOST'])){
 $server["host"] = $_SERVER['HTTP_HOST'];
}else{ $server["host"] = false; }

if(isset($_SERVER['QUERY_STRING'])){
 $server["query"] = $_SERVER['QUERY_STRING'];
}else{ $server["query"] = false; }

if(isset($_SERVER['HTTP_REFERER'])){
 $server["from"] = $_SERVER['HTTP_REFERER']; 
}else{ $server["from"] = false; }

if(isset($_SERVER["REQUEST_URI"])){
  $server["requested"] = $_SERVER["REQUEST_URI"];
}else{ $server["requested"] = false; }

return $server;

}

function redirect_to($location,$content = "text/html"){

header("Location: ".$location);
header("Content-Type: ".$content);
exit;

}

this should redirect you to the original intendet location whit extra url data somthing like this

function add_to_url(){

$server = get_server_info();

if( youre condition ){

    $to_add ='<div id="login_error">El usuario o contraseña no es correcto. 
    </div>';

    $redirect_to =  $server["requested"]."&".$to_add;

    redirect_to( $redirect_to);

}

}

reading about this type of problems (because my template wasn't the problem) and it's a fresh installation. I read that wordpress recommends the use of $_REQUEST instead $_GET (Anyway I see that other people can use $_GET without problems) and tried with $_REQUEST, now it's working.

发布评论

评论列表(0)

  1. 暂无评论