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

Shortcode

Other topics

Registering shortcode

Shortcode is a small piece of code that can be added into the WordPress editor and will output something different once the page is published or previewed.

Frequently, shortcodes are added to the theme functions.php file, but that's not a good practice as shortcodes are expected to keep working after changing themes. Instead, write a plugin to add this functionality.

The structure for registering shortcode is:

function new_shortcode($atts, $content = null){
    // if parameters are needed in the shortcode
    // parameters can be set to default to something
    extract( shortcode_atts( array(
        'param_one' => 'h1'
    ), $atts ) );
    $shortcode = '<'.$param_one'>'.$content.'</'.$param_one.'>';
    return $shortcode;
}
// this is what registers the shortcode with wordpress
add_shortcode('demo-shortcode','new_shortcode');

Inside the WordPress editor, you can type:

[demo-shortcode param_one="h2"]Demo[/demo-shortcode]
// you don't need to insert param_one into the editor if it has a default value.
// having it in the editor will override the default

Once the page is published, this will turn into

<h2>Demo</h2>

Using Shortcodes in WordPress Backend

[footag foo="value of 1" attribute-2="value of 2"]

In wordpress admin we use pre defined shortcodes by writing the shortcode name inside square brackets and optionally adding attributes to it separating by space.

Adding New Shortcodes

function footag_func( $atts ) {
    return "foo = {$atts['foo']}";
}
add_shortcode( 'footag', 'footag_func' );

In plugins we can add shortcodes using the add_shortcode function.

The shortcode can be used in any Wordpress page or post just by enclosing it in square brackets.

[footag]

Using Shortcodes Inside PHP Code (themes and plugins)

<?php echo do_shortcode("[footag foo='Hi! I am a foo output']"); ?>

To print a shortcode using php use the do_shortcode function and echo the returned value.

Using Shortcodes in Widgets

add_filter( 'widget_text', 'shortcode_unautop' );
add_filter( 'widget_text', 'do_shortcode' );enter code here

Add this to a plugin or the functions.php file to enable shortcodes in widgets. The code first stops WordPress turning line breaks into paragraph tags and then lets shortcodes to parse for widgets. The order of the two lines is important.

Contributors

Topic Id: 4952

Example Ids: 16197,17495,17496,17497,20700

This site is not affiliated with any of the contributors.