1

I need to build a landing page from which I can grab a token from a query-string in the URL.

I would therefore like to create a shortcode which can achieve this.

I have found a plugin which can grab the token and display it on the page. It looks like this:

add_shortcode("urlparam", "urlparam");
add_shortcode("ifurlparam", "ifurlparam");

function urlparam($atts, $content) {
    $defaults = array(
        'param'          => '',
        'default'        => '',
        'dateformat'     => '',
        'attr'           => '',
        'htmltag'        => false,
    );

    /* We used to use shortcode_atts(), but that would nuke an extra attributes
       that we don't know about but want. array_merge() keeps them all. */
    $atts = array_merge($defaults, $atts);

    $params = preg_split('/\,\s*/',$atts['param']);

    $return = false;

    foreach($params as $param)
    {
        if(!$return and ($rawtext = $_REQUEST[$param]))
        {
            if(($atts['dateformat'] != '') && strtotime($rawtext))
            {
                $return = date($atts['dateformat'], strtotime($rawtext));
            } else {
                $return = esc_html($rawtext);
            }
        }
    }

    if(!$return) {
        $return = $atts['default'];
    }

    if($atts['attr']) {
        $return = ' ' . $atts['attr'] . '="' . $return . '" ';

        if($atts['htmltag']) {
            $tagname = $atts['htmltag'];

            foreach(array_keys($defaults) as $key) {
                unset($atts[$key]);
            }

            $otheratts = "";
            foreach($atts as $key => $val) {
                $otheratts .= " $key=\"$val\"";
            }

            $return = "<$tagname $otheratts $return".($content ?
                    ">$content</$tagname>" : "/>");
        }
    }

    return $return;
}

/*
 * If 'param' is found and 'is' is set, compare the two and display the contact if they match
 * If 'param' is found and 'is' isn't set, display the content between the tags
 * If 'param' is not found and 'empty' is set, display the content between the tags
 *
 */
function ifurlparam($atts, $content) {
    $atts = shortcode_atts(array(
                               'param'           => '',
                               'empty'          => false,
                               'is'            => false,
                           ), $atts);

    $params = preg_split('/\,\s*/',$atts['param']);

    foreach($params as $param)
    {
        if($_REQUEST[$param])
        {
            if($atts['empty'])
            {
                return '';
            } elseif(!$atts['is'] or ($_REQUEST[$param] == $atts['is'])) {
                return do_shortcode($content);
            }
        }
    }

    if ($atts['empty'])
    {
        return do_shortcode($content);
    }

    return '';
}

I would like to add some code which puts the query string from the code above into my MySQL database. On https://www.w3schools.com/php/php_mysql_insert.asp I found this code:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
                    $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>

I therefore tried to combine the code by first creating a table with phpmyadmin called wp_token and afterwards inserting the line

$sql = "INSERT INTO wp_token ('$return')";

into the plugin code as can be seen below:

<?php
/*
Plugin Name: URL Params
Plugin URI: http://asandia.com/wordpress-plugins/urlparams/
Description: Short Code to grab any URL Parameter
Version: 2.1
Author: Jeremy B. Shapiro
Author URI: http://www.asandia.com/
*/

/*
URL Params (Wordpress Plugin)
Copyright (C) 2011-2016 Jeremy Shapiro

*/

//tell wordpress to register the shortcodes
add_shortcode("urlparam", "urlparam");
add_shortcode("ifurlparam", "ifurlparam");

function urlparam($atts, $content) {
    $defaults = array(
        'param'          => '',
        'default'        => '',
        'dateformat'     => '',
        'attr'           => '',
        'htmltag'        => false,
    );

    /* We used to use shortcode_atts(), but that would nuke an extra attributes 
       that we don't know about but want. array_merge() keeps them all. */
    $atts = array_merge($defaults, $atts);

    $params = preg_split('/\,\s*/',$atts['param']);

    $return = false;

    foreach($params as $param)
    {
        if(!$return and ($rawtext = $_REQUEST[$param]))
        {
            if(($atts['dateformat'] != '') && strtotime($rawtext))
            {
                $return = date($atts['dateformat'], strtotime($rawtext));
            } else {
                $return = esc_html($rawtext);
            }
        }
    }

    if(!$return) {
        $return = $atts['default'];
    }

    if($atts['attr']) {
        $return = ' ' . $atts['attr'] . '="' . $return . '" ';

        if($atts['htmltag']) {
            $tagname = $atts['htmltag'];

            foreach(array_keys($defaults) as $key) {
                unset($atts[$key]);
            }

            $otheratts = "";
            foreach($atts as $key => $val) {
                $otheratts .= " $key=\"$val\"";
            }

            $return = "<$tagname $otheratts $return".($content ?
                    ">$content</$tagname>" : "/>");
        }
    }

    $sql = "INSERT INTO wp_token ('$return')";

    return $return;
}

/*
* If 'param' is found and 'is' is set, compare the two and display the contact if they match
* If 'param' is found and 'is' isn't set, display the content between the tags
* If 'param' is not found and 'empty' is set, display the content between the tags
*
*/
function ifurlparam($atts, $content) {
    $atts = shortcode_atts(array(
                               'param'           => '',
                               'empty'          => false,
                               'is'            => false,
                           ), $atts);

    $params = preg_split('/\,\s*/',$atts['param']);

    foreach($params as $param)
    {
        if($_REQUEST[$param])
        {
            if($atts['empty'])
            {
                return '';
            } elseif(!$atts['is'] or ($_REQUEST[$param] == $atts['is'])) {
                return do_shortcode($content);
            }
        }
    }

    if ($atts['empty'])
    {
        return do_shortcode($content);
    }

    return '';
}

?>

However, it is not working. I am not sure if I need to define the connection first or what the issue is?

  • Note: I removed the request for plugin recommendation, as that is out of scope for this site. – Dave Romsey Mar 19 '18 at 22:20
  • 2
    WordPress has its own wpdb class for interacting with the database. – Milo Mar 19 '18 at 22:41
  • How are you implementing this shortcode? i.e: [urlparam ...][/urlparam] – Eduardo Escobar Mar 19 '18 at 23:27
  • 2
    Why are you using a shortcode for this? Shortcodes are for, essentially, content macros. If you need to handle this sort of thing it should be hooked somewhere. – Jacob Peattie Mar 19 '18 at 23:48
  • @EduardoEscobar if the query string I need is called token I just write: test [urlparam param="token" /] and the query string is displayed. – flemmingha Mar 20 '18 at 15:26
1

Short codes are for generating HTML for the front end, and no front end functionality should write to the DB because:

  1. With enough traffic it will bring down your site.
  2. If you use caching or CDN for your content it will simply not work at all, if you expect it to work for all page loads.

Your Answer

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

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