Skip to content

Adding a CTA (Call‑to‑Action) 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.

  • 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).
  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.

  • 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.

  • 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.