0

I created the following shortcode:

function newsletter_signup_shortcode( $atts ) {
$post_type = get_post_type();

// Attributes
extract( shortcode_atts(
    array(
        'location' => get_permalink(),
        'show_title' => 'yes',
    ), $atts )
);

// Return the unfiltered content if we're not on a post.
if ( $post_type != 'post' ) {
    return $content;
}

ob_start();
?>

<!-- MailChimp Signup Form outputs here - code omitted for clarity -->

<?php
// Add the opt-in form after the post content.
return $content . ob_get_clean();
}
add_shortcode( 'newsletter_signup', 'newsletter_signup_shortcode' );

This works great on the home page and blog post pages, within the post and in the sidebar widget.

However, when I view a page, the sidebar widget does not display the shortcode content:

What am I missing here?

1

You have a conditional in your function that will skip (return) if the current "view" is not a single post. You also have an undefined variable $content - the following has the condition removed and undefined variable fixed (always, always have debugging on when building with WordPress):

function newsletter_signup_shortcode( $atts ) {
    $post_type = get_post_type(); // Not sure if you still need this for your MailChimp template?

    // Attributes
    extract( shortcode_atts(
        array(
            'location' => get_permalink(),
            'show_title' => 'yes',
        ), $atts )
    );

    ob_start();
    ?>

    <!-- MailChimp Signup Form outputs here - code omitted for clarity -->

    <?php
    return ob_get_clean();
}
  • Thank you. Don't know why I was missing that... – Yazmin Nov 5 '15 at 19:03

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.