如何构建一个可以将查询字符串插入到DB表中的短码

时间:2018-03-19 作者:flemmingha

我需要构建一个登录页,从中可以从URL中的查询字符串中获取令牌。

因此,我想创建一个可以实现这一点的短代码。

我发现了一个插件,可以抓取令牌并将其显示在页面上。看起来是这样的:

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 \'\';
}
我想添加一些代码,将上面代码中的查询字符串放入我的MySQL数据库。在…上https://www.w3schools.com/php/php_mysql_insert.asp 我找到了以下代码:

<?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\', \'[email protected]\')";
    // 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;
?>
因此,我尝试通过首先创建一个名为wp\\u token的phpmyadmin表,然后插入行来组合代码

$sql = "INSERT INTO wp_token (\'$return\')";
插入插件代码,如下所示:

<?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 \'\';
}

?>
然而,它不起作用。我不确定是否需要首先定义连接或问题是什么?

1 个回复
最合适的回答,由SO网友:Mark Kaplun 整理而成

短代码用于为前端生成HTML,前端功能不应写入DB,因为:

如果有足够的流量,它会让你的网站瘫痪

结束

相关推荐

ADD_FILTER IMG_CAPTION_SHORTCODE未实现

在我正在创建的插件中,我需要更改[caption]. 我是这样做的(就像《法典》和这里的其他例子一样)add_filter(\'img_caption_shortcode\', \'caption_fix\', 10, 3); function caption_fix ($x, $att, $content) { $content = \'new caption\' ; // change it return $content; } 但是[caption]