Home / Admin / WPCode Functions
Duplicate Snippet

Embed Snippet on Your Site

WPCode Functions

For the WPCode plugin interface

Code Preview
php
<?php
/**
 * WPCode Admin UI Enhancements
 * - Skip library screen
 * - Set snippets per page
 * - Show active snippets first
 * - Adjust column widths and header wrapping
 * - Format Created column
 */
// 1. Skip library screen
add_filter( 'wpcode_add_snippet_show_library', '__return_false' );
// 2. Set snippets per page
add_filter( 'wpcode_snippets_per_page', fn() => 50 );
// 3. Order Snippets
add_filter( 'wpcode_code_snippets_table_prepare_items_args', function( $args ) {
    if ( ! isset( $_GET['orderby'] ) ) {
        $args['orderby'] = 'title';
    }
    if ( ! isset( $_GET['order'] ) ) {
        $args['order'] = 'ASC';
    }
    
    return $args;
});
// 4. Enqueue JS and CSS only on WPCode admin page
add_action( 'admin_enqueue_scripts', function( $hook ) {
    if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'wpcode' ) {
        return;
    }
    // --- JS: Move active snippets first + format Created column ---
    $js = <<<JS
document.addEventListener('DOMContentLoaded', function() {
    const tableBody = document.querySelector('.wp-list-table tbody');
    if (!tableBody) return;
    const rows = Array.from(tableBody.querySelectorAll('tr'));
    const activeRows = [];
    const inactiveRows = [];
    rows.forEach(row => {
        const checkbox = row.querySelector('input.wpcode-status-toggle');
        if (checkbox && checkbox.checked) {
            activeRows.push(row);
        } else {
            inactiveRows.push(row);
        }
    });
    // Rebuild table: active first
    tableBody.innerHTML = '';
    const sortedRows = activeRows.concat(inactiveRows);
    sortedRows.forEach(row => tableBody.appendChild(row));
	// Format Created column
    var createdCells = tableBody.querySelectorAll("td.column-created");
    createdCells.forEach(function(cell) {
        var text = cell.textContent.trim();
        var match = text.match(/([A-Za-z]+) (\d+), (\d{4}) at (\d{1,2}:\d{2} [AP]M)/);
        if (match) {
            var month = match[1].slice(0,3);
            var day = match[2].padStart(2,"0");
            var year = match[3];
            var time = match[4];
            cell.textContent = month + " " + day + ", " + year + " at " + time;
        }
    });
	
});
JS;
    wp_add_inline_script( 'jquery-core', $js );
    // --- CSS: Adjust column widths and wrap headers ---
    $css = <<<CSS
	
:root {font-size: clamp(12px, 12px + 1vw, 14px);}
body {font-size: 1rem; gap: 1rem;}
/* Note column wider */
.wp-list-table .column-note, #name {
	min-width: 30%;
	width: auto;
}
/* Column narrow */
.wp-list-table .column-code_type,
.wp-list-table #status {
    width: 80px;
}
.wp-list-table #location,
#tags {
    width: 15%;
}
#tags {min-width: 150px;}
/* Created column styling */
.wp-list-table .column-created {
    width: 110px;
    white-space: normal !important;
    word-wrap: break-word;
    font-family: monospace;
    font-size: 0.8rem;
    font-weight: 500;
}
.wp-list-table {word-wrap: break-word;}
.wp-list-table td {
	overflow: hidden !important;
	text-overflow: ellipses !important;
}
@media (min-width: 950px) {
	/* Wrap header text in all columns */
	.wp-list-table th {
		white-space: normal !important;
		word-wrap: break-word;
		text-overflow: ellipses !important;
		align-items: baseline;
	}
}
@media (max-width:1200px)  {
	body {font-size: 90%;}
	.wp-list-table th {
		overflow: hidden !important;
		text-overflow: clip !important;
	}
	.wp-list-table tr {
		height: fit-content;
		max-height: 200px;
		overflow: hidden !important;
	}
	.wp-list-table .column-note {
		display: none;
	}
	#tags, #location {width: 20%;}
}
@media (max-width:850px)  {
	body {font-size: 85%;}
	.wp-list-table .column-created {
		display: none;
	}
}
CSS;
    wp_add_inline_style( 'wp-admin', $css );
});

Comments

Add a Comment