dongtong0796 2015-08-11 11:11
浏览 52
已采纳

Wordpress全局变量,我可以在管理面板中编辑

i have one question about WP global variables. I have implemented if function into my header.php file. This is very simple function that would need to check variable value and show the correct data, depending on value.

This is if function:

<?php

if ($spon == 0)
{
echo 'number 0';
}
    elseif ($spon == 1)
{
    echo 'number 2';
}
else
{
    echo 'number 3';
}
?>

This function works fine for my needs, but now i would like to create global variable that i can change the value of it in admin panel. Is that possible and how ?

  • 写回答

1条回答 默认 最新

  • duanhaodi4809 2015-08-11 14:36
    关注

    I would recommend you the Options Framework.

    If you still do want to do it by yourself, here is a long description.

    https://codex.wordpress.org/Creating_Options_Pages

    On this Wordpress-Codex Page is everything you need and you should try it - if you still have any questions, just ask again.

    But just as a summary, these are the steps you have to do:

    • Create/register Options Page, like add_options_page() or add_menu_page()
    • register Settings (add fields you need)
    • Sanitize and save the user-inputs

    On the other part you get your saved option (check if you have already saved it …) simply with:

    <?php echo get_option( $option, $default ); ?> 
    

    e.g.

    <?php echo get_option( 'blogname' ); ?>
    <?php echo get_option( 'myOption' ); ?>
    

    It's actually not that hard .. but it's much easier with the Options-Framework.

    http://wptheming.com/options-framework-theme/

    Here is the ready-to-go-but-understand-it-before-you-use-it-code as in the Codex:

    <?php
    class MySettingsPage
    {
        /**
         * Holds the values to be used in the fields callbacks
         */
        private $options;
    
        /**
         * Start up
         */
        public function __construct()
        {
            add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
            add_action( 'admin_init', array( $this, 'page_init' ) );
        }
    
        /**
         * Add options page
         */
        public function add_plugin_page()
        {
            // This page will be under "Settings"
            add_options_page(
                'Settings Admin', 
                'My Settings', 
                'manage_options', 
                'my-setting-admin', 
                array( $this, 'create_admin_page' )
            );
        }
    
        /**
         * Options page callback
         */
        public function create_admin_page()
        {
            // Set class property
            $this->options = get_option( 'my_option_name' );
            ?>
            <div class="wrap">
                <h2>My Settings</h2>           
                <form method="post" action="options.php">
                <?php
                    // This prints out all hidden setting fields
                    settings_fields( 'my_option_group' );   
                    do_settings_sections( 'my-setting-admin' );
                    submit_button(); 
                ?>
                </form>
            </div>
            <?php
        }
    
        /**
         * Register and add settings
         */
        public function page_init()
        {        
            register_setting(
                'my_option_group', // Option group
                'my_option_name', // Option name
                array( $this, 'sanitize' ) // Sanitize
            );
    
            add_settings_section(
                'setting_section_id', // ID
                'My Custom Settings', // Title
                array( $this, 'print_section_info' ), // Callback
                'my-setting-admin' // Page
            );  
    
            add_settings_field(
                'id_number', // ID
                'ID Number', // Title 
                array( $this, 'id_number_callback' ), // Callback
                'my-setting-admin', // Page
                'setting_section_id' // Section           
            );      
    
            add_settings_field(
                'title', 
                'Title', 
                array( $this, 'title_callback' ), 
                'my-setting-admin', 
                'setting_section_id'
            );      
        }
    
        /**
         * Sanitize each setting field as needed
         *
         * @param array $input Contains all settings fields as array keys
         */
        public function sanitize( $input )
        {
            $new_input = array();
            if( isset( $input['id_number'] ) )
                $new_input['id_number'] = absint( $input['id_number'] );
    
            if( isset( $input['title'] ) )
                $new_input['title'] = sanitize_text_field( $input['title'] );
    
            return $new_input;
        }
    
        /** 
         * Print the Section text
         */
        public function print_section_info()
        {
            print 'Enter your settings below:';
        }
    
        /** 
         * Get the settings option array and print one of its values
         */
        public function id_number_callback()
        {
            printf(
                '<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />',
                isset( $this->options['id_number'] ) ? esc_attr( $this->options['id_number']) : ''
            );
        }
    
        /** 
         * Get the settings option array and print one of its values
         */
        public function title_callback()
        {
            printf(
                '<input type="text" id="title" name="my_option_name[title]" value="%s" />',
                isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
            );
        }
    }
    
    if( is_admin() )
        $my_settings_page = new MySettingsPage();
    

    Have fun, and please understand what you do!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历