# Adding a call-to-action (CTA) button shortcode in WordPress

> Create a reusable call-to-action button shortcode in functions.php and insert styled buttons anywhere in WordPress.

Source: https://helpcenter.ruachost.com/wordpress/guides/adding-a-cta-calltoaction-button-shortcode-in-wordpress/

A **CTA button shortcode** allows you to insert customizable buttons into posts and pages without writing HTML each time. On **ruachost.com**, you can create a shortcode in your theme’s `functions.php` file and use it anywhere in your content.

## Why Use a CTA Shortcode?

- Encourages visitors to take action (subscribe, buy, contact).
- Saves time by reusing the same button style across multiple pages.
- Provides flexibility with customizable attributes (color, size, link).

## Steps to Add a CTA Button Shortcode

1. **Log in to WordPress** with an administrator account.
2. In the dashboard, go to **Appearance → Theme Editor**.
3. Select your active theme and open the **functions.php** file.
4. Add the following code snippet:

   ```
   // Register CTA button shortcode
   function wordpress_cta($atts) {
       $a = shortcode_atts(
           array(
               'link'   => 'https://ruachost.com/wordpress-hosting',
               'id'     => 'cta-btn',
               'color'  => 'blue',
               'size'   => '',
               'label'  => 'Click Here',
               'target' => '_self',
           ),
           $atts
       );

       $output = '<p><a href="' . esc_url($a['link']) . '"
           id="' . esc_attr($a['id']) . '"
           class="button ' . esc_attr($a['color']) . ' ' . esc_attr($a['size']) . '"
           target="' . esc_attr($a['target']) . '">'
           . esc_html($a['label']) . '</a></p>';

       return $output;
   }
   add_shortcode('cta_button', 'wordpress_cta');
   ```
5. Save the file.

## Using the Shortcode

- Open any post or page in WordPress.
- Insert the shortcode:

  ```
  [cta_button link="https://ruachost.com/signup" label="Get Started" color="green" size="large" target="_blank"]
  ```
- The button will appear with the specified attributes.

## Important Notes

- Always back up your theme before editing `functions.php`.
- Use a **child theme** to preserve changes during updates.
- Customize CSS for `.button` class to style buttons consistently.
