如何将函数中提交的值用于另一个函数,然后在wordpress插件中使用短代码使用该值

时间:2018-02-05 10:07:15

标签: php wordpress wordpress-theming

我希望使用提交的值[ $ x ]连接“您好”并使用短代码在页面或帖子中显示。我正在尝试将值[ $ x ]发送到另一个函数[ display_output($ x)],然后使用该函数[ display_output ] add_shortcode()中的名称,但它只显示“您好”而不是提交的值[ $ x ]。如果我的方法有误,那么有没有其他方法可以使用短代码在页面或帖子中使用提交的值。我是wordpress插件开发的新手。提前谢谢。

<?php
/*
Plugin Name:  Iron Man
Plugin URI:   https://rajbiswas.com
Description:  My Plugin Dev.
Version:      0.1
Author:       RB

*/

/*Adding option page in dashboard*/
function my_plugin_settings()
{
    add_menu_page( 'My Plugin',
                    'myCust Form',
                    'administrator',
                    'insert-my-plugin_bro',
                    'my_plugin_settings_page',
                    'dashicons-translation',
                    '60'
                );  
}
add_action('admin_menu', 'my_plugin_settings');

/*registering input group and field*/
function my_plugin_options()
{
    register_setting('my-form-group','user_input_name');
}
add_action('admin_init', 'my_plugin_options');



function my_plugin_settings_page()
{
    ?>
    <h1>hello fellas</h1>

    <form  action="options.php" method="post">
        <?php settings_fields('my-form-group'); ?>

        <input type="text" name="user_input_name" value="<?php echo esc_attr(get_option('user_input_name')) ?>" >
        <?php //submit_button(); ?>
        <input type="submit" value="submit" name="submit" >
    <form>  

    <?php
    $x = get_option('user_input_name');
    display_output($x);     //Sending $x to another function and trying to use it using shortcode. 
}

function display_output($x)
{   
    echo "HI"." ".$x;   //I want to concatenate "Hi" with $x and print it in page/post wherever I use shortcode([test-shortcode]). But it is not working.
}
add_shortcode('test-shortcode', 'display_output');
?>

1 个答案:

答案 0 :(得分:0)

如果您要保存输入选项,请尝试在短代码功能中检索此选项。您可以全局使用get_option

function display_output($x)
{   
    $x = get_option('user_input_name');
    echo "HI"." ".$x;   //I want to concatenate "Hi" with $x and print it in page/post wherever I use shortcode([test-shortcode]). But it is not working.
}
add_shortcode('test-shortcode', 'display_output');