3

On my WordPress page I have created a shortcode function which gets a parameters from the URL of the post. So, I have created this simple function and added the following code to the theme's file function.php:

function getParam($param) {
  if ($param !== null && $param !== '') {
    echo $param;
  } else {
    echo "Success";
  }
}

add_shortcode('myFunc', 'getParam');

And I know I have to add this shortcode to posts and pages using (in my case) [myFunc param=''].

Usually, I would get the value of the parameter from the URL using <?php $_GET['param'] ?>, but in this case I don't know how to send this PHP code to the shortcode function.

For example, I doubt I can write [myFunc param=$_GET['param']].

4
  • have you tried [myFunc param=$_GET['param']]? $_GET is a global var. btw, don't use echo "Success";, use return "Success"; Commented Sep 27, 2016 at 12:54
  • @Dimentica: The $param var is a Array and you can access $param['param']. Commented Sep 27, 2016 at 12:54
  • @Dimentica: Try to use return instead echo. Commented Sep 27, 2016 at 13:01
  • @Dimentica - never take $_GET[] for granted. It can contain malicious code - always (!) do safety checks on it before applying its value! (In Wordpress sanitize_text_field() is a good idea.) Commented Sep 27, 2016 at 14:15

3 Answers 3

1

A shortcode like this:

[myFunc funcparam="param"]

Is not needed here, unless the called parameter is changing with the posts.

Let's say you have this URL:

http://example.com?param=thisparam

To get the value of 'param' by using the shortcode described above, your function in functions.php should look something like this:

function sc_getParam() {

    // Get parameter(s) from the shortcode
    extract( shortcode_atts( array(
        "funcparam" => 'funcparam',
    ), $atts ) );

    // Check whether the parameter is not empty AND if there is
    // something in the $_GET[]
    if ( $funcparam != '' && isset( $_GET[ $funcparam ] ) ) {

        // Sanitizing - this is for protection!
        $thisparam = sanitize_text_field( $_GET[ $funcparam ] );

        // Returning the value from the $_GET[], sanitized!
        return $thisparam;
    } 
    else {
        // Something is not OK with the shortcode function, so it
        // returns false
        return false;
    }
}

add_shortcode( 'myFunc', 'sc_getParam' );

Look up these references:

Sign up to request clarification or add additional context in comments.

7 Comments

Exactly, I need the param to be changing with the posts. However, I still cannot get [myFunc funcparam="param"] to take the param from a url like this one example.com?param=thisparam
Then I suggest going step by step. Does the shortcode function correctly? (E.g. you extract the parameter from the shortcode, and return a test string (do not use $_GET[] yet).) If it works correctly, then go on to check what is wrong with $_GET[]. If not, then there's another problem (probably some mistyping or something frustrating like that).
And there's another thing: shortcodes do not work everywhere in WP by default (for example: in widgets you have to "activate" them first in functions.php - wpbeginner.com/wp-tutorials/….
The function works correctly, but the $_GET[] comes empty. My URL is sth like mysite/2016/09/28/17/?param=ZZZ and the shortcode is [myFunc funcparam="param"]
If you modify the template file (just to <?php print_r( $_GET[ "param" ] ); ?> somewhere), does the $_GET[] work? When is the ?param attached to the URL? (If JS puts it there dynamically, then PHP won't be able to handle it, only through an AJAX request after it's put there.) Here's a question with an "empty $_GET[]": stackoverflow.com/questions/27673211/…
|
0

If you need get the parameters you can:

function getParam($arg) {

    if (isset($arg) &&
        array_key_exists('param', $arg ) &&
        $arg['param'] != '')
    {
        return $_GET[$arg['param']]; // OR  get_query_var($arg['param']);
    }
    else
        return "Success";
}

add_shortcode('name', 'getParam');

Comments

0

Do it this way:

function getParam($data)
{
    $var = $_GET[$data['param']];

    if ($var !== null && $var !== '')
    {
        echo "True: " . $var;
    }
    else echo "False: " . $var;
}

And call it by: [myFunc param=whatever]

You shouldn't call a function in the shortcode.

For better understanding, I changed your code just a little bit, but beware this is not secure and clean.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.