Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Set default colors for Elementor page builder once my custom plugin is activated WordPress

  • SOLVED

I'm trying to set default colors for Elementor page builder once my custom plugin is activated. When Elementor is installed it saves default color values into the database with the option name elementor_scheme_color.

What I want to achieve is when I activate my custom plugin for Elementor it will override these values only once in the database. Later the user might change these colors, so this function should not be executed again. The Elementor developers on Github pointed that I should use pre_update_option_elementor_scheme_color hook for this.
But when I try to apply this filter it overrides this values every time when you hit save button in Elementor color settings.

Here is my code reference for my custom plugin (stripped down for the sake of simplicity):


final class Deo_Elementor_Extension
{

public function __construct() {
add_action( 'plugins_loaded', [ $this, 'init' ] );
}

public function init() {
add_filter( 'pre_update_option_elementor_scheme_color', [ $this, 'filter_pre_update_option' ], 10, 2 );
}

/**
* Filter for Elementor colors
**/
public function filter_pre_update_option( $new_value, $old_value ) {
$new_value = array(
1 => '#ffb31a',
2 => '#ffb31a',
3 => '#ffb31a',
4 => '#ffb31a'
);
return $new_value;
}
}

Answers (3)

2018-07-22

Francisco Javier Carazo Gil answers:

As C├ęsar Contreras said, you have to use this hook, include this one after your class:

register_activation_hook( __FILE__, array( 'Deo_Elementor_Extension', 'install' ) );

And inside your class:

static function install(){
// update the database with your option to override colors
}


User179872 comments:

What if the user activated plugin first time, then changed colors and then deactivated plugin (for example when updating something) after second activation this method will override all colors again.

2018-07-22

Reigel Gallarde answers:

unless there's a way to check if the color schemes has been updated by the user, what you are trying to achieve is impossible.

If somehow there's a way to check that the user did change the color schemes, you could use some "if" statement in your in changing $new_value.


User179872 comments:

Actually I've already achieved it :) Not perfect solution, but it works. Just added new option in DB that updates after the colors are updated.


// Update Elementor Default Colors
add_option( 'deo_elementor_colors', 0 );

function deo_update_option() {
if ( get_option( 'deo_elementor_colors' ) != 1 ) {
update_option( 'elementor_scheme_color', array(
1 => '#333b69',
2 => '#4c86e7',
3 => '#6a798c',
4 => '#fa6262'
) );
update_option( 'deo_elementor_colors', 1 );
}
}


Reigel Gallarde comments:

Yes, that's the closest solution you could get.

2018-07-21

Cesar Contreras answers:

I hope this documentation can help you
https://codex.wordpress.org/Function_Reference/register_activation_hook