• Is there a way to test for shortcode output and perform actions based on that?

    I’ve tried this code, but either it doesn’t run at all or it generates PHP out-of-memory errors.

    if ( do_shortcode( '[so_show_offers]' ) != NULL ) {
        // take action
    }

    Use case: I’m trying to redirect to a specific page if the shortcode has content; if not, then redirect to a different page.

    Thanks

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You have the right idea, but shortcode handlers should never return NULL, they should always return a string, though it might be an empty string. Simply calling do_shortcode() should not cause memory errors unless there’s something faulty with an involved shortcode handler function. For example, if the handler itself calls do_shortcode() and the passed string contains shortcode as well, an infinite loop condition could be created, quickly chewing up memory.

Viewing 1 replies (of 1 total)
  • The topic ‘Testing for shortcode output’ is closed to new replies.