Je dois créer une page de destination à partir de laquelle je peux récupérer un jeton à partir d'une chaîne de requête dans l'URL.
Je voudrais donc créer un shortcode qui puisse y parvenir.
J'ai trouvé un plugin qui peut récupérer le jeton et l'afficher sur la page. Cela ressemble à ceci:
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 '';
}
J'aimerais ajouter du code qui place la chaîne de requête du code ci-dessus dans ma base de données MySQL. Sur https://www.w3schools.com/php/php_mysql_insert.asp j'ai trouvé ce 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', '[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;
?>
J'ai donc essayé de combiner le code en créant d'abord une table avec phpmyadmin appelée wp_token et en insérant ensuite la ligne
$sql = "INSERT INTO wp_token ('$return')";
dans le code du plugin comme on peut le voir ci-dessous:
<?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 '';
}
?>
Cependant, cela ne fonctionne pas. Je ne sais pas si je dois d'abord définir la connexion ou quel est le problème?
Les codes courts permettent de générer du code HTML pour le serveur frontal. Aucune fonctionnalité de serveur principal ne doit écrire dans la base de données pour les raisons suivantes: