BenDains Posted March 5, 2014 Share Posted March 5, 2014 (edited) Good afternoon everyone, I am 24 and I just started my first website (yay me, right) and I had a small question in regards to PHP. I am editing a Wordpress template (don't worry i've already made a child theme and copied the header.php), but I want to add (embed) an HTML form (I am using MailChimp as my newsletter & mailing service) into my header.php I am unsure which way to go about this problem and I wanted some excellent advice from the pros (you all!). After researching the problem I have concluded there are two ways to accomplish this (remember I am a newbie and there are probably more ways to accomplish this task, ha ha). 1. Use the PHP Command - Echo to write the code into the header.php but I feel that would be a lot of Echo commands (and it may not be because I don't know very much about PHP). 2. Create a new function.php file that houses my "Newsletter Form" and use PHP to call that function (and now that I am typing this, it probably isn't a "function" file and its named something else) into my header.php and have it display that way. This is my website: www.hulkapps.com The "Newsletter Form" should appear under the site description ("We Make College Easier"). Thank you in advance for all responses, and taking the time to read my post. Have a great day,Ben @ HulkApps Edited March 5, 2014 by BenDains Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 5, 2014 Share Posted March 5, 2014 PHP allows you to easily jump in and out of HTML. <!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <h1> <?php echo 'It's ' . date(); ?> </h1> </body> You can also use a heredoc in circumstances like the one you describe: $form = <<<HERE <form id="newsletter"> ..... Form code </form> HERE; echo $form; Heredocs will even interpolate variables, so they are an excellent hybrid option. If you have pure html, then I'd drop into HTML at the point you need to include it. Quote Link to comment Share on other sites More sharing options...
dalecosp Posted March 5, 2014 Share Posted March 5, 2014 There are lots of ways to do this, and several of them might even a] work, and b] make sense.First off, there's "include()", which is great for, well, including some code in some other place: <?php //my_includer.php //a PHP function, and another PHP function do_something(); do_something_else(); include "myHTML.html"; //"myHTML.html's content will appear right at this spot. This file is in the same directory as "my_includer.php" ?> Now, contrary to your statement above, there's no reason to use echo over and over ... as long as you understand how to make echo work over multiple lines, and realize that the delimiting characters for the string cannot be in the string itself. <?php //my_html_echoing.php //a PHP function do_yet_something_more($with_an_argument); //echo my HTML here echo " <ul> <li class='foo'>Item</li> <li class='foo'>Item 2</li> <li class='foo'>Item 3</li> <li class='foo'>Item 4</li> </ul> "; //note that THIS statement (below) will cause a parse error because it contains //the double-quote, which I'm using to delimit the string for echo() to use... echo " <ul> <li class="bad-quote">Item 5</li> </ul> "; //done properly, the above should use single-quotes for the LI class, or //single-quotes for the string delimiter. This won't create an error: echo ' <ul> <li class="bad-quote">Item 5</li> </ul> '; Quote Link to comment Share on other sites More sharing options...
dalecosp Posted March 5, 2014 Share Posted March 5, 2014 Incidentally, both of the options you describe would also work, but tend to be "more work", which we programmers don't like so much ;) Quote Link to comment Share on other sites More sharing options...
BenDains Posted March 5, 2014 Author Share Posted March 5, 2014 Thanks for the great answers! Just to be clear, it IS easier to just use the "include()" command? After you explained it, that does make sense. Just speaking openly, I should create very basic html page which has my form, and then "include()" it in my header.php? Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 5, 2014 Share Posted March 5, 2014 Thanks for the great answers! Just to be clear, it IS easier to just use the "include()" command? After you explained it, that does make sense. Just speaking openly, I should create very basic html page which has my form, and then "include()" it in my header.php? Yes, however, the file should be named something like newsletter.php or newsletter.inc.php. It should include just the HTML, utilizing the technique I illustrated above. It sounds like you don't need any php in there, but you can drop into php if you need to inside the file. When you include it, php will insert the html at that point in your header file. Quote Link to comment Share on other sites More sharing options...
.josh Posted March 5, 2014 Share Posted March 5, 2014 Why aren't you building your form within the wp-admin interface? That's like, the whole point of using a CMS.. Quote Link to comment Share on other sites More sharing options...
BenDains Posted March 5, 2014 Author Share Posted March 5, 2014 (edited) Alright, so I have tried so many different things. I am not sure what I am doing wrong and I am kind of going insane.. This is the code for header.php and I highlighted where I the form to go. <?php $class = 'no-cover'; if ( is_front_page() || is_archive() || is_search() || is_home() ) { $class = 'cover'; } if ( is_single() ) { $post_thumbnail_url = wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ); if ( ! empty($post_thumbnail_url) ) { $class = 'cover'; ?> <style> .banner.cover { background-image: url( <?php echo $post_thumbnail_url; ?> ); } </style> <?php } } ?> <header class="banner <?php echo $class ?>" role="banner"> <div class="header-inner"> <nav class="nav-main" role="navigation"> <div class="container"> <?php if (has_nav_menu('primary_navigation')) : wp_nav_menu(array('theme_location' => 'primary_navigation', 'menu_class' => 'nav navbar-nav')); endif; ?> </div> </nav> <?php if( is_front_page() || is_archive() || is_home() ) : ?> <hgroup> <div class="container"> <h1 class="page-title"> <?php echo dw_timeline_title(); ?> </h1> <h2 class="page-description"><?php bloginfo('description'); ?></h2> Im trying to get it to appear here ----------------------------------- <button id="get-started" class="btn btn-default btn-coner"><?php echo dw_timeline_get_theme_option('get_start','Get Start Now') ?></button> </div> </hgroup> <?php elseif( is_search() ) : ?> <div class="container"> <h1 class="page-title"> <?php echo dw_timeline_title(); ?> </h1> </div> <?php endif; ?> </div> </header> and this is the stupid HTML from MailChimp <!-- Begin MailChimp Signup Form --> <link href="//cdn-images.mailchimp.com/embedcode/slim-081711.css" rel="stylesheet" type="text/css"> <style type="text/css"> #mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; } /* Add your own MailChimp form style overrides in your site stylesheet or in this style block. We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */ </style> <div id="mc_embed_signup"> <form action="http://hulkapps.us6.list-manage2.com/subscribe/post?u=0ae3bf2251c52f99a359c13c2&id=32aad6d49f" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate> <label for="mce-EMAIL">Subscribe to our mailing list</label> <input type="email" value="" name="EMAIL" class="email" id="mce-EMAIL" placeholder="email address" required> <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups--> <div style="position: absolute; left: -5000px;"><input type="text" name="b_0ae3bf2251c52f99a359c13c2_32aad6d49f" value=""></div> <div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div> </form> </div> <!--End mc_embed_signup--> I do not want anyone to write it the code for me because I am trying to learn, but this one has me stumped.. :/ Why aren't you building your form within the wp-admin interface? That's like, the whole point of using a CMS.. Because this theme doesn't support a form at the place I want to put it, if that makes sense. But then again, I could be just a dumb newbie and I am making this WAY TO COMPLICATED. Haha. Edited March 5, 2014 by BenDains Quote Link to comment Share on other sites More sharing options...
BenDains Posted March 6, 2014 Author Share Posted March 6, 2014 Just wanted to bump this so you all could see it again, not sure if everyone was able to read my last comment. I'll only bump once. Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 6, 2014 Share Posted March 6, 2014 What have you tried? I suggested that you: Put the mail chimp code into a file named mailchimp.inc.php There should NOT be a <?php at the top. Simply put that code exactly as is, in the file. This file should be in the same directory as your template. In the template add: include('mailchimp.inc.php'); Quote Link to comment Share on other sites More sharing options...
dalecosp Posted March 6, 2014 Share Posted March 6, 2014 Well, it would have to be "<?php include('mailchimp.inc.php'); ?>" ... he's in HTML mode at that point. Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 7, 2014 Share Posted March 7, 2014 Well, it would have to be "<?php include('mailchimp.inc.php'); ?>" ... he's in HTML mode at that point. In that template he provided, yes, good point. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.