100 WordPress Code Snippets from Across the Net
https://wpmudev.com/blog/shun-the-plugin-100-wordpress-code-snippets-from-across-the-net/
<?php | |
/************************************************************************************** | |
* this function for make wp became under maintenece | |
*/ | |
function wp_maintenance_mode() { | |
if (!current_user_can('edit_themes') || !is_user_logged_in()) { | |
wp_die('<h1>Under Maintenance</h1><br />Website under planned maintenance. Please check back later.'); | |
} | |
} | |
add_action('get_header', 'wp_maintenance_mode'); | |
/************************************************************************************** | |
* this function for add new flag for order status | |
*/ | |
// Register new status | |
function register_awaiting_shipment_order_status() { | |
register_post_status( 'wc-awaiting-shipment', array( | |
'label' => 'Awaiting shipment', | |
'public' => true, | |
'exclude_from_search' => false, | |
'show_in_admin_all_list' => true, | |
'show_in_admin_status_list' => true, | |
'label_count' => _n_noop( 'Awaiting shipment (%s)', 'Awaiting shipment (%s)' ) | |
) ); | |
} | |
add_action( 'init', 'register_awaiting_shipment_order_status' ); | |
// Add to list of WC Order statuses | |
function add_awaiting_shipment_to_order_statuses( $order_statuses ) { | |
$new_order_statuses = array(); | |
// add new order status after processing | |
foreach ( $order_statuses as $key => $status ) { | |
$new_order_statuses[ $key ] = $status; | |
if ( 'wc-processing' === $key ) { | |
$new_order_statuses['wc-awaiting-shipment'] = 'Awaiting shipment'; | |
} | |
} | |
return $new_order_statuses; | |
} | |
add_filter( 'wc_order_statuses', 'add_awaiting_shipment_to_order_statuses' ); | |
/************************************************************************************** | |
* this function for getting parent css theme | |
*/ | |
function total_child_enqueue_parent_theme_style() { | |
// Dynamically get version number of the parent stylesheet (lets browsers re-cache your stylesheet when you update your theme) | |
$theme = wp_get_theme( 'Total' ); | |
$version = $theme->get( 'Version' ); | |
// Load the stylesheet | |
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css', array(), $version ); | |
} | |
add_action( 'wp_enqueue_scripts', 'total_child_enqueue_parent_theme_style' ); | |
/**************************************************************************************** | |
* Remove dashboard widgets | |
*/ | |
function remove_dashboard_meta() { | |
// if ( ! current_user_can( 'manage_options' ) ) { | |
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' ); | |
remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' ); | |
remove_meta_box( 'dashboard_primary', 'dashboard', 'normal' ); | |
remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' ); | |
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' ); | |
remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' ); | |
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' ); | |
remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' ); | |
remove_meta_box( 'dashboard_activity', 'dashboard', 'normal'); | |
// } | |
} | |
add_action( 'admin_init', 'remove_dashboard_meta' ); | |
/**************************************************************************************** | |
* Add a widget to the dashboard. | |
* | |
* This function is hooked into the 'wp_dashboard_setup' action below. | |
*/ | |
function wpexplorer_add_dashboard_widgets() { | |
// wp_add_dashboard_widget( | |
// 'wpexplorer_dashboard_widget', // Widget slug. | |
// 'My Custom Dashboard Widget', // Title. | |
// 'wpexplorer_dashboard_widget_function' // Display function. | |
// ); | |
add_meta_box('id', 'Dashboard Widget Title', 'wpexplorer_dashboard_widget_function', 'dashboard', 'side', 'high'); | |
} | |
/**************************************************************************************** | |
* Create the function to output the contents of your Dashboard Widget. | |
*/ | |
function wpexplorer_dashboard_widget_function() { | |
echo "<h1>List Category</h1>"; | |
echo '</br>'; | |
//get all category | |
$categories = get_categories( array( | |
'orderby' => 'name', | |
'order' => 'ASC' | |
) ); | |
// echo '<pre>' , var_dump($categories) , '</pre>'; | |
foreach( $categories as $category ) { | |
echo ' ---------------------------------- <br>'; | |
echo $category->name ; | |
echo '<p>' . sprintf( esc_html__( 'Post Count: %s' ), $category->count ) . '</p>'; | |
echo '<p>' . sprintf( esc_html__( 'Slug: %s' ), $category->slug ) . '</p>'; | |
$posts = get_posts( array( | |
'category_name' => $category->slug | |
) ); | |
echo '<ul style="background-color: red; padding: 0 0 0 20px">'; | |
foreach($posts as $post){ | |
echo '<li>' . $post->post_title .'</br>'. $post->post_content. '</li>'; | |
echo '==============================================='; | |
} | |
echo '</ul>'; | |
} | |
} | |
add_action( 'wp_dashboard_setup', 'wpexplorer_add_dashboard_widgets' ); | |
/**************************************************************************************** | |
* hooks buat saat user click update btn | |
*/ | |
add_action( 'edit_category', 'edit_category_update_btn_click' ); | |
function edit_category_update_btn_click(){ | |
$url = 'https://postb.in/1571729554638-9586148213129?wimpy'; | |
// wp_redirect( $redirect_url ); | |
$request = wp_remote_get( $url ); | |
print_r($request); | |
exit; | |
} | |
/**************************************************************************************** | |
* CREATE CUSTOM POST TYPE | |
*/ | |
// Our custom post type function | |
function create_posttype() { | |
register_post_type( 'movies', | |
// CPT Options | |
array( | |
'labels' => array( | |
'name' => __( 'Movies' ), | |
'singular_name' => __( 'Movie' ) | |
), | |
'public' => true, | |
'has_archive' => true, | |
'rewrite' => array('slug' => 'movies'), | |
) | |
); | |
} | |
// Hooking up our function to theme setup | |
add_action( 'init', 'create_posttype' ); | |
/** | |
* Creating a function to create our CPT | |
*/ | |
function custom_post_type() { | |
// Set UI labels for Custom Post Type | |
$labels = array( | |
'name' => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ), | |
'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ), | |
'menu_name' => __( 'Movies', 'twentythirteen' ), | |
'parent_item_colon' => __( 'Parent Movie', 'twentythirteen' ), | |
'all_items' => __( 'All Movies', 'twentythirteen' ), | |
'view_item' => __( 'View Movie', 'twentythirteen' ), | |
'add_new_item' => __( 'Add New Movie', 'twentythirteen' ), | |
'add_new' => __( 'Add New', 'twentythirteen' ), | |
'edit_item' => __( 'Edit Movie', 'twentythirteen' ), | |
'update_item' => __( 'Update Movie', 'twentythirteen' ), | |
'search_items' => __( 'Search Movie', 'twentythirteen' ), | |
'not_found' => __( 'Not Found', 'twentythirteen' ), | |
'not_found_in_trash' => __( 'Not found in Trash', 'twentythirteen' ), | |
); | |
// Set other options for Custom Post Type | |
$args = array( | |
'label' => __( 'movies', 'twentythirteen' ), | |
'description' => __( 'Movie news and reviews', 'twentythirteen' ), | |
'labels' => $labels, | |
// Features this CPT supports in Post Editor | |
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ), | |
// You can associate this CPT with a taxonomy or custom taxonomy. | |
'taxonomies' => array( 'genres' ), | |
/* A hierarchical CPT is like Pages and can have | |
* Parent and child items. A non-hierarchical CPT | |
* is like Posts. | |
*/ | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_nav_menus' => true, | |
'show_in_admin_bar' => true, | |
'menu_position' => 5, | |
'can_export' => true, | |
'has_archive' => true, | |
'exclude_from_search' => false, | |
'publicly_queryable' => true, | |
'capability_type' => 'page', | |
); | |
// Registering your Custom Post Type | |
register_post_type( 'movies', $args ); | |
} | |
/** | |
* Hook into the 'init' action so that the function | |
* Containing our post type registration is not | |
* unnecessarily executed. | |
*/ | |
add_action( 'init', 'custom_post_type', 0 ); | |
/** | |
* create custom taxonomy for movies | |
*/ | |
// Let us create Taxonomy for Custom Post Type | |
add_action( 'init', 'create_movies_custom_taxonomy', 0 ); | |
//create a custom taxonomy name it "type" for your posts | |
function create_movies_custom_taxonomy() { | |
$labels = array( | |
'name' => _x( 'Types', 'taxonomy general name' ), | |
'singular_name' => _x( 'Type', 'taxonomy singular name' ), | |
'search_items' => __( 'Search Types' ), | |
'all_items' => __( 'All Types' ), | |
'parent_item' => __( 'Parent Type' ), | |
'parent_item_colon' => __( 'Parent Type:' ), | |
'edit_item' => __( 'Edit Type' ), | |
'update_item' => __( 'Update Type' ), | |
'add_new_item' => __( 'Add New Type' ), | |
'new_item_name' => __( 'New Type Name' ), | |
'menu_name' => __( 'Types' ), | |
); | |
register_taxonomy('types',array('movies'), array( | |
'hierarchical' => true, | |
'labels' => $labels, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'type' ), | |
)); | |
} | |
/**************************************************************************************** | |
* hooke edit_<taxonomy> | |
*/ | |
add_action( 'edit_<taxonomy>', 'edit_types_update_btn_click' ); | |
function edit_types_update_btn_click($id){ | |
// $url = 'https://postb.in/1571823946414-3031071899458?types=wimpy'; | |
// wp_redirect( $redirect_url ); | |
// $request = wp_remote_get( $url ); | |
echo $id; | |
// $args = array( | |
// 'post_type' => 'movies', | |
// 'post_status' => 'publish', | |
// ); | |
// $posts = new WP_Query( $args ); | |
// | |
// echo '<pre>'; | |
// var_dump($posts); | |
// echo '</pre>'; | |
// We want to find the Taxonomy to this slug. | |
$term_slug = 'movies'; | |
$taxonomies = get_taxonomies(); | |
echo '<pre>'; | |
var_dump($taxonomies); | |
echo '</pre>'; | |
exit; | |
} | |
?> |
See all order woocommerce query
select
p.ID as order_id,
p.post_date,
p.post_status,
max( CASE WHEN pm.meta_key = '_billing_email' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_email,
max( CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_first_name,
max( CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_last_name,
max( CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_1,
max( CASE WHEN pm.meta_key = '_billing_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_2,
max( CASE WHEN pm.meta_key = '_billing_city' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_city,
max( CASE WHEN pm.meta_key = '_billing_state' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_state,
max( CASE WHEN pm.meta_key = '_billing_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_postcode,
max( CASE WHEN pm.meta_key = '_shipping_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_first_name,
max( CASE WHEN pm.meta_key = '_shipping_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_last_name,
max( CASE WHEN pm.meta_key = '_shipping_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_address_1,
max( CASE WHEN pm.meta_key = '_shipping_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_address_2,
max( CASE WHEN pm.meta_key = '_shipping_city' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_city,
max( CASE WHEN pm.meta_key = '_shipping_state' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_state,
max( CASE WHEN pm.meta_key = '_shipping_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_postcode,
max( CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END ) as order_total,
max( CASE WHEN pm.meta_key = '_order_tax' and p.ID = pm.post_id THEN pm.meta_value END ) as order_tax,
max( CASE WHEN pm.meta_key = '_paid_date' and p.ID = pm.post_id THEN pm.meta_value END ) as paid_date,
( select group_concat( order_item_name separator ' |#| ' ) from wp_woocommerce_order_items where order_id = p.ID ) as order_items
from
wp_posts p
join wp_postmeta pm on p.ID = pm.post_id
join wp_woocommerce_order_items oi on p.ID = oi.order_id
where
post_type = 'shop_order' AND
post_status = 'wc-completed'
group by
p.ID
ORDER BY `order_id` DESC
see all order with affiliate
select
p.ID as order_id,
p.post_date,
p.post_status,
rf.affiliate_id,
us.display_name,
max( CASE WHEN pm.meta_key = '_billing_email' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_email,
max( CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_first_name,
max( CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_last_name,
max( CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_1,
max( CASE WHEN pm.meta_key = '_billing_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_2,
max( CASE WHEN pm.meta_key = '_billing_city' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_city,
max( CASE WHEN pm.meta_key = '_billing_state' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_state,
max( CASE WHEN pm.meta_key = '_billing_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_postcode,
max( CASE WHEN pm.meta_key = '_shipping_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_first_name,
max( CASE WHEN pm.meta_key = '_shipping_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_last_name,
max( CASE WHEN pm.meta_key = '_shipping_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_address_1,
max( CASE WHEN pm.meta_key = '_shipping_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_address_2,
max( CASE WHEN pm.meta_key = '_shipping_city' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_city,
max( CASE WHEN pm.meta_key = '_shipping_state' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_state,
max( CASE WHEN pm.meta_key = '_shipping_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as _shipping_postcode,
max( CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END ) as order_total,
max( CASE WHEN pm.meta_key = '_order_tax' and p.ID = pm.post_id THEN pm.meta_value END ) as order_tax,
max( CASE WHEN pm.meta_key = '_paid_date' and p.ID = pm.post_id THEN pm.meta_value END ) as paid_date,
( select group_concat( order_item_name separator ' |#| ' ) from wp_woocommerce_order_items where order_id = p.ID ) as order_items
from
wp_posts p
join wp_postmeta pm on p.ID = pm.post_id
join wp_woocommerce_order_items oi on p.ID = oi.order_id
join wp_afwc_referrals rf on p.id = rf.post_id
join wp_users us on rf.affiliate_id = us.ID
where
post_type = 'shop_order' AND
post_status = 'wc-completed'
group by
p.ID
ORDER BY `order_id` DESC