Skip to content

Instantly share code, notes, and snippets.

View kingjmaningo's full-sized avatar
🌱
A work in progress

kingjmaningo

🌱
A work in progress
View GitHub Profile
@kingjmaningo
kingjmaningo / custom-file-upload-wordpress.php
Created February 27, 2023 01:36
Custom file upload with nonce - Wordpress
<!-- Image preview Script -->
<script>
jQuery(document).ready(function($){
function imagePreview(uploader) {
if (uploader.files && uploader.files[0]) {
$("#display-upload-image").attr("src", window.URL.createObjectURL(uploader.files[0]));
}
}
$("#sample_image").change(function() {
imagePreview(this);
@kingjmaningo
kingjmaningo / export-custom-data-to-csv.php
Created March 5, 2022 09:56
Wordpress export custom data to CSV
<?php
if (isset($_GET['download-users'])) {
global $wpdb;
// Fetch data from a sepecific table(wp_users) from database
$query = $wpdb->get_results("SELECT * FROM `wp_users`");
$filename = "wp_users_" . date('Y-m-d') . ".csv";
$path = wp_upload_dir(); // or where ever you want the file to go
$outstream = fopen( 'php://output', 'w' );
$user_fields_csv = array('Row','ID','user_login','user_pass','user_email','user_registered');
ob_end_clean();
@kingjmaningo
kingjmaningo / get-data-using-wpdb-global-object.php
Created March 5, 2022 06:01
Simple ways to retrieve data using wpdb
<?php
global $wpdb;
// Fetch data from a sepecific table(wp_users) from database
$get_users = $wpdb->get_results("SELECT * FROM `wp_users`");
//Sample output
Array
(
[0] => stdClass Object
(
[ID] => 1
@kingjmaningo
kingjmaningo / recognise-meta-key-order-query.php
Created July 7, 2021 01:58
Recognise order meta for you to b e able to use it in your args to display specific orders - Woocommerce
<?php
// add this filter to recognise meta key when you do your order query
add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', 'my_function', 10, 2 );
function my_function( $query, $query_vars ) {
if ( ! empty( $query_vars['_my_custom_order_meta'] ) ) {
$query['meta_query'][] = array(
'key' => '_my_custom_order_meta',
'value' => esc_attr( $query_vars['_my_custom_order_meta'] ),
);
}
@kingjmaningo
kingjmaningo / plugin-activated-deactivated-hooks-sample.php
Created June 23, 2021 05:40
Plugin activated/deactivated hooks sample
<?php
// Let's assume this is your main plugin file
register_activation_hook( __FILE__, array( 'YOURTEXTDOMAIN', 'function_to_call_upon_activation' ) );
//register_activation_hook( __FILE__, array ( $my_class, 'function_to_call') ); // If inside a Class
function function_to_call_upon_activation() {
// Check PHP Version and deactivate & die if it doesn't meet minimum requirements.
if ( version_compare( PHP_VERSION, '5.4', '<=' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
add_action( 'admin_notices', 'my_plugin_notice' );
@kingjmaningo
kingjmaningo / copy-text-button.html
Last active June 12, 2021 03:11
Copy text from an input field
<div class="input-group mb-3">
<input type="text" class="form-control text-value" value="sample text here">
<div class="input-group-append">
<button class="btn btn-outline-secondary copy-url" type="button"><i class="fas fa-copy"></i></button>
</div>
</div>
<script>
jQuery(document).ready(function($){
$('.copy-url').click(function() {
@kingjmaningo
kingjmaningo / create-page-wp-admin-dashboard.php
Last active December 3, 2022 07:25
Create sample page on the admin dashboard
<?php
add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_menu() {
add_menu_page(
__( 'My page name', 'my-textdomain' ),
__( 'My menu name', 'my-textdomain' ),
'manage_options',
'sample-page',
'my_admin_page_contents',
'dashicons-schedule',
@kingjmaningo
kingjmaningo / get-30-min-interval-between-two-time.php
Created May 22, 2021 11:52
Get 30 min interval between two time
<?php
$time_start = 1619164800; //8:00:00 AM converted to integer
$time_end = 1619197200; //5:00:00 PM converted to integer
$interval = 1800; // 30min interval in seconds
$x = 1;
for( $item_time = $time_start; $item_time < $time_end; $item_time += $interval ) {
echo date('g:i a', $item_time); ?>
$x++;
}
?>
@kingjmaningo
kingjmaningo / get-update-data-from-separate-db-wordpress.php
Last active May 22, 2021 11:28
Get/Update data from a separate database - Wordpress
<?php
// Create global variable for another db
add_action('init', 'get_certain_db');
function get_certain_db() {
global $another_db;
$another_db = new wpdb(USERNAME, PASSWORD, DATABASE_NAME, HOSTNAME);
}
// Example #1 Get taxonomies from another db
function listing_cat_list() {
@kingjmaningo
kingjmaningo / create-database-table-after-plugin-activated.php
Created March 3, 2021 06:47
Create database table upon plugin activation
<?php
register_activation_hook( __FILE__, array( 'SAMPLE_CLASS', 'plugin_activated' ) );
class SAMPLE_CLASS {
public function plugin_activated() {
$add_custom_table = $wpdb->prefix . 'table_name';
if($wpdb->get_var("SHOW TABLES LIKE '$add_custom_table'") != $add_custom_table) {
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $add_custom_table (
`id` int(100) NOT NULL auto_increment,
`field_label` int(100) NOT NULL,