customization - How to create a shortcode with HTML code in it and custom parameters

I want to create a shortcode for putting newsletter opt-in boxes in my posts and pages. But depending on the page or pos

I want to create a shortcode for putting newsletter opt-in boxes in my posts and pages. But depending on the page or post, I want to have different headline and description for the opt-in box.

I created a shortcode-newsletter.php file with this code in it:

<?php

function newsletter(){

return <<<HTML

    <div class="newsletterForm">

        <h3>Newsletter Box Headline</h3>

        <p>Newsletter Box Description</p>

        <form method="post" action="">

            <input type="hidden" name="ml-submit" value="1">

                <div class="newsletterFields">

                    <div class="newsletterEmail">

                        <input required="" type="email" class="newsletterInput" name="fields[email]"
                            placeholder="Your email...">
                    </div>

                    <div class="newsletterButton">

                        <input type="submit" class="newsletterSubmit" value="Subscribe">

                    </div>

                </div>

                <div class="newsletterDisclaimer">

                    <p>Newsletter Box Disclaimer</p>

            </div>

        </form>

    </div>
HTML;
}
?>

In functions.php I added:

include('shortcode-newsletter.php');
add_shortcode( 'newsletter', 'newsletter' );

Now I want to have a shortcode with custom parameters:

[newsletter url="formActionUrl" headline="newsletterHeadline" description="newsletterDescription"]

But I don't know how to change the first code to have those parameters customizable.

Thank you for your help!

I want to create a shortcode for putting newsletter opt-in boxes in my posts and pages. But depending on the page or post, I want to have different headline and description for the opt-in box.

I created a shortcode-newsletter.php file with this code in it:

<?php

function newsletter(){

return <<<HTML

    <div class="newsletterForm">

        <h3>Newsletter Box Headline</h3>

        <p>Newsletter Box Description</p>

        <form method="post" action="https://domain/newsletter/z2o7q6">

            <input type="hidden" name="ml-submit" value="1">

                <div class="newsletterFields">

                    <div class="newsletterEmail">

                        <input required="" type="email" class="newsletterInput" name="fields[email]"
                            placeholder="Your email...">
                    </div>

                    <div class="newsletterButton">

                        <input type="submit" class="newsletterSubmit" value="Subscribe">

                    </div>

                </div>

                <div class="newsletterDisclaimer">

                    <p>Newsletter Box Disclaimer</p>

            </div>

        </form>

    </div>
HTML;
}
?>

In functions.php I added:

include('shortcode-newsletter.php');
add_shortcode( 'newsletter', 'newsletter' );

Now I want to have a shortcode with custom parameters:

[newsletter url="formActionUrl" headline="newsletterHeadline" description="newsletterDescription"]

But I don't know how to change the first code to have those parameters customizable.

Thank you for your help!

Share Improve this question asked Jun 30, 2019 at 7:32 Pawel KadyszPawel Kadysz 31 bronze badge 1
  • Have you read the documentation? – Jacob Peattie Commented Jun 30, 2019 at 7:38
Add a comment  | 

1 Answer 1

Reset to default 0

As per the shortcode documentation, your shortcode callback recieves any parameters set by the user in a $atts variable. You can combine them with any default values you might have with shortcode_atts().

function newsletter( $atts ) {

  $atts = shortcode_atts( 
    array(
      'url'         => 'https://domain/newsletter/z2o7q6', // change default value as needed
      'headline'    => 'Newsletter Box Headline', // change default value as needed
      'description' => 'Newsletter Box Description', // change default value as needed
    ), 
    $atts, 
    'newsletter' 
  );

  // you can access parameters with $atts['url'], $atts['headline'], and $atts['description']
  // e.g <h3><?php echo esc_html($atts['headline']); ?></h3>

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745358340a4624244.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信