WordPress.org

Forums

Adding shortcode outside loops if function exists (3 posts)

  1. micasuh
    Member
    Posted 4 years ago #

    I am writing a custom feature for my theme to populate the footer with a credit link to my business site for my clients. I've been using an admin panel which I enable the credit link so that it shows up.

    To keep it simple (since it's way more complicated), I've created the follow shortcode:

    function add_footer_credit() {
    return bloginfo( 'name' ).' is maintained by <a href="http://business.com">My Business</a>.';
    add_shortcode('footer_credit', 'add_footer_credit');

    In the footer.php, I have the following:
    <?php if (function_exists('add_footer_credit')) { echo do_shortcode( '[footer_credit]' ); } ?>

    Unfortunately, when I don't have the shortcode activated in my admin panel, the footer still shows [footer_credit]. I'd prefer for it to show nothing when the shortcode isn't activated. Somehow, it's still showing up even when I haven't activated it in the admin panel.

  2. micasuh
    Member
    Posted 4 years ago #

    I missed a close bracket in the initial shortcode so don't pay attention to that. The code works great when activated, I just want to make sure that when NOT activated nothing shows up instead of the [footer_credit] text (meaning it's not rendering when there's nothing to render).

  3. Kezz Bracey
    Member
    Posted 4 years ago #

    Thanks for the snippet! I'm using this in a theme designed to work with a specific plugin for a client, very helpful. :)

Topic Closed

This topic has been closed to new replies.

About this Topic