1

The shortcode for the MailChimp subscribe form in my theme's custom homepage is not working. But when I put the same shortcode in a blog page and other pages, then it is working

I put the shortcode [mc4wp_form id="id"] in theme pages and it's working.

But when I put <?php echo do_shortcode ([mc4wp_form id="id"]); ?> in my custom homepage, then it's not working.

thanks

2 Answers 2

3

The common way to approach this is to use do_shortcode(). But that's not an efficient way to do it because it has to run a pretty extensive regex (regular expression) to parse through every single shortcode in your WP install to get to the one you are asking for. See this post for a more thorough explanation.

A better approach is to run the callback function needed directly. But that can sometimes be a challenge - either you have to dig through a lot of code to find it, or it may possibly be in an object class and how do you call that?

J.D. Grimes has provided a good utility function for calling shortcodes this way so that you can get to the direct callback function without having to use do_shortcode(). Add the following function, which you can use for any shortcode instance:

/**
 * Call a shortcode function by tag name.
 *
 * @author J.D. Grimes
 * @link https://codesymphony.co/dont-do_shortcode/
 *
 * @param string $tag     The shortcode whose function to call.
 * @param array  $atts    The attributes to pass to the shortcode function. Optional.
 * @param array  $content The shortcode's content. Default is null (none).
 *
 * @return string|bool False on failure, the result of the shortcode on success.
 */
function do_shortcode_func( $tag, array $atts = array(), $content = null ) {
 
    global $shortcode_tags;
 
    if ( ! isset( $shortcode_tags[ $tag ] ) )
        return false;
 
    return call_user_func( $shortcode_tags[ $tag ], $atts, $content, $tag );
}

Then you can call your shortcode this way:

echo do_shortcode_func( 'mc4wp_form', array( 'id' => 'id' ) );
1

You're missing quotes around the shortcode in the do_shortcode() function:

<?php echo do_shortcode( '[mc4wp_form id="id"]' ); ?>

do_shortcode() is a PHP function that takes a String as an argument. The quotes are required to make the text a string.

4
  • still not working Commented Oct 12, 2018 at 12:43
  • What is this "custom front page" you're referring to? A page template? Is it in you theme folder? Commented Oct 12, 2018 at 13:02
  • yes it is in theme folder and has name tpl-home.php Commented Oct 12, 2018 at 13:06
  • I'm using wp-generic theme Commented Oct 12, 2018 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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