Pro Verified

Name

Add Custom Fields

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

This is how to add Custom Fields for Posts without using a Plugin.

Codevault

WebSquadron

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

16/05/2023

Important Note

This snippet has the following status:

Pro Verified

This snippet has been verified by a Code Snippet Pro team member.

Pro Comments:

Add Custom Fields

 
                    
1/*
2This is what I entered into ChatGPT if you ever want to expand on this.
3 
4"How can I add three custom fields to Wordpress Posts without a Plugin.
5 
6The custom fields are Reading Time, Level, and Source.
7 
8The Reading Time field will be numeric.
9The Level field will be a drop down field with the following entries; Basic, Intermediate, and Advanced.
10The Source field is a text field."
11 
12*/
13 
14 
15 
16 
17function add_custom_fields() {
18 // Add custom field: Reading Time
19 add_meta_box(
20 'reading_time', // Unique ID
21 'Reading Time', // Title
22 'display_reading_time_callback', // Callback function to display the field
23 'post', // Post type
24 'normal', // Context
25 'high' // Priority
26 );
27 
28 // Add custom field: Level
29 add_meta_box(
30 'level', // Unique ID
31 'Level', // Title
32 'display_level_callback', // Callback function to display the field
33 'post', // Post type
34 'normal', // Context
35 'high' // Priority
36 );
37 
38 // Add custom field: Source
39 add_meta_box(
40 'source', // Unique ID
41 'Source', // Title
42 'display_source_callback', // Callback function to display the field
43 'post', // Post type
44 'normal', // Context
45 'high' // Priority
46 );
47}
48 
49function display_reading_time_callback($post) {
50 // Retrieve the current value of the field
51 $reading_time = get_post_meta($post->ID, 'reading_time', true);
52 
53 // Output the field HTML
54 echo '<label for="reading_time">Reading Time (minutes):</label>';
55 echo '<input type="number" id="reading_time" name="reading_time" value="' . esc_attr($reading_time) . '" />';
56}
57 
58function display_level_callback($post) {
59 // Retrieve the current value of the field
60 $level = get_post_meta($post->ID, 'level', true);
61 
62 // Output the field HTML
63 echo '<label for="level">Level:</label>';
64 echo '<select id="level" name="level">';
65 echo '<option value="Basic" ' . selected($level, 'Basic', false) . '>Basic</option>';
66 echo '<option value="Intermediate" ' . selected($level, 'Intermediate', false) . '>Intermediate</option>';
67 echo '<option value="Advanced" ' . selected($level, 'Advanced', false) . '>Advanced</option>';
68 echo '</select>';
69}
70 
71function display_source_callback($post) {
72 // Retrieve the current value of the field
73 $source = get_post_meta($post->ID, 'source', true);
74 
75 // Output the field HTML
76 echo '<label for="source">Source:</label>';
77 echo '<input type="text" id="source" name="source" value="' . esc_attr($source) . '" />';
78}
79 
80function save_custom_field_data($post_id) {
81 // Save the custom field data: Reading Time
82 if (isset($_POST['reading_time'])) {
83 $reading_time = sanitize_text_field($_POST['reading_time']);
84 update_post_meta($post_id, 'reading_time', $reading_time);
85 }
86 
87 // Save the custom field data: Level
88 if (isset($_POST['level'])) {
89 $level = sanitize_text_field($_POST['level']);
90 update_post_meta($post_id, 'level', $level);
91 }
92 
93 // Save the custom field data: Source
94 if (isset($_POST['source'])) {
95 $source = sanitize_text_field($_POST['source']);
96 update_post_meta($post_id, 'source', $source);
97 }
98}
99 
100add_action('add_meta_boxes', 'add_custom_fields');
101add_action('save_post', 'save_custom_field_data');

0

Related Snippets

Please see some snippets below related to this snippet..

General

AI Verified

0

Remove Dashboard Welcome Panel

Added: 6 months ago

Last Updated: 6 months ago

Hide the Welcome Panel on the WordPress dashboard for all users.

General

AI Verified

0

Migrate BasePress -> BetterDocs: 4. Search Logs

Added: 6 months ago

Last Updated: 6 months ago

<p>Use these to migrate data from BasePress to BetterDocs, or clone them and customize to migrate to some other structure.</p> <p>Import all 4 snippets, then run them in order. This will handle docs,...

General

AI Verified

0

Display and Search Post ID

Added: 4 months ago

Last Updated: 4 months ago

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

29

Remove Unused Javascript

Added: 1 year ago

Last Updated: 3 weeks ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

9

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 5 months ago

Deactivate some WooCommerce Checkout Fields from showing

Performance

Pro Verified

5

Disable e-icons from Elementor

Added: 1 year ago

Last Updated: 1 year ago

Disable e-icons - and remove the notification from occurring during your Page Speed Insight Check.