[fusion_builder_container type=”flex” hundred_percent=”no” equal_height_columns=”no” menu_anchor=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” background_color=”” background_image=”” background_position=”center center” background_repeat=”no-repeat” fade=”no” background_parallax=”none” parallax_speed=”0.3″ video_mp4=”” video_webm=”” video_ogv=”” video_url=”” video_aspect_ratio=”16:9″ video_loop=”yes” video_mute=”yes” overlay_color=”” video_preview_image=”” border_color=”” border_style=”solid” padding_top=”” padding_bottom=”” padding_left=”” padding_right=””][fusion_builder_row][fusion_builder_column type=”1_1″ layout=”1_1″ background_position=”left top” background_color=”” border_color=”” border_style=”solid” border_position=”all” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding_top=”” padding_right=”” padding_bottom=”” padding_left=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” center_content=”no” last=”true” min_height=”” hover_type=”none” link=”” border_sizes_top=”” border_sizes_bottom=”” border_sizes_left=”” border_sizes_right=”” first=”true”][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
What is a Shortcode in WordPress?
Shortcodes in WordPress are compact snippets enclosed in square brackets ([]) that allow users to execute PHP code within WordPress posts, pages, and widgets without writing any code directly. Introduced in WordPress version 2.5, shortcodes make it easy for users to embed files or create complex page elements using simple, readable tags.
Why Use Shortcodes?
Shortcodes dramatically simplify the addition of dynamic features to content. Instead of manually writing complex HTML or PHP, users can use a single line of code. This makes them especially useful for non-technical users.
Syntax and Attributes
The basic syntax of a shortcode looks like this:
[/fusion_text][fusion_syntax_highlighter theme=”” language=”javascript” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]W3Nob3J0Y29kZV9uYW1lXQ==[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
Some shortcodes can accept attributes, like so:
[/fusion_text][fusion_syntax_highlighter theme=”” language=”javascript” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]W3Nob3J0Y29kZV9uYW1lIGF0dHJpYnV0ZT0idmFsdWUiXQ==[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
Example: Embedding a Video
To embed a video using a shortcode:
[/fusion_text][fusion_syntax_highlighter theme=”” language=”javascript” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]W3ZpZGVvIHNyYz0idmlkZW8tc291cmNlLm1wNCJd[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
This shortcode dynamically generates the necessary HTML to display a video player with the given source file.
Built-in WordPress Shortcodes
WordPress includes several default shortcodes:
[/fusion_text][fusion_syntax_highlighter theme=”” language=”javascript” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]W2F1ZGlvXSDigJMgRW1iZWRzIGF1ZGlvIGZpbGVzLgoKW2NhcHRpb25dIOKAkyBBZGRzIGEgY2FwdGlvbiB0byBhbiBpbWFnZS4KCltlbWJlZF0g4oCTIEF1dG9tYXRpY2FsbHkgZW1iZWRzIG1lZGlhIGZyb20gc3VwcG9ydGVkIHNlcnZpY2VzLgoKW2dhbGxlcnldIOKAkyBDcmVhdGVzIGFuIGltYWdlIGdhbGxlcnkuCgpbdmlkZW9dIOKAkyBFbWJlZHMgYSB2aWRlbyBmaWxlLg==[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
Escaping Shortcodes
If you want to display the shortcode text rather than executing it, escape it using double brackets:
[/fusion_text][fusion_syntax_highlighter theme=”” language=”javascript” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]W1t2aWRlb11dCgpUaGlzIHdpbGwgcmVuZGVyIHRoZSB0ZXh0IFt2aWRlb10gaW5zdGVhZCBvZiB0aGUgdmlkZW8gcGxheWVyLg==[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
Creating Custom Shortcodes
Developers can create custom shortcodes using the Shortcode API in functions.php or a plugin:
[/fusion_text][fusion_syntax_highlighter theme=”” language=”x-php” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]ZnVuY3Rpb24gbXlfY3VzdG9tX3Nob3J0Y29kZSgpIHsKICAgIHJldHVybiAnPHA+VGhpcyBpcyBhIGN1c3RvbSBtZXNzYWdlLjwvcD4nOwp9CmFkZF9zaG9ydGNvZGUoJ215c2hvcnRjb2RlJywgJ215X2N1c3RvbV9zaG9ydGNvZGUnKTsKClVzYWdlOgoKW215c2hvcnRjb2RlXQ==[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
Case Study: Real Estate Website
Scenario: A real estate agency wants to list property details dynamically.
Custom Shortcode Function:
[/fusion_text][fusion_syntax_highlighter theme=”” language=”x-php” line_numbers=”” line_wrapping=”” copy_to_clipboard=”” copy_to_clipboard_text=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=”” font_size=”” border_size=”” border_color=”” hue=”” saturation=”” lightness=”” alpha=”” border_style=”” background_color=”” line_number_background_color=”” line_number_text_color=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=””]ZnVuY3Rpb24gcHJvcGVydHlfbGlzdGluZ19zaG9ydGNvZGUoJGF0dHMpIHsKICAgICRhdHRzID0gc2hvcnRjb2RlX2F0dHMoWwogICAgICAgICdwcmljZScgPT4gJ0NvbnRhY3QgZm9yIFByaWNlJywKICAgICAgICAnbG9jYXRpb24nID0+ICdVbmtub3duIExvY2F0aW9uJywKICAgIF0sICRhdHRzKTsKCiAgICByZXR1cm4gIjxkaXYgY2xhc3M9J3Byb3BlcnR5Jz4KICAgICAgICAgICAgICAgIDxoMz5Qcm9wZXJ0eSBMaXN0aW5nPC9oMz4KICAgICAgICAgICAgICAgIDxwPkxvY2F0aW9uOiB7JGF0dHNbJ2xvY2F0aW9uJ119PC9wPgogICAgICAgICAgICAgICAgPHA+UHJpY2U6IHskYXR0c1sncHJpY2UnXX08L3A+CiAgICAgICAgICAgIDwvZGl2PiI7Cn0KYWRkX3Nob3J0Y29kZSgncHJvcGVydHknLCAncHJvcGVydHlfbGlzdGluZ19zaG9ydGNvZGUnKTsKClVzYWdlIGluIFBhZ2UgRWRpdG9yOgoKW3Byb3BlcnR5IHByaWNlPSIkNTAwLDAwMCIgbG9jYXRpb249IkRvd250b3duIExBIl0=[/fusion_syntax_highlighter][fusion_text columns=”” column_min_width=”” column_spacing=”” rule_style=”” rule_size=”” rule_color=”” hue=”” saturation=”” lightness=”” alpha=”” user_select=”” awb-switch-editor-focus=”” content_alignment_medium=”” content_alignment_small=”” content_alignment=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” sticky_display=”normal,sticky” class=”” id=”” margin_top=”” margin_right=”” margin_bottom=”” margin_left=”” fusion_font_family_text_font=”” fusion_font_variant_text_font=”” font_size=”” line_height=”” letter_spacing=”” text_transform=”” text_color=”” animation_type=”” animation_direction=”left” animation_color=”” animation_speed=”0.3″ animation_delay=”0″ animation_offset=”” logics=””]
This renders a styled div with the property information, allowing editors to manage content without touching HTML or PHP.
Conclusion
Shortcodes are a powerful feature of WordPress, enabling content creators to add advanced functionality quickly and easily. Whether you’re embedding media, creating layouts, or integrating plugin features, shortcodes provide a robust and user-friendly solution.
[/fusion_text][/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]