WordPressbuttonsThe use of short codes can add interactivity and visual appeal to your website. With button shortcodes, the addition of stylized buttons can be easily accomplished without complex coding or can be done quickly with plugins or built-in functionality.
![图片[1]-WordPress 按钮短代码完整指南:短代码、插件与 Gutenberg 方法详解](https://www.361sale.com/wp-content/uploads/2024/12/20241203121839405-image.png)
In this article, we will detail the implementation and best practices for WordPress button shortcodes from the following aspects:
- Short code method
- Plug-in method
- Direct method of adding buttons
- Button Design and Optimization Tips
- Frequently Asked Questions
1. Add WordPress buttons using shortcodes
Shortcodes are concise snippets of code that can embed complex functionality into a page or article. For example, with shortcodes, you can quickly create link buttons and customize their style.
Step 1: Add button shortcode
- Log in to the WordPress admin panel.
- Go to "exterior condition > Theme File Editor", find functions.php file. If you are concerned that modifying the theme files may affect updates, use the subtheme or custom plug-ins.
![图片[2]-WordPress 按钮短代码完整指南:短代码、插件与 Gutenberg 方法详解](https://www.361sale.com/wp-content/uploads/2024/12/20241203092304435-image.png)
- exist functions.php Add the following snippet to the file:
// Functions that define button shortcodes
function custom_button_shortcode($atts) {
// Define default properties
$atts = shortcode_atts(
array(
'url' => '#', // default link
'text' => 'click me', // default button text
'class' => 'custom-button', // default CSS class
),
$atts.
'button'
);
// Return the HTML code for the button
return '' . esc_html($atts['text']) . '' .
}
// Registering a short code
add_shortcode('button', 'custom_button_shortcode');
Step 2: Set the button style
- Go to "exterior condition > customizable > Additional CSS".
![图片[3]-WordPress 按钮短代码完整指南:短代码、插件与 Gutenberg 方法详解](https://www.361sale.com/wp-content/uploads/2024/12/20241203094046937-image.png)
- Add the following CSS:
.custom-button {
display: inline-block;
padding: 10px 20px;
background-color: #0073aa;
color: #fff.
text-decoration: none;
border-radius: 5px;
font-size: 16px;
}
.custom-button:hover {
background-color: #005177.
}
![图片[4]-WordPress 按钮短代码完整指南:短代码、插件与 Gutenberg 方法详解](https://www.361sale.com/wp-content/uploads/2024/12/20241203094619143-image.png)
Step 3: Use short codes
Insert the following short code where you want the button to be displayed (e.g., on a page, in a post):
[button url="https://example.com" text="Learn more" class="my-custom-class"]
Attribute Description
- url: The target link for the button.
- text: The display text of the button.
- class: Custom CSS class (can be empty).
2. Using plug-ins to add WordPress button shortcodes
Plugins are a handy solution if you are not familiar with coding. Here are the detailed steps to add a button shortcode via a plugin.
Recommended plug-ins::WP Shortcodes
WP Shortcodes is a powerful plugin that allows you to quickly create shortcodes for elements like buttons.
procedure
- Installation of plug-ins::
- Log in to the WordPress dashboard.
- Search and install WP Shortcodes plugin to activate it.
![图片[5]-WordPress 按钮短代码完整指南:短代码、插件与 Gutenberg 方法详解](https://www.361sale.com/wp-content/uploads/2024/12/20241203111536510-image.png)
- Configuration Button Short Code::
- Open the page or article to which you wish to add a button.
- In the editor, find the short code button Icons.
- Select "buttons" short code to configure the button's text, link, color, size, etc. as required.
![图片[6]-WordPress 按钮短代码完整指南:短代码、插件与 Gutenberg 方法详解](https://www.361sale.com/wp-content/uploads/2024/12/20241203112235549-image.png)
![图片[7]-WordPress 按钮短代码完整指南:短代码、插件与 Gutenberg 方法详解](https://www.361sale.com/wp-content/uploads/2024/12/20241203112304904-image.png)
- Save and Preview::
- Once the configuration is complete, save the page and preview the button effect.
vantage
- Provides visualization without the need to write code manually.
- A wide range of styles and features are available.
3. Adding buttons directly using the Gutenberg editor
The Gutenberg editor has a built-in "button" module, which is the most straightforward way to do this without additional plugins or shortcodes.
move
- Open the editor::
- Create or edit pages/posts.
- Click the "+" button to add a new block.
![图片[8]-WordPress 按钮短代码完整指南:短代码、插件与 Gutenberg 方法详解](https://www.361sale.com/wp-content/uploads/2024/12/20241203112425869-image.png)
- Adding a button block::
- Search for the "Button" block and insert it.
- Enter the button text and set the destination link.
![图片[9]-WordPress 按钮短代码完整指南:短代码、插件与 Gutenberg 方法详解](https://www.361sale.com/wp-content/uploads/2024/12/20241203112536143-image.png)
- Customize Button Styles::
- Use the toolbar to set button color, size, alignment, etc.
- Switchable styles, e.g. filled, outlined buttons.
![图片[10]-WordPress 按钮短代码完整指南:短代码、插件与 Gutenberg 方法详解](https://www.361sale.com/wp-content/uploads/2024/12/20241203112622682-image.png)
- Save and Preview::
- After confirming that you are satisfied with the appearance, save and publish.
4. Button design and optimization techniques
Designing button text
- Keep it simple, e.g. "Learn more", "Sign up now".
- Use verbs to enhance action, such as "download" and "start".
![图片[11]-WordPress 按钮短代码完整指南:短代码、插件与 Gutenberg 方法详解](https://www.361sale.com/wp-content/uploads/2024/12/20241203112705739-image.png)
visual comparison
- Button colors should contrast with the background to attract attention.
- A branded color palette is recommended, consistent with the overall style of the site.
clickability
- Ensure that the buttons are large enough to adapt to touchscreen devices.
- Use rounded corners to enhance visual comfort.
Testing and Optimization
- Check the display of buttons on different devices (e.g., phone, tablet, desktop).
- Make sure the button links work to avoid user experience issues.
![图片[12]-WordPress 按钮短代码完整指南:短代码、插件与 Gutenberg 方法详解](https://www.361sale.com/wp-content/uploads/2024/12/20241203112743245-image.png)
5. Frequently asked questions
Can I add buttons to the footer or sidebar?
Yes, it is possible to use shortcodes in widgets. For example, set the [button]
The short code is simply inserted into the text widget.
Can buttons be applied to custom post types?
Can. Just insert the shortcode in the custom post type and the effect will be the same as a regular post/page.
How can I further customize the button style?
By adding custom CSS, you can adjust button colors, fonts, animation effects, and more. Plug-ins that support advanced styles are also available.
Link to this article:https://www.361sale.com/en/28617The article is copyrighted and must be reproduced with attribution.
No comments