【问题标题】:Issue with adding a custom dynamic colour to WYSIWIG向所见即所得添加自定义动态颜色的问题
【发布时间】:2019-03-23 12:27:14
【问题描述】:

我正在尝试使用以下代码将变量添加到调色板。前两种颜色有效,但没有添加我的第三种颜色,正如您在我的代码中看到的那样; echo 将显示来自 CMS 的正确颜色,但当它进入函数时,此变量不起作用。

// Get current user identification
$current_user = wp_get_current_user();
$current_role = $current_user->roles[0];


// Loop through ACF repeater in the options
if( have_rows('group_colour', 'options') ):
    while ( have_rows('group_colour','options') ) : the_row();

        // Set the selected user role and the colour
        $colourrole = get_sub_field('user_role');
        $colourvalue = get_sub_field('role_colour');

            // If options user role matches the logged in user, get the colour and remove the #
            if ($current_role == $colourrole){
                $colour_no_hash = str_replace('#', '', $colourvalue);
                echo $colour_no_hash;



                // Customize the TinyMCE Color Palette. Attempting to add a colour for 'Custom Login'. Other 2 colours work.
                function wptb_tinymce_options($options) {

                    $custom_colours =  '"000000", "Black",
                                        "00AC9F", "Custom Teal",
                                        "' . $colour_no_hash . '", "Custom Login"';
                    $options['textcolor_map'] = '['.$custom_colours.']';
                    return $options;
                }
                add_filter('tiny_mce_before_init', 'wptb_tinymce_options');

            }

    endwhile;
else :
endif;

任何帮助表示赞赏。

【问题讨论】:

  • 我很好奇为什么 $custom_colours 不是 assoc 而是 CSV 字符串?作为一个副手会不会更容易?
  • 抱歉,我不知道你说的 assoc 是什么意思?
  • 对不起,我本来应该澄清的,但我觉得很懒。我的意思是一个关联数组,例如$custom_colours = [ '000000' => 'Black', [...] ]
  • @Script47 我会看看这个,我在网上找到了这个改变所见即所得颜色的例子,并希望我能将我的代码集成到其中。这会很遥远吗?
  • TinyMCEcolor picker plguin 一起使用会不会更容易?

标签: php wordpress function advanced-custom-fields


【解决方案1】:

我可能是错的,但我相信您的问题在于变量范围。

您在函数外部声明 $colour_no_hash,而函数无法访问它。您可以使用use 继承函数中的变量:

$colour_no_hash = str_replace('#', '', $colourvalue);
echo $colour_no_hash;

// Customize the TinyMCE Color Palette. Attempting to add a colour for 'Custom Login'. Other 2 colours work.
wptb_tinymce_options = function ($options) use ($colour_no_hash) {

    $custom_colours =  '"000000", "Black",
                        "00AC9F", "Custom Teal",
                        "' . $colour_no_hash . '", "Custom Login"';
    $options['textcolor_map'] = '['.$custom_colours.']';
    return $options;
};

EDIT把上面的改成匿名函数

来源:PHP Docs

【讨论】:

  • 谢谢你。 'use' 函数似乎会导致错误使用 'use' 的语法错误(这是有道理的)。
  • 啊,我的错,这仅适用于匿名函数 - 我将编辑代码,看看是否适合您?不知道你能不能这样通过?
猜你喜欢
  • 1970-01-01
  • 2014-02-01
  • 2021-10-23
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多