protected function add_button_before(array &$params, $row, $button,
            $before = '', $alwaysadd = true) {

        if ($button !== '|' && $this->find_button($params, $button)) {
            return true;
        }
        $row = $this->fix_row($params, $row);

        $field = 'theme_advanced_buttons' . $row;
        $old = $params[$field];

        // Empty = add at start.
        if ($before === '') {
            $params[$field] = $button . ',' . $old;
            return true;
        }

        // Try to add before given plugin.
        $params[$field] = preg_replace('~(,|^)(' . preg_quote($before) . ')(,|$)~',
                '$1' . $button . ',$2$3', $old);
        if ($params[$field] !== $old) {
            return true;
        }

        // If always adding, recurse to add it empty.
        if ($alwaysadd) {
            return $this->add_button_before($params, $row, $button);
        }

        // Otherwise return false (failed to add).
        return false;
    }