jrobinsonc
9/23/2016 - 12:11 AM

WordPress custom post type helper

WordPress custom post type helper

<?php

/**
 * @see https://gist.github.com/jrobinsonc/d5aba3a6066ff2e54e24ced341eaa3aa
 */
class WP_CPT {

    public function __construct()
    {
        add_action( 'init', [$this, 'action_init']);
        add_action( "add_meta_boxes_{$this->slug}", [$this, 'action_add_meta_boxes']);
        add_action( 'save_post', [$this, 'action_save_post'], 10, 2);
    }

    public function get_field( $metabox_name, $field_name, $content = '' )
    {
        $field = new stdClass();
        $field->label = $this->custom_fields[ $metabox_name ][ 'fields' ][ $field_name ];
        $field->name = "{$this->slug}_{$metabox_name}_{$field_name}";
        $field->slug = "{$metabox_name}/{$field_name}";
        $field->content = $content;

        return $field;
    }

    public function action_add_meta_boxes($post)
    {
        foreach ($this->custom_fields as $metabox_name => $metabox_info)
        {
            add_meta_box(
                "{$this->slug}-{$metabox_name}",
                $metabox_info['title'],
                function() use ($metabox_name, $metabox_info, $post) {

                    foreach ($metabox_info['fields'] as $field_name => $field_label) {

                        $field = $this->get_field( $metabox_name, $field_name );
                        $field->content = get_post_metax( $field->name, $post->ID );

                        echo '<p>';
                        printf('<label for="%s">%s</label><br>', $field->name, $field->label);

                        $input_html = $this->custom_field_html( $field );

                        if ( null === $input_html )
                            printf('<input type="text" name="%1$s" id="%1$s" value="%2$s">', $field->name, $field->content );
                        else
                            echo $input_html;

                        echo '<p>';
                    }

                },
                $this->slug,
                'normal',
                'default'
            );
        }

    }

    public function action_save_post($post_id, $post)
    {
        if ( $post->post_type !== $this->slug )
            return;


        foreach ($this->custom_fields as $metabox_name => $metabox_info)
        {
            foreach ($metabox_info['fields'] as $field_name => $field_label) {
                $field = $this->get_field( $metabox_name, $field_name );

                update_post_meta( $post_id, $field->name, $_POST[$field->name] );
            }
        }

    }

}

//class Custom_post_type_field {
//    public $name;
//    public $content;
//    public $slug;
//
//    public function __construct($name, )
//    {
//
//    }
//}

WordPress custom post type helper

Usage

<?php

require_once 'WP_CPT.php';

class Portfolio_post_type extends WP_CPT {

    public $slug = 'portfolio';

    public $custom_fields = [
        'details' => [
            'title' => 'Project details',
            'fields' => [
                'name' => 'Project name',
                'description' => 'Project description'
            ]
        ]
    ];

    function custom_field_html($field)
    {
        // Adding custom inputs for some fields.
        switch ($field->slug)
        {
            case 'details/description':
                $input = sprintf('<textarea name="%s">%s</textarea>', $field->name, $field->content);
                break;

        }

        return $input;
    }
    
    public function action_init()
    {
        register_post_type( $this->slug, [
            'labels' => array(
                'name' => __( 'Portfolio' ),
                'singular_name' => __( 'Portfolio' )
            ),
            'public' => true,
            'supports' => ['title'],
            'rewrite' => [
                'with_front' => false
            ]
        ] );

    }
}

new Portfolio_post_type;