Last active
February 12, 2021 21:46
-
-
Save jsongerber/365cb8d3e2ac8542d72f418b2dc838ac to your computer and use it in GitHub Desktop.
Useful Wordpress functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Dequeue functions.js -> Contains handlers for navigation and widget area. | |
function buxum_dequeue_script() { | |
wp_dequeue_script( 'twentysixteen-script' ); | |
} | |
add_action( 'wp_print_scripts', 'buxum_dequeue_script', 100 ); | |
// Hide admin bar | |
function my_function_admin_bar(){ return false; } | |
add_filter( 'show_admin_bar' , 'my_function_admin_bar'); | |
// Proper way to enqueue scripts and styles. | |
function buxum_child_scripts() { | |
wp_enqueue_style( 'style-name', get_stylesheet_uri() ); | |
wp_enqueue_script( 'buxum-main', get_stylesheet_directory_uri() . '/js/main.js', array('jquery'), '1.0.0', true ); | |
// Use for ajax calls | |
wp_localize_script('buxum-main', 'ajaxurl', admin_url( 'admin-ajax.php' ) ); | |
} | |
add_action( 'wp_enqueue_scripts', 'buxum_child_scripts' ); | |
add_action( 'init', 'create_post_type' ); | |
function create_post_type() { | |
// Custom post type with single | |
register_post_type( 'product', // Name of post type | |
array( | |
'labels' => array( | |
'name' => __( 'Products' ), | |
'singular_name' => __( 'Product' ) | |
), | |
'public' => true, // False for no single page | |
'has_archive' => true, // Archive page | |
'rewrite' => array('slug' => 'products'), // Name of clean url | |
'supports' => array ('title', 'editor', 'thumbnail') // Available fields | |
) | |
); | |
// Custom post type without single | |
register_post_type( 'store', | |
array( | |
'labels' => array( | |
'name' => __( 'Magasins' ), | |
'all_items' => __('Tous les magasins'), | |
'singular_name' => __( 'Magasin' ) | |
), | |
'public' => true, | |
'exclude_from_search' => true, | |
'show_in_admin_bar' => true, | |
'show_in_nav_menus' => false, | |
'publicly_queryable' => false, | |
'query_var' => false, | |
'supports' => array('title', 'editor', 'thumbnail', 'excerpt'), | |
) | |
); | |
// Custom taxonomy | |
register_taxonomy( | |
'genre', // Name of taxonomy | |
'book', // Name of post, possibility to link to multiple post: array('post_type_1', post_type_2) | |
array( | |
'label' => __( 'Genre' ), | |
'public' => false, // Set to true if the taxonomy must be accessible by front user | |
'rewrite' => false, // Set to true if the taxonomy must be accessible by front user | |
'hierarchical' => true, | |
) | |
); | |
} | |
// Custom image sizes | |
add_action( 'after_setup_theme', 'custom_image_size' ); | |
function custom_image_size() { | |
add_image_size( 'category-thumb', 300 ); // 300 pixels wide (and unlimited height) | |
add_image_size( 'homepage-thumb', 220, 180, true ); // true = cropped | |
} | |
// Image size in admin | |
add_filter( 'image_size_names_choose', 'buxum_image_sizes_admin' ); | |
function buxum_image_sizes_admin( $sizes ) { | |
return array_merge( $sizes, array( | |
'content' => __( 'Image de contenu' ), | |
) ); | |
} | |
// Set max page content width (Change 1360), image won't be able to be bigger than this number | |
function buxum_content_width() { | |
$GLOBALS['content_width'] = apply_filters( 'buxum_content_width', 1360 ); | |
} | |
add_action( 'template_redirect', 'buxum_content_width', 0 ); | |
add_action( 'admin_init', 'buxum_content_width', 0 ); | |
// Function for ajax calls | |
add_action( 'wp_ajax_ajax_call', 'ajax_call' ); | |
add_action( 'wp_ajax_nopriv_ajax_call', 'ajax_call' ); | |
function ajax_call(){ | |
exit(); // Prevent from returning 0 | |
} | |
// Email obfuscation shortcode | |
// Use: [email mailto="[email protected]" text="Contact us|[email protected]"] | |
function email_func( $atts ) { | |
$atts = shortcode_atts( array( | |
'mailto' => '', | |
'text' => '' | |
), $atts, 'email' ); | |
$email_link = '<a href="'.antispambot('mailto:'.$atts['mailto']).'">'.antispambot($atts['text']).'</a>'; | |
return $email_link; | |
} | |
add_shortcode( 'email', 'email_func' ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment