使用参数创建快捷代码

时间:2017-06-19 作者:Abuzettin

我正在使用短代码。

function bold_text_shortcode( $atts , $content = null ) {
    return \'<b>\' . $content . \'</b>\';
}
add_shortcode( \'b\', \'bold_text_shortcode\' );  
我创建了加粗的短代码[b]content[b].

我需要创造[color=red]content[/color].

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

下面是一个[color] 短代码。如果指定了一个参数,我们假设(但验证)它是一种颜色。$content 然后包裹在<span> 使用内联样式应用标记和颜色。

颜色可以作为命名颜色或十六进制颜色传递。接受3或6位数的十六进制颜色,并且# 是可选的。

/**
 * A shortcode that outputs the content in the color
 * specified. 
 *
 * @param array $atts
 * @param string content
 * @return string
 */
add_shortcode( \'color\', \'wpse_color\' );
function wpse_color( $atts, $content = \'\' ) {
    // Bail if there is no content or color to work with.
    if ( ! $content || ! isset( $atts[0] ) ) {
        return false;
    }

    $color = $atts[0];
    $valid_color = false;

    // Validate color: Allow named color or 
    // 3 or 6 digit hex color with/without preceding #.
    // @link https://stackoverflow.com/a/23541977/3059883       
    preg_match(\'/(#?[a-f0-9]{3}([a-f0-9]{3})?)/i\', $color, $color_matches );
    if ( isset( $color_matches[1] ) ) {
        $valid_color = true;
        // Maybe add preceeding #
        if ( strncmp( $color, "#", 1 ) !== 0 ) {
            $color = \'#\' . $color;
        }
    } else { // Named color
    $named_colors = array( \'aliceblue\', \'antiquewhite\', \'aqua\', \'aquamarine\', \'azure\', \'beige\', \'bisque\', \'black\', \'blanchedalmond\', \'blue\', \'blueviolet\', \'brown\', \'burlywood\', \'cadetblue\', \'chartreuse\', \'chocolate\', \'coral\', \'cornflowerblue\', \'cornsilk\', \'crimson\', \'cyan\', \'darkblue\', \'darkcyan\', \'darkgoldenrod\', \'darkgray\', \'darkgreen\', \'darkkhaki\', \'darkmagenta\', \'darkolivegreen\', \'darkorange\', \'darkorchid\', \'darkred\', \'darksalmon\', \'darkseagreen\', \'darkslateblue\', \'darkslategray\', \'darkturquoise\', \'darkviolet\', \'deeppink\', \'deepskyblue\', \'dimgray\', \'dodgerblue\', \'firebrick\', \'floralwhite\', \'forestgreen\', \'fuchsia\', \'gainsboro\', \'ghostwhite\', \'gold\', \'goldenrod\', \'gray\', \'green\', \'greenyellow\', \'honeydew\', \'hotpink\', \'indianred\', \'indigo\', \'ivory\', \'khaki\', \'lavender\', \'lavenderblush\', \'lawngreen\', \'lemonchiffon\', \'lightblue\', \'lightcoral\', \'lightcyan\', \'lightgoldenrodyellow\', \'lightgreen\', \'lightgrey\', \'lightpink\', \'lightsalmon\', \'lightseagreen\', \'lightskyblue\', \'lightslategray\', \'lightsteelblue\', \'lightyellow\', \'lime\', \'limegreen\', \'linen\', \'magenta\', \'maroon\', \'mediumaquamarine\', \'mediumblue\', \'mediumorchid\', \'mediumpurple\', \'mediumseagreen\', \'mediumslateblue\', \'mediumspringgreen\', \'mediumturquoise\', \'mediumvioletred\', \'midnightblue\', \'mintcream\', \'mistyrose\', \'moccasin\', \'navajowhite\', \'navy\', \'oldlace\', \'olive\', \'olivedrab\', \'orange\', \'orangered\', \'orchid\', \'palegoldenrod\', \'palegreen\', \'paleturquoise\', \'palevioletred\', \'papayawhip\', \'peachpuff\', \'peru\', \'pink\', \'plum\', \'powderblue\', \'purple\', \'rebeccapurple\', \'red\', \'rosybrown\', \'royalblue\', \'saddlebrown\', \'salmon\', \'sandybrown\', \'seagreen\', \'seashell\', \'sienna\', \'silver\', \'skyblue\', \'slateblue\', \'slategray\', \'snow\', \'springgreen\', \'steelblue\', \'tan\', \'teal\', \'thistle\', \'tomato\', \'turquoise\', \'violet\', \'wheat\', \'white\', \'whitesmoke\', \'yellow\', \'yellowgreen\' );
    if ( in_array( strtolower( $color ), $named_colors ) ) {
            $valid_color = true;
    }
    }

    // If we have a valid color, colorize $content.
    if ( $valid_color ) {
        return \'<span style="color:\' . esc_attr( $color ) . \'">\' . $content . \'</span>\';
    }
}

Example usage:

这是使用命名颜色对颜色快捷码进行的测试。

这是对彩色短码的另一个测试,使用前一个#的6位十六进制颜色。

这是对颜色短码的另一个测试,它使用了6位十六进制颜色,没有前面的#。

这是对彩色短码的另一个测试,使用前一个#的3位十六进制颜色。

这是对彩色短码的另一个测试,使用3位十六进制颜色,而不使用前面的#

结束

相关推荐

php code to shortcode

这是我的php代码:<?php global $royal_profile;?> <div class=\"profile-img\" data-key=\"profilepicture\"> <?php echo get_avatar( get_current_user_id(), 64 ); ?> </div> 然后,我尝试将其转换为一个短代码(以便我可以在菜单中使用,因为我意识到我不能简