Buscar..


Introducción al código corto

Los códigos cortos son útiles cuando desea poder agregar elementos más complejos en línea en el editor de contenido normal.

Un shortcode en su forma más simple para se vería así:

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

Mostraría el texto "This is a shortcode" y lo utilizarías escribiendo [my_shortcode] en el editor de contenido.

Button shortcode

Aquí hay un ejemplo de un simple código abreviado de botón:

<?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' );

Este código abreviado se puede usar al escribir [button url="/my-other-page" id="my-other-page-button" class="dark" text="Click me!"] En el editor y se mostrará el siguiente HTML:

<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
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow