Die hier akzeptierte Antwort ist sowohl falsch als auch richtig. Der Satz:
"WordPress verwirft alle Atts, die nicht standardmäßig angegeben sind."
Dies gilt nur für die shortcode_attsFunktion und nicht für die gesamte WP-Shortcode-Funktionalität.
Wenn Sie sich den folgenden Code ansehen:
add_shortcode( 'paragraph', 'custom_shortcode_paragraph' );
function custom_shortcode_paragraph( $atts ) {
// Attribute Defaults Function
$atts = shortcode_atts(
array(
'last' => false,
),
$atts
);
$isLast = $atts['last'] !== false;
}
Eine Short Nutzung wie [paragraph last] something [/paragraph]wird immer einen Wert falsefür die $isLastvar.
Das Problem ist , dass , wenn die shortcode_attsFunktion läuft es nicht die Attribute ohne Werte zu verwerfen. Sie befinden sich jedoch vor diesem Punkt absolut im $attsArray . A von als erste Zeile der Funktion würde erzeugen:var_dump$attscustom_shortcode_paragraph
array(1) {
[0]=>
string(4) "last"
}
Das 0. Element im Array ist also eine Zeichenfolge des Attributnamens, die in Kleinbuchstaben geschrieben werden muss.
Ändern wir stattdessen den Code wie folgt:
add_shortcode( 'paragraph', 'custom_shortcode_paragraph' );
function custom_shortcode_paragraph( $atts ) {
// look for the existance of the string 'last' in the array
$last = in_array('last', $atts); // $last is now a boolean
// Attribute Defaults Function
$atts = shortcode_atts(
array(
'last' => $last, // default to true if the attribute existed valueless
),
$atts
);
$isLast = $atts['last'] !== false;
}
Sie haben jetzt $atts['last'] === true && $isLast === truefür den Shortcode:
[paragraph last] something [/paragraph]
Wenn Sie am Ende einen Wert hinzufügen, lautet der Shortcode:
[paragraph last="any value at all"] something [/paragraph]
würde yeild $atts['last'] === "any value at all" && $isLast === true. $lastwäre, falseweil das $attsArray am Anfang den Inhalt hat:
array(1) {
["last"]=>
string(16) "any value at all"
}
Der Wert des Array-Elements ist also nicht mehr der Attributname, und in_array('last', $atts) === falseder Standardwert shortcode_attsist false. aber das ist nur ein Standard , der von der Ist - Wert wird überschrieben any value at allund dann $atts['last'] !== falseist trueda "any value at all" !== false.
Alles in allem denke ich, dass dies das tut, was Sie wollen und unempfindlich gegen Benutzerfehler ist.