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!

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

报告相同问题?

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗