0

I'm trying to write reusable functions in a plugin and call the functions within a shortcode. But, I can't get any output. Why don't either of these methods work?

    <?php

    function say_sup(){
    return 'sup';
    }

    add_shortcode( 'sup_shortcode', 'register_sup_shortcode' );
    function register_sup_shortcode( $atts, $content = null) {

    $sup = say_sup();
    return $sup;

    }

    ?>



    <?php

    function say_sup(){
        $sup = 'sup';
        echo $sup;
        var_dump( $sup );
    }

    add_action( 'say_sup_now', 'say_sup', 1 );

    add_shortcode( 'sup_shortcode', 'register_sup_shortcode' );
    function register_sup_shortcode( $atts, $content = null) {

    do_action( 'say_sup_now' );

    }

    ?>
  • Your first example is fine. Is this the actual code you’re having trouble with? – Jacob Peattie Nov 1 '18 at 15:19
  • I guess you forgot to write echo before do_shortcode(). It should be echo do_shortcode('[your-shorcode]') ; – KAGG Design Nov 1 '18 at 17:16
  • @KAGGDesign They haven't written do_shortcode() anywhere? – Jacob Peattie Nov 2 '18 at 1:09
0

This is correct (I just had file versioning issues):

        <?php


        function say_sup(){
        return 'sup';
        }

        add_shortcode( 'sup_shortcode', 'register_sup_shortcode' );
        function register_sup_shortcode( $atts, $content = null) {


        $sup = say_sup();
        return $sup;


        }

        ?>

This is not correct - and there would be little purpose in trying to make something like this work, if the above worked:

        <?php


        function say_sup(){
            $sup = 'sup';
            echo $sup;
            var_dump( $sup );
        }


        add_action( 'say_sup_now', 'say_sup', 1 );


        add_shortcode( 'sup_shortcode', 'register_sup_shortcode' );
        function register_sup_shortcode( $atts, $content = null) {


        do_action( 'say_sup_now' );


        }

        ?>

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.