Getting started with WordPressget_bloginfo()Enqueuing scriptsMaking network requests with HTTP APIEnqueuing Styleshome_url()Custom Post Typestemplate_includeThe Loop (main WordPress loop)AJAXThe $wpdb ObjectActions and Filterswp_get_current_user()Add/remove contact info for users with user_contactmethods filter hookCreating a custom templateCustomizer Hello WorldCustomizer Basics (Add Panel, Section, Setting, Control)The Admin Bar (aka "The Toolbar")Querying postsAlternating main loop (pre_get_posts filter)ShortcodeCreate a Post Programmaticallyget_template_part()Taxonomiesget_template_part()ShortcodesPost FormatsCustom exerpts with excerpt_length and excerpt_morePlugin developmentSecurity in WordPress - EscapingTemplate hierarchyRemove Version from Wordpress and StylesheetsChild Theme Basicsadd_action()get_template_part()Shortcode with attributeSidebarsSecurity in WordPress - SanitizationinitCreate Template for Custom Post TypeFunction: add_action()Add ShortcodeHow Can I integrate Markdown editor with Advance Custom Field's repeater Add-on.Installation and Configurationwp_get_current_user()WP-CronSecure your installationOptions APIFunction : wp_trim_words()WP_Query() LoopUpdate WordPress ManuallyThemesWP-CLIDebuggingadd_menu_page()add_submenu_page()get_option()get_permalink()get_the_category()the_title()get_the_title()add_editor_style()add_theme_support()WordPress Plugin creationRun WordPress local with XAMPPAdmin Dashboard WidgetsSite MigrationMeta BoxRemove Auto Line Breaks From Content and Excerptget_home_path()Wordpress theme and child-theme developmentREST API

Shortcodes

Other topics

Shortcode introduction

Shortcodes are useful when you want to be able add more complex elements inline into the normal content editor.

A shortcode in it's simplest for would look like this:

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

It would output the text "This is a shortcode" and you would use it by writing [my_shortcode] in the content editor.

Button shortcode

Here is an example of a simple button short code:

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

This shortcode can be used by typing [button url="/my-other-page" id="my-other-page-button" class="dark" text="Click me!"] into the editor and will output the following HTML:

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

Contributors

Topic Id: 6070

Example Ids: 21173,21174

This site is not affiliated with any of the contributors.