Reputation:
I have the following website: https://www.ajagrawal.com
Currently in my website, I am trying to edit the hero section and replace the name, email and button fields with a custom wordpress form that was made with the plugin. Right now the emails are stored in Wordpress, but I want them in Mailchimp.
I know which code in my theme that form pertains to which is the following:
if(function_exists('newsletter_form')):
$output .= '<div class="row">';
$output .= '<div class="col-sm-6 col-sm-offset-6 col-md-4 col-md-offset-6">';
$output .= '<form method="post" action="'.home_url('/').'?na=s" onsubmit="return newsletter_check(this)">';
$output .= '<div class="c-input-3-wrapper">';
$output .= '<input class="c-input type-3" type="text" name="nn" required="" placeholder="'.esc_html($name_placehodler).'">';
$output .= '<div class="c-input-3-icon"><span class="lnr lnr-user"></span></div>';
$output .= '</div>';
$output .= '<div class="c-input-3-wrapper">';
$output .= '<input class="c-input type-3" type="email" name="ne" required="" placeholder="'.esc_html($email_placeholder).'">';
$output .= '<div class="c-input-3-icon"><span class="lnr lnr-envelope"></span></div>';
$output .= '</div>';
$output .= '<input class="newsletter-submit c-btn type-1 size-4 full" type="submit" value="'.esc_html($btn_text).'">';
$output .= '</form>';
$output .= '</div>';
$output .= '</div>';
endif;
Here is my mailchimp shortcode that represents the form.
[mc4wp_form id="2198"]
I've tried editing the code and inserting it multiple ways and even ended up crashing the site and still no luck. Considering my goal, is this the right approach (putting the shortcode in the PHP code)?
If so, how can this be done?
Upvotes: -1
Views: 1281
Reputation: 2760
You can use the do_shortcode
function.
Examples in the documentation:
// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );
// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
// Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );
// Use shortcodes in form like Landing Page Template.
echo do_shortcode( '[contact-form-7 id="91" title="quote"]' );
// Store the short code in a variable.
$var = do_shortcode( '' );
echo $var;
So in your case something like:
echo do_shortcode( '[mc4wp_form id="2198"]' );
should do the trick.
EDIT: Something like this should work:
$output .= '<div class="row">';
$output .= '<div class="col-sm-6 col-sm-offset-6 col-md-4 col-md-offset-6">';
$output .= do_shortcode( '[mc4wp_form id="2198"]' );
$output .= '</div>';
$output .= '</div>';
Then do whatever you want with output - probably will have to echo it...
Upvotes: 1