• Resolved nickk40

    (@nickk40)


    I’ve followed this tutorial to add custom styling to the wordpress editor. For example, I created a child theme and to functions.php I added code such as:

    function add_style_select_buttons( $buttons ) {
    	array_unshift( $buttons, 'styleselect' );
    	return $buttons;
    }
    add_filter( 'mce_buttons_2', 'add_style_select_buttons' );

    It all worked. But now I no longer want to use a child theme but instead add the code by using the Code Snippets plugin. However, when I add the above code (and the same problem is there for other code) as a Snippet and want to activate it, I get the error:

    The snippet has been deactivated due to an error on line 2:
    Cannot redeclare function add_style_select_buttons.

    What adjustment do I need to make to the code from the tutorial as to make this work with Code Snippets instead of a Child theme?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter nickk40

    (@nickk40)

    Ah, I get it: when I tried to activate the snippet, I also still had the child theme. So I had the same function name in two locations.

    Plugin Author Shea Bunge

    (@bungeshea)

    That’s exactly it! If you want to avoid this sort of error completely in the future, I recommend writing your functions as anonymous callbacks instead, like this:

    add_filter( 'mce_buttons_2', function ( $buttons ) {
    	array_unshift( $buttons, 'styleselect' );
    	return $buttons;
    } );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Moving from child theme to Code snippets: Code error’ is closed to new replies.