在高级自定义字段中将自定义样式按钮添加到wysiwyg

时间:2015-09-07 14:55:04

标签: wordpress advanced-custom-fields

我想在高级自定义字段中为wysiwyg编辑器添加自定义按钮。我没有找到任何关于如何做的好方法。

我想要的是一个包含dynamic_cast中所选文本的按钮,以便我可以根据需要设置样式。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要做的是与TinyMCE编辑器按钮绑定。这将为您的WYSIWYG编辑器添加格式下拉列表。然后,您可以选择文本并从下拉列表中选择班级。使用文本视图检查它是否有效。

/*
 * Callback function to filter the MCE settings
 */

// Callback function to insert 'styleselect' into the $buttons array
function my_mce_buttons_2($buttons) {
    array_unshift($buttons, 'styleselect');
    return $buttons;
}

// Register our callback to the appropriate filter
add_filter('mce_buttons_2', 'my_mce_buttons_2');

function my_mce_before_init_insert_formats($init_array) {
// Define the style_formats array
    $style_formats = array(
        // Each array child is a format with it's own settings
        array(
            'title' => 'Some Class',
            'inline' => 'span',
            'classes' => 'someclass'
        ),
    );
    // Insert the array, JSON ENCODED, into 'style_formats'
    $init_array['style_formats'] = json_encode($style_formats);
    return $init_array;
}

// Attach callback to 'tiny_mce_before_init' 
add_filter('tiny_mce_before_init', 'my_mce_before_init_insert_formats');
相关问题