$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugins - Return function results within shortcode|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - Return function results within shortcode

matteradmin10PV0评论

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' );

    }

    ?>

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' );

    }

    ?>
Share Improve this question edited Nov 4, 2018 at 11:55 James Valeii asked Nov 1, 2018 at 14:50 James ValeiiJames Valeii 1185 bronze badges 3
  • Your first example is fine. Is this the actual code you’re having trouble with? – Jacob Peattie Commented Nov 1, 2018 at 15:19
  • I guess you forgot to write echo before do_shortcode(). It should be echo do_shortcode('[your-shorcode]') ; – KAGG Design Commented Nov 1, 2018 at 17:16
  • @KAGGDesign They haven't written do_shortcode() anywhere? – Jacob Peattie Commented Nov 2, 2018 at 1:09
Add a comment  | 

1 Answer 1

Reset to default 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' );


        }

        ?>
Post a comment

comment list (0)

  1. No comments so far