0

I have a subscribe form where users can subscribe to receive news from the desired post categories. Each category have two options of receiving/sending news: daily or weekly. This form is displayed by a shortcode on a Subscribe page. Now, when a post of some category is opened, I want to display (conditionally?) at the end of that post the same subscribe form (so no need to have it in multiple copies) only with the email field and the daily and weekly options of corresponding post category, hiding the rest of the form. The question is how to adapt the form/shortcode code so I will be able to use it further as now, displaying it fully, but also in a scenario described above?

The code (removed some non-important elements):

function mailchimp_form_shortcode() { ?>

<div id="mc_embed_signup">
    <form action="..." method="post">
        <div id="mc_embed_signup_scroll">

            <div class="container">

                <div class="mc-field-group">
                    <label for="mce-EMAIL">E-mail: <span class="asterisk"> (required)</span></label>
                    <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="your e-mail address">
                </div>

                <strong>Subscribe to categories: </strong>

                <div class="layout">
                    <p class="col col-main" role="main"> - News Category 1</p>
                    <p class="col col-complementary" role="complementary">
                        <input type="checkbox" value="4" name="group[...][4]" id="mce-group[...]-...-2"> daily<span class="asterisk">*</span>
                        <input type="checkbox" value="16" name="group[...][16]" id="mce-group[...]-...-3"> weekly
                    </p>
                </div>

                <div class="layout">
                    <p class="col col-main" role="main"> - News Category 2</p>
                    <p class="col col-complementary" role="complementary">
                        <input type="checkbox" value="2" name="group[...][2]" id="mce-group[...]-...-4"> daily<span class="asterisk">*</span>
                        <input type="checkbox" value="32" name="group[...][32]" id="mce-group[...]-...-5"> weekly
                    </p>
                </div>

                <div class="layout">
                    <p class="col col-main" role="main"> - News Category 3</p>
                    <p class="col col-complementary" role="complementary">
                        <input type="checkbox" value="64" name="group[...][64]" id="mce-group[...]-...-6"> daily<span class="asterisk">*</span>
                        <input type="checkbox" value="128" name="group[...][128]" id="mce-group[...]-...-7"> weekly
                    </p>
                </div>

            <div class="clear">
                <input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button">
            </div>

            </div>

        </div>
    </form>
</div>

<?php }

add_shortcode('mailchimp', 'mailchimp_form_shortcode');
1

In your function mailchimp_form_shortcode(), you need to do following changes (specifically first line inside the function):

<?php
function mailchimp_form_shortcode($atts) {
    extract(shortcode_atts(array(), $atts));

    if(isset($attrs['type']) && $attrs['type'] == 'some_value'){
            //return "form element";
    }

}
add_shortcode('mailchimp', 'mailchimp_form_shortcode');
?>

and your shortcode will be as:

[mailchimp type="some_value"]

By this, you can pass parameters to your shortcode function. And inside that shortcode function, you can display the form elements based on the passed parameters.

Hope this will help you.

|improve this answer|||||
  • 1
    obviously you need to return not display. It is also partial answer – Mark Kaplun Oct 11 '17 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.