DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Easy Microdata in WordPress
  • Managed MQTT Broker Comparison — Console/Dashboard Features
  • Automated Deployment with Mule Management Console and Maven
  • How to Grab Eclipse Console Output Painlessly

Trending

  • Microservices With Apache Camel and Quarkus (Part 5)
  • Getting Started With Prometheus Workshop: Instrumenting Applications
  • Java Parallel GC Tuning
  • Choosing the Appropriate AWS Load Balancer: ALB vs. NLB

Add Color Picker to WordPress Admin Console

Paul Underwood user avatar by
Paul Underwood
·
Oct. 16, 12 · Interview
Like (0)
Save
Tweet
Share
5.96K Views

Join the DZone community and get the full member experience.

Join For Free

In a previous tutorial you would of learnt how you can create your own theme options page using the Settings API. You might of also seen the tutorial about default WordPress scripts which come with the WordPress core.

In this tutorial we are going to use both of these tutorials to create a colour picker on your theme options page using the default WordPress scripts.

When you create a field on your theme options page you use the function add_settings_field(), one of the parameters of this function is to name a callback function which displays the field on the page.

The below code will add a colour picker to your theme option page.

add_settings_field( 'example_colour_picker', 'Example Colour Picker', 'pu_display_colour_picker', 'pu_theme_options.php', 'pu_colour_section', array() );
add_action( 'admin_enqueue_scripts', 'enqueue_colour_picker' );
function pu_display_colour_picker($args){
     extract( $args );
     echo '
<div class="farb-popup-wrapper">';
     echo '<input type="text" id="'.$id.'" name="'.OPT_NAME.'['.$id.']" value="'.$value.'" class="'.$class.' popup-colorpicker" style="width:70px;"/>';
     echo '
<div id="'.$id.'picker" class="color-picker"></div>
';
     echo (!empty($desc))?' <span class="description">'.$desc.'</span>':'';
     echo '</div>
';
}
/**
 * Enqueue the colour picker
 */
function enqueue_colour_picker(){
                wp_enqueue_script(
			'artus-field-color-js',
			'Field_Color.js',
			array('jquery', 'farbtastic'),
			time(),
			true
		);	
		wp_enqueue_style( 'farbtastic' );
}

The above code will use the add_settings_field() function to create a field of displaying the colour picker. Within the function we add an action to attach a function onto the admin_enqueue_scripts to add javascript to the page. This javascript will load the inbuilt default script of farbtastic, the colour picker.

We also load a Javascript file called Field_Color.js which we will use to attach elements to the farbtastic jQuery function.

Now create a Javascript file and add the following code.

jQuery(document).ready(function(){
	var $color_inputs = jQuery('input.popup-colorpicker');
	$color_inputs.each(function(){
		var $input = jQuery(this);
		var $pickerId = "#" + jQuery(this).attr('id') + "picker";
		jQuery($pickerId).hide();
		jQuery($pickerId).farbtastic($input);
		jQuery($input).click(function(){jQuery($pickerId).slideToggle()});
	});
});

This searches for all elements with the class popup-colorpicker, with these items we can attach the farbtastic function to display the colour picker.

 

 

 

WordPress Console (video game CLI)

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Easy Microdata in WordPress
  • Managed MQTT Broker Comparison — Console/Dashboard Features
  • Automated Deployment with Mule Management Console and Maven
  • How to Grab Eclipse Console Output Painlessly

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: