Home / Admin / DIR_CPT_WPCODE – WPCode Snippets Sync CPT Registration
Duplicate Snippet

Embed Snippet on Your Site

DIR_CPT_WPCODE – WPCode Snippets Sync CPT Registration

ismail daugherty PRO
<10
Code Preview
php
<?php
/**
 * WPCode Snippet: WPCode Snippets Sync CPT Registration
 * Description: Registers shadow CPT for syncing WPCode snippets to REST API
 * Location: Run Everywhere
 * Priority: 5
 * 
 * INSTALL ORDER: #1 - Install this first
 * 
 * IMPORTANT: This creates a SHADOW CPT (wpcode_snippets_sync) separate from WPCode's actual wpcode CPT
 */
defined( 'ABSPATH' ) || exit;
/**
 * Register wpcode_snippets_sync custom post type
 * This is a READ-ONLY mirror of WPCode snippets for REST API access
 */
add_action( 'init', function() {
    
    register_post_type( 'wpcode_snippets_sync', array(
        'labels' => array(
            'name'               => 'Code Snippets (Sync)',
            'singular_name'      => 'Code Snippet (Sync)',
            'menu_name'          => 'Code Snippets',
            'add_new'            => 'Add New',
            'add_new_item'       => 'Add New Code Snippet',
            'edit_item'          => 'Edit Code Snippet',
            'new_item'           => 'New Code Snippet',
            'view_item'          => 'View Code Snippet',
            'search_items'       => 'Search Code Snippets',
            'not_found'          => 'No code snippets found',
            'not_found_in_trash' => 'No code snippets found in Trash',
            'all_items'          => 'All Code Snippets',
        ),
        'public'              => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => false,
        'show_in_admin_bar'   => true,
        'menu_position'       => 25,
        'menu_icon'           => 'dashicons-code-standards',
        'capability_type'     => 'post',
        'hierarchical'        => false,
        'supports'            => array( 'title' ),
        'has_archive'         => false,
        'rewrite'             => false,
        'query_var'           => false,
        'can_export'          => true,
        'delete_with_user'    => false,
        
        // REST API - CRITICAL for WhaleSync
        'show_in_rest'        => true,
        'rest_base'           => 'wpcode-snippets-sync',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
    ) );
    
} );
/**
 * Add admin notice about shadow CPT purpose
 */
add_action( 'admin_notices', function() {
    $screen = get_current_screen();
    
    if ( $screen && 'wpcode_snippets_sync' === $screen->post_type ) {
        ?>
        <div class="notice notice-info">
            <p>
                <strong>ℹ️ Read-Only Mirror:</strong> 
                This is a synced copy of your WPCode snippets for REST API access. 
                To edit snippets, use the <a href="<?php echo admin_url( 'admin.php?page=wpcode' ); ?>">WPCode plugin</a>.
            </p>
        </div>
        <?php
    }
} );
/**
 * Make title field read-only in admin
 */
add_action( 'admin_head-post.php', function() {
    global $post_type;
    
    if ( 'wpcode_snippets_sync' === $post_type ) {
        ?>
        <style>
            #titlewrap input { 
                background: #f0f0f1; 
                cursor: not-allowed; 
            }
        </style>
        <script>
            jQuery(document).ready(function($) {
                $('#title').prop('readonly', true);
            });
        </script>
        <?php
    }
} );
/**
 * Flush rewrite rules on activation
 */
register_activation_hook( __FILE__, function() {
    // Re-register the post type
    add_action( 'init', function() {
        register_post_type( 'wpcode_snippets_sync', array(
            'show_in_rest' => true,
            'rest_base'    => 'wpcode-snippets-sync',
        ) );
    } );
    
    // Flush
    flush_rewrite_rules();
} );

Comments

Add a Comment