Beee

acf_color_swatches

Mar 31st, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.70 KB | None | 0 0
  1. function pg_override_color_picker_swatches() {
  2.     if ( is_page( [ CREATE_BANNER_PAGE, EDIT_BANNER_PAGE ] ) ) {
  3.         $colors = [
  4.             '#000000',
  5.             '#ffffff',
  6.             '#DD3333', // red
  7.             '#DD9933', // orange
  8.             // '#EEEE22', // yellow
  9.             // '#81D742', // green
  10.             // '#1E73BE', // blue
  11.             // '#8224E3', // purple
  12.         ];
  13.         $primary_color   = false;
  14.         $secondary_color = false;
  15.  
  16.         if ( isset( $_GET[ 'id' ] ) ) {
  17.             $post_id         = $_GET[ 'id' ];
  18.             $fields          = get_field_objects( $post_id );
  19.             $primary_color   = ! empty( $fields[ 'pg_primary_color' ][ 'value' ] ) ? $fields[ 'pg_primary_color' ][ 'value' ] : $primary_color;
  20.             $secondary_color = ! empty( $fields[ 'pg_secondary_color' ][ 'value' ] ) ? $fields[ 'pg_secondary_color' ][ 'value' ] : $secondary_color;
  21.         }
  22.         if ( false != $secondary_color ) {
  23.             array_unshift( $colors, $secondary_color );
  24.             array_pop( $colors );
  25.         }
  26.         if ( false != $primary_color ) {
  27.             array_unshift( $colors, $primary_color );
  28.             array_pop( $colors );
  29.         }
  30.         $colors = "'" . implode("','", $colors ) . "'";
  31.  
  32.         // @TODO: maybe don't apply to primary/secondary color
  33.     ?>
  34.     <script type="text/javascript">
  35.         (function($) {
  36.             acf.add_filter('color_picker_args', function( args, $field ) {
  37.                 args.palettes = [ <?php echo $colors; ?> ];
  38.  
  39.                 return args;
  40.             });
  41.         })(jQuery);
  42.     </script>
  43.     <?php
  44.     }
  45. }
  46. add_action( 'acf/input/admin_footer', 'pg_override_color_picker_swatches' );
Add Comment
Please, Sign In to add comment