Recherche…


Introduction au shortcode

Les raccourcis sont utiles lorsque vous souhaitez pouvoir ajouter des éléments plus complexes en ligne dans l'éditeur de contenu normal.

Un shortcode dans sa forme la plus simple ressemble à ceci:

function my_shortcode( ){
    return "This is a shortcode";
}
add_shortcode( 'my_shortcode', 'my_shortcode' );

Il afficherait le texte "This is a shortcode" et vous l'utiliseriez en écrivant [my_shortcode] dans l'éditeur de contenu.

Bouton shortcode

Voici un exemple de code court de bouton simple:

<?php
function my_button_shortcode( $atts ) {

    // Parse the input attributes and assgn default values for the 
    // attributes that are not specified on the shortcode
    $a = shortcode_atts( array(
        'id' => '',
        'url' => '#',
        'class' => '',
        'text' => ''
    ), $atts );

    // Open the anchor tag and add role=button for better accessibility
    $btn_html = '<a role="button"';

    // Add the href(link) attribute
    $btn_html .= ' href="' . $a['url'] . '"';

    // Add id attribute to output if specified
    if ( !empty( $a['id'] ) ) {
        $btn_html .= ' id="' . $a['id'] . '"';
    }

    $btn_classes = 'button';


    // Add class attribute to output
    $btn_html .= ' class="button ' . ( !empty(a['class']) ? $a['class'] : '' ) . '"';

    // Close opening anchor tag
    $btn_html .= '>'.
        // Add button text
        $a['text'].
        // Add closing anchor tag
        '</a>'."\n";

    return $btn_html;
}
add_shortcode( 'button', 'my_button_shortcode' );

Ce shortcode peut être utilisé en tapant [button url="/my-other-page" id="my-other-page-button" class="dark" text="Click me!"] Dans l'éditeur et affichera le HTML suivant:

<a role="button" href="/my-other-page" id="my-other-page-button" 
class="button dark">Click me!</a>


Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow