Edit the admin post type title column

To edit the Admin header of your new post type page you need to add the following code:

add_action(‘admin_head-edit.php‘,’edit_change_title_function’);

in your function you have to check if the post type is the one you need, and then apply a filter for the title

function edit_change_title_function(){
global$post;
if($post->post_type == 'posttype_name') {
add_filter('the_title','posttype_title_function',100,2);
}
}

So, you can override the title getting the value from db:

function posttype_title_function($title, $post_id) {
$new_title = get_post_meta($post_id,'slb_first_name',true) .' '.get_post_meta($post_id,'slb_last_name',true);
return $new_title;
}
WordPress Custom Fields (meta key)

A custom fields are, basically, the properties for a post type.

The creation phase of a custom field is divided in two parts:

  1. Creation of the structure
  2. Define the behavior

Creation of the structure

Metabox is the graphic container of our custom fields. It will appear as a fieldsets in the admin page, containing our custom fields.

add_action('add_meta_boxes_{post_type_name}','add_metabox_function');

function add_metabox_function($post)
{
  add_meta_box('id','title','metabox_field_function','post_type_name','normal','default');
}

This function will contain all necessary html, CSS and logical code to manage and show our custom fields.

function metabox_field_function()
{
  //html code to show fields and php code to manage fields
}

One interesting thing is that you don’t have to define an html form and a submit button, because your code will be automatically wrapped in.
So: no form and no submit butto, but the normal html input tag.

After defining the html field in the previous step it’s time to manage the submit of your metadata.

The hook to define it is:

add_action('save_post','save_posttypename_meta', 10, 2);
and the function to manage it:
function save_posttypename_meta($post_id, $post) 
{
  //php code to get fields from POST and save them into db
}

Define the behavior

With this variable you can access directly to your wordpress database to make query, access tables and so on

global $wpdb;
Ex. to get the list of content of a custo post type slb_list
$list_query=$wpdb->get_results("select ID, post_title from {$wpdb->posts} where post_type='slb_list' and post_status in ('draft', 'publish')");

Ex. get the post id

$post_id = $post->ID;

 

to read custom field value from the DB, you can use this code the wordpress function “get_post_meta”:

$first_name = (!empty(get_post_meta($post_id, 'slb_first_name', true))) ? get_post_meta($post_id, 'slb_first_name', true) : '';

Basically it takes the custom field value of that post_id and that custom field name

To take parameter value from a http request (after a submit) in PHP you have to do like this:

$first_name = ( isset($_POST['slb_first_name']) ) ? sanitize_text_field($_POST['slb_first_name']) : '';

 

To add a customfield, and its value, do this:

add_post_meta( $post_id, 'customfield_name', value, true/false );

The last parameter means that you want to exlude (true) or you don’t mind (false) duplicated values of that custom field for that post_id 

to update a custom field:

update_post_meta($post_id, ‘customfield_name’, value);
To delete the existing list meta for that post id:
delete_post_meta( $post_id, 'customfield_name');

Yes, all values of that custom fields will be deleted, even if you associated more than one value.

WordPress Custom Post Type

Post types are a way to categorize different types of content in WordPress.

WordPress has already the following native post type:

  • Post
  • Pages
  • Attachments
  • Revisions
  • Navigation menu
  • custom CSS
  • Changeset

Most probably you just knew about the first three in the list.

And you can extend these wordpress post type with your own.

To do this you here it is a little function recap.

 

There is another way to create a custom post type. It’s using the plugin Custom Post Type UI (or CPTUI). In this case you can create your post types directly in the wordpress admin with a simple UI.

Of course it depends on what you need from Custom Post Types.

If you are creating a plugin it’s better to create them inside it.

If you need them only for you personal use then it’s easer to use CPTUI.

 

That’s it

WordPress Shortcodes

Here it is a little schema to recap the creation of a wp shortcode

But how we can submit data through shortcodes?

Here it is.

you need to register two ajax function:

add_action('wp_ajax_nopriv_{save_your_posttype_function}', 'save_your_posttype_function'); // regular website visitor
add_action('wp_ajax_{save_your_posttype_function}', 'save_your_posttype_function'); // admin user

The submit action inside your shortcode must be something like this:

action="/wp-admin/admin-ajax.php?action=save_your_posttype_function" method="post"

and finally you can implement that function:

function slb_save_subscription() {
...
//after saving your data you have to return a json
if( $data_saved ):
  $result['status']=1;
  $result['message']='Data saved';
endif;

// encode result as json string
$json_result = json_encode( $result );

// return result
die( $json_result );

// stop all other processing
exit;

}