Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the community forum?
All Topics
splaquet

With or Without the # (before #hex Color field type)

I wanted to ask about the Field Type “color”. I’m using Modern Event Calendar, which uses a color field. Their plugin does not use the # in the wp_postmeta field (so, 000000; and not #000000).

AdminColumns needs the # in front of the hex color, to display a color on the field. here’s a screenshot of what i’m talking about:

Color - field type

would it be possible to modify your code so that it detects the color with or without the # in front of the hex color? (see how the hex color is there, but only displays color if the # is present?) without the #, the color doesn’t show on the admin row… but with the #, it ends up producing two ## (eg: ##000000) in the end result of the plugin.

1 month ago
splaquet

1 month ago
Stefan

Our color type always stores the value as #000000.
I don’t find it logical to store the color without the # in the database but that is a choise MEC made apparently. Unfortunately, this means you cannot use our color type in the custom field column to manage the value of that field.

If you’re a developer and know how to work with hooks, you can use the following hook to remove the # from the value just before it is saved by our plugin.

acp/editing/save_value

Have a look at the first example where there is a check for the custom field column and a specific meta field key.

1 month ago
splaquet

sadly, i’m not familiar enough with php to work with hooks yet 🙁

although i haven’t worked with the database side of things (to check and see how it translates), many themes and plugins will take a HEX color code (with or without the #) and translate it. my assumption was that this was something native to storing the data. i didn’t realize that the standard is to store the #HEX in the DB, and have the front end translate it (w or w/o #) to the final #HEX format.

if it’s as easy as you imply, would you mind helping me out?

here’s what it looks like in the table:

    meta_key meta_value

mec_color fdd700
MEC_color

1 month ago
Stefan

There are other color encodings than just hex (RGB, RGBA, HSL ), so only if you’re sure that colors are always implemented as a specific format, you can do such a thing. We choose for one implementation for our inline editing feature which is hex codes. We can’t do assumptions on what is stored in the database of our customers, so if a theme or plugin requires a specific format, we have our hooks to alter it as you want. I wrote the two hooks for you that you can implement in your codebase. The first hook changes the value just before it is stored in the database (removes #) and the other one adds the # for display purpose.

/**
 * Changes the #HEX value to value without #
 */
add_filter( 'ac/editing/save_value', function ( $value, AC\Column $column, $id ) {
	if ( $value && $column instanceof ACP\Column\CustomField && 'mec_color' === $column->get_meta_key() ) {
		$value = str_replace( '#', '', $value );
	}

	return $value;
}, 10, 3 );

/**
 * Add the # to the string again so it can be parsed
 */
add_filter( 'ac/column/value', function ( $value, $id, AC\Column $column ) {
	if ( $value && $column instanceof ACP\Column\CustomField && 'mec_color' === $column->get_meta_key() ) {
		$value = ac_helper()->string->get_color_block( '#' . $column->get_raw_value( $id ) );
	}

	return $value;
}, 10, 3 );
4 weeks ago

You must be logged in to reply to this topic.