2

Is there a way to prevent more than one shortcode to run in the same page? If shortcode is already been executed not to run another.

add_shortcode('foo', 'foo_add_player');
function foo_add_player($atts){
   //do not run twice
}

1 Answer 1

3

Set a flag like $already_run to static and give it an initial value false. Then check if that value is true. If not, do the one-time thing and then set $already_run to true. The next time this function is called, it will not re-assign the static property, but will instead use the value set at end of code. So it will skip second and subsequent calls.

function foo_add_player($atts) {
    static $already_run = false;
    if ( $already_run !== true ) {
        // do stuff here
    }
    $already_run = true;
}

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.