Skip to content

Instantly share code, notes, and snippets.

View ekka21's full-sized avatar

Ekkachai Danwanichakul ekka21

View GitHub Profile
@ekka21
ekka21 / gist:2975228
Created June 22, 2012 21:11
Wordpress: Page with child pages List
/* Page with child pages List */
function imediapixel_pagelist($page_name, $num, $orderby="menu_order",$style="2col") {
global $post;
$page_id = get_page_by_title($page_name);
$services_num = ($num) ? $num : 4;
$counter = 0;
$out = "";
@ekka21
ekka21 / gist:2975251
Created June 22, 2012 21:16
Wordpress: Post lists base on category
/* Posts List base on category*/
function imediapixel_postslist($category, $num, $orderby="date",$style="2col") {
global $post;
$category_id = get_cat_ID($category);
$cat_num = ($num) ? $num : 4;
$counter = 0;
$out = "";
query_posts('cat='.$category_id.'&showposts='.$cat_num.'&orderby='.$orderby);
@ekka21
ekka21 / gist:2984925
Created June 24, 2012 20:59
Wordpress: corn job
//You probably know that WordPress can schedule events.
// In this recipe, I’ll show you how you create an event that will be executed once hourly, or daily, etc.
if (!wp_next_scheduled('my_task_hook')) {
wp_schedule_event( time(), 'hourly', 'my_task_hook' );
}
add_action( 'my_task_hook', 'my_task_function' );
function my_task_function() {
wp_mail('[email protected]', 'Automatic email', 'Hello, this is an automatically scheduled email from WordPress.');
@ekka21
ekka21 / gist:2992432
Created June 26, 2012 01:07
Wordpress: Remove menu items from WordPress admin bar
function wps_admin_bar() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('wp-logo');
$wp_admin_bar->remove_menu('about');
$wp_admin_bar->remove_menu('wporg');
$wp_admin_bar->remove_menu('documentation');
$wp_admin_bar->remove_menu('support-forums');
$wp_admin_bar->remove_menu('feedback');
$wp_admin_bar->remove_menu('view-site');
}
@ekka21
ekka21 / gist:2992435
Created June 26, 2012 01:09
Wordpress: Disable the “please update now” message in WP dashboard
if ( !current_user_can( 'edit_users' ) ) {
add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
}
@ekka21
ekka21 / gist:2992439
Created June 26, 2012 01:10
Wordpress: Disable theme switching
add_action('admin_init', 'slt_lock_theme');
function slt_lock_theme() {
global $submenu, $userdata;
get_currentuserinfo();
if ($userdata->ID != 1) {
unset($submenu['themes.php'][5]);
unset($submenu['themes.php'][15]);
}
}
@ekka21
ekka21 / gist:2998115
Created June 26, 2012 19:09
Wordpress: Add a new class to body_class
add_filter('body_class','add_mobile_class');
function add_mobile_class($classes) {
$classes[] = 'NEW_CLASS';
return $classes;
}
@ekka21
ekka21 / Wordpress load $wpdb class
Created June 28, 2012 17:08
Wordpress: Load core wordpress classes outside of wordpress
<?php
define( 'BLOCK_LOAD', true );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' );
$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
@ekka21
ekka21 / gist:3018426
Created June 29, 2012 14:52
Wordpress: Using wordpress call Ajax the right way
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
type: 'GET',//POST, JSON, XML
dataType: 'html',
data: ({
action: 'MY_AJAX_FUNCTION',
state: state,
}),
success: function(data){
if (data){
@ekka21
ekka21 / gist:3028773
Created July 1, 2012 15:40
Wordpress: saving default post thumbnail
//Paste this in function.php
add_action( 'save_post', 'wptuts_save_thumbnail' );
function wptuts_save_thumbnail( $post_id ) {
// Get Thumbnail
$post_thumbnail = get_post_meta( $post_id, $key = '_thumbnail_id', $single = true );
// Verify that post is not a revision
if ( !wp_is_post_revision( $post_id ) ) {