dongxiong5546 2018-06-01 20:55
浏览 41
已采纳

PHP Wordpress插件变量不响应内联样式

I am developing a Wordpress Plugin and It has a shortcode that I am using to print out content to a page/post.

In my plugin file I have initiated all menu pages and settings, I have 2 settings in the plugin admin page and one of them is Brand Color so users can change the branding color of the form the shortcode is spitting out.

The data is saved in the wp-options table in wordpress and all working ok, the problem is when i try to echo out the variable content nothing is happening?

What I have done:

I first added a Global array variable at the top of my Plugin php file:

    $options = array();

    options_init(){

        if(current_user_can('manage_options')):

           global  $options;

            if(isset($_POST['form_submitted'])){

              $hidden = esc_html($_POST['form_submitted']);

                if($hidden == 'Y'){

                    $Brand = $_POST['brand_color'];
                    $options['brand_color'] = $Brand;

                    update_option('general_info', $options);
                }   

            }
                $options = get_option('general_info');

         endif
     }

So now I have stored the data I then retreive the data and store it in my options variable. In another function I create the shortcode function and content to show on the screen, when the shortcode is used.

I first call my global variable then the output for the shortcode content.

add_shortcode('my-form', 'budget_form');
function budget_form() {
global  $options;   
  ?>
<div class="monkey-budget-planner-wrapper">
        <div class="monkey-header-wrapper" style="background:<?php echo $options['brand_color'] ?>">
       </div>
</div>

    <?php
}

Th problem is that the style is not being output, there is no errors and in the inspector i see the output is blank. I am not sure what I am doing wrong?

  • 写回答

1条回答 默认 最新

  • douzhuo5671 2018-06-01 21:12
    关注

    Why not? You can combine some of your conditions also.

      options_init(){
    
            if(current_user_can('manage_options') && isset($_POST['form_submitted'])):
    
                  $hidden = esc_html($_POST['form_submitted']);
    
                    if( $hidden == 'Y'){
    
                        $brand = $_POST['brand_color'];
    
                        update_option('brand_color', $brand);
                    }   
    
    
             endif
         }
    

    Than you can use

        add_shortcode('my-form', 'budget_form');
    
            function budget_form() { 
    
              $color = get_option('brand_color'); ?>
    
              <div class="monkey-budget-planner-wrapper">
                     <div class="monkey-header-wrapper" style="background:<?php echo $color ?>"></div>
              </div>
    
        <?php } ?>
    

    if you need more than one option set, than just create more with update_option(), if you are going to set page specific values, I would suggest update_post_meta and get_post_meta(). The update_option() is normally for data at the theme level.

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

报告相同问题?

悬赏问题

  • ¥20 c语言写的8051单片机存储器mt29的模块程序
  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大