Jump to content

Subscribe Box in 'Body' - how?


aw.be
 Share

Recommended Posts

Good morning.

I'm trying to put a Mailchimp subscribe box in the body area (TinyMCE) of my basic-page template. I pasted the provided html in the appropriate spot but it's not working.

Do I have to place the code directly in my template file to get it to render? Because I want the box in the middle of the page's copy, the only thing I can think to do is create two fields - one for body copy above the signup form and one below. Can't think of another way to do it right now.

Thanks.

Link to comment
Share on other sites

Just to make sure, you've used the "HTML" button in TinyMCE and pasted the code there? Could you paste it here too, so that we can take a look and possibly determine what's the problem?

Anyway, the first thing I'd check would be body fields settings. Under Input > TinyMCE Advanced Configuration Options there's a valid_elements input. Check if all elements in your code are included there. If I remember correctly, this might not be the only thing that affects which HTML tags are allowed in a TinyMCE field, but that's a good starting point.

Link to comment
Share on other sites

Thanks teppo.

Here's the code for the form.

<!-- Begin MailChimp Signup Form -->

<div id="mc_embed_signup">

<form action="http://link here" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>

<label for="mce-EMAIL">Subscribe to my mailing list</label>

<input type="email" value="" name="EMAIL" class="email" id="mce-EMAIL" placeholder="email address" required>

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

</form>

</div>

<!--End mc_embed_signup-->

I'll have a look at the settings as you suggested.

Aaron

Link to comment
Share on other sites

you can also use a special tag and php to insert it in the right spot. lets say you put {{MCsignup}} where you want the form inside the body field, and in your template do:

$body = str_replace("{{MCsignup}}", $formCode, $page->body);
echo $body;

i'm on mobile and doing this by heart, so you should check the correct form of str_replace because i might have messed up the order :)

  • Like 1
Link to comment
Share on other sites

Thanks everyone.

Teppo - I tried your suggestion but it didn't seem to work. But then, I probably did it incorrectly.

Diogo - your suggestion seems almost like a shortcode which is what I'm most familiar with. But I need baby steps.

  1. Your code above would replace <?php echo $page->body; ?> in my page template? So this would add a textarea field with the ability to add {{MCsignup}}? Do I have all that correct?
  2. Where do I put the actual form code? Does that go into a separate template?

Thanks.

Link to comment
Share on other sites

Yes, that's what it does. to make it more clear, here is the complete code:

<?php

$formCode = <<<FORM
<!-- Begin MailChimp Signup Form -->
<div id="mc_embed_signup">
<form action="http://link here" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
<label for="mce-EMAIL">Subscribe to my mailing list</label>
<input type="email" value="" name="EMAIL" class="email" id="mce-EMAIL" placeholder="email address" required>
<div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
</form>
</div>
<!--End mc_embed_signup-->
FORM;

$body = str_replace("{{MCsignup}}", $formCode, $page->body);
echo $body;

?>
  • First we assign the form code to the variable $formCode using the heredoc sintax (look for it here http://php.net/manua...ypes.string.php to be aware of what it does and cautions to have while using it). Here I'm using <<<FORM and FORM; to delimit the code, but I could use any other word.
  • Then we find the chosen shortcode inside the body field and replace it by the form code with str_replace(), and assign the result to $body.
  • Finally we echo $body.

The shortcode could also be anything like ##myform## or **signup**. Just make sure it's something you wouldn't use inadvertently in the text.

  • Like 2
Link to comment
Share on other sites

OK - thanks diogo, I got it working. I was confused at first, though. Still having a rough time with php and how it works within PW's templates.

What if I wanted to be able to insert this form anywhere within my site - using a different template, for example?

Would I have to add the same code to each template, or could I put the code into it's own template and include it anywhere I wanted using this shortcode method?

Thanks.

Link to comment
Share on other sites

Still having a rough time with php and how it works within PW's templates.

There's nothing too complicated about it. PHP works in PW templates as it work everywhere else, if you understand how PHP works, you will also understand how it works with PW :)

Answering your question:

(although, honestly, I don't really get why you want to spread the form all over your fields. Did you consider having the form on a fixed place, like a sidebar?)

If you have a file that you include in all the templates where you need this (for example, head.inc in the default install), you can put the code on a function there, and call that function from any place that includes the file.

in head.inc

function subscribeForm($field){

$formCode = <<<FORM
<!-- Begin MailChimp Signup Form -->
<div id="mc_embed_signup">
<form action="http://link here" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
<label for="mce-EMAIL">Subscribe to my mailing list</label>
<input type="email" value="" name="EMAIL" class="email" id="mce-EMAIL" placeholder="email address" required>
<div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
</form>
</div>

<!--End mc_embed_signup-->
FORM;

$result = str_replace("{{MCsignup}}", $formCode, $field);
return $result;

}

in any template that includes head.inc

echo subscribeForm($page->body); //you can use any field here

The alternative would be to build a textFormatter module, but I think this case doesn't justify.

Link to comment
Share on other sites

Thanks diogo - what I meant was, I'm having a hard time with php. ??? Not necessarily PW.

Having multiple instances of a simple form can help improve conversions on longer pages. That's why I need to be able to insert it in different places.

I'll try a few different solutions to see what works best. Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...