I think you are looking for “meta boxes”.
You will work with add_meta_box()
to create one or multiple new meta-boxes for your post-types. (Codex)
You have to set up some callback function which holds the HTML of the fields you want to show/display.
And you will also need a function to save these fields using add_post_meta()
and update_post_meta()
. (Codex to ADD and UPDATE)
If you want to remove some existing meta-boxes you can use remove_meta_box()
. (Codex)
Some Details:
With the following code you can create a new Meta-box on the new/edit screen of your “quicklinks” post-type. (cause I entered “quicklink” as the post-type)
function create_custom_metabox() {
add_meta_box(
'my_meta', // HTML 'id' attribute of the metabox
__( 'My Setting', 'textdomain' ), // Title of metabox
'my_fields_callback', // Function that prints out the HTML for metabox
'quicklink', // The post-type of writing screen on which to show the edit screen section (example 'post' or 'page')
'normal', // The part of the page where the metabox should be shown ('normal', 'advanced', or 'side')
'high' // The priority within the context where the boxes should show ('high', 'core', 'default' or 'low')
);
}
add_action( 'add_meta_boxes', 'create_custom_metabox' );
After this code you will already see a new Meta-Box, but it will be empty, there are no fields yet to show.
So next we create a new callback function with our new fields: (see function name and callback argument in add_meta_box()
above)
function my_fields_callback( $post ) {
// creating a custom nonce
wp_nonce_field( basename( __FILE__ ), 'my_custom_nonce' );
// see and get if some meta is already saved
$stored_meta = get_post_meta( $post->ID );
?>
<!-- Textfield START -->
<p>
<span class="my-row-title">
<label for="meta-text" class="my-row-title"><?php _e( 'Text', 'textdomain' )?></label>
</span>
<div class="my-row-content">
<input type="text" name="meta-text" id="meta-text" placeholder="Text..." value="<?php if ( isset ( $stored_meta['meta-text'] ) ) echo $stored_meta['meta-text'][0]; ?>" />
</div>
</p>
<!-- Textfield END -->
<?php
}
After this code, you will already see a new input field and a label. But if you enter something it will still not get saved! So we need to add a save function to this:
function save_my_meta( $post_id ) {
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'my_custom_nonce' ] ) && wp_verify_nonce( $_POST[ 'my_custom_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// save our new created field
if( isset( $_POST[ 'meta-text' ] ) ) {
// if there is some content in the field, we update it
update_post_meta( $post_id, 'meta-text', sanitize_text_field( $_POST[ 'meta-text' ] ) );
}
}
add_action( 'save_post', 'save_my_meta' );
After that we can save the values of your new field.
If you want to show these values you can use the get_post_meta()
function. (Codex) like this:
$my_meta_value = get_post_meta( get_the_ID(), 'meta-text', true );
if( !empty( $my_meta_value ) ) {
echo $my_meta_value;
}
So, you see there are several functions needed. There are a lots of tutorials out there, here is an old one which still functions.
I couldnt find one in german in the moment, sorry.
Be also sure to have an eye an sanitize the different values of the fields on saving and retrieving.
I hope this helps!
Update:
When you create a meta-box with the add_meta_box()
function, you can set where the meta-box should appear with the context parameter…
add_meta_box(
'my_meta',
__( 'My Setting', 'textdomain' ),
'my_fields_callback',
'quicklink',
'normal', // The part of the page where the metabox should be shown ('normal', 'advanced', or 'side')
'high'
);
If you choose side, the box will be created on the right sidebar.
Regarding ACF and fields without the wrapper container:
It seems when you create an field with ACF without the meta-box-wrapper/container, some elements are just hidden, and some extra styles are added to hide the container!
So I think the best way would be to load some custom css and maybe jQuery, only on the edit/new page of the post-type which has your custom fields.
function add_scripts_and_styles()
{
global $typenow;
if( 'quicklink' != $typenow )
return;
echo "
<script type="text/javascript">
jQuery(document).ready( function($) {
$('#my_meta').removeClass('postbox');
$('#my_meta h3').hide();
});
</script>
<style type="text/css">
/* CUSTOM RULES */
</style>
";
}
add_action( 'admin_head-post-new.php', 'add_scripts_and_styles' );
add_action( 'admin_head-post.php', 'add_scripts_and_styles' );
Source: This Stackexchange answer
Nonces:
I try to explain it, but maybe the codex will be clearer to you:
A nonce in the WP envoirnment is a security feature.
Its a random and unique combination of numbers and letters which is only valid a limited time and also per user.
In this case we are using it in the form to check that the input-data are coming from the genuine user.
The system will check the nonce upon saving to see that nobody/something else is trying to save the data.
Or as the codex says it:
The nonce field is used to validate that the contents of the form request came from the current site and not somewhere else.