Skip to content

Instantly share code, notes, and snippets.

@baptistebriel
Last active August 26, 2022 10:14
Show Gist options
  • Save baptistebriel/9325682 to your computer and use it in GitHub Desktop.
Save baptistebriel/9325682 to your computer and use it in GitHub Desktop.
[WordPress] - Collection of WordPress Snippets
<?php
// Show something only if the user is logged / admin.
// To be used in the current theme files - front.
if (current_user_can('manage_options')){ ?>
<div class="work">
<div class="work_layer">
<div class="device-content row">
<div class="work_content right">
<span class="f_13 uppercase sub_title">Web</span>
<h2 class="grey_dark medium">Perturbator.</h2>
<h4 class="grey_dark times">Wesite for Perturbator,<br>a music artist inspired by the 80's.</h4>
<a href="/perturbator/" class="link uppercase grey_dark">See project</a>
</div>
</div>
</div>
<img src="<?php echo get_template_directory_uri(); ?>/includes/images/uploads/perturbator.jpg" alt="">
</div>
<?php } ?>
<?php
// Detect an Ajax request using PHP.
// Ex: To be used in the current theme's single.php file to make ajax calls between articles...
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ){
// This is an Ajax request
}
else{
get_header();
}
echo '<article class="article-content">';
echo '</article>';
?>
<?php
// A custom background-image for your WordPress theme.
// To be used in the current theme's functions.php file.
$args = array(
'default-color' => 'F5F5F5',
'default-image' => get_template_directory_uri() . '/images/default-bg.jpg',
);
add_theme_support('custom-background',$args);
?>
<?php
// A custom widget for your WordPress Dashboard.
// In this example, we have a table called wp_email in the database
// Works with any loops : posts, etc.
function custom_dashboard_widget() {
global $wpdb; // http://codex.wordpress.org/Class_Reference/wpdb
$emails = $wpdb->get_results("SELECT mail FROM wp_email");
if($emails){
foreach ($emails as $mail) {
echo $email->mail.'<br>';
echo '<hr>';
}
}
die();
}
function add_custom_dashboard_widget() {
wp_add_dashboard_widget('custom_dashboard_widget', 'Espace Pro : Emails', 'custom_dashboard_widget');
}
add_action('wp_dashboard_setup', 'add_custom_dashboard_widget');
?>
<?php
// You can add new taxonomies (such as categories) in WordPress.
// For example, create a taxonomy named 'Clients'.
// You'll be able to filter your posts by clients !
// To be used in the current theme's functions.php file.
// Option : add a thumbnail image to every items listed in 'Clients'. — http://wordpress.org/plugins/taxonomy-images/
add_action('init', 'clients_init',0); // Here I call a function, defined right after this.
// My function clients_init();
function clients_init() {
$labels = array(
'name' => _x( 'Clients', 'taxonomy general name'),
'singular_name' => _x('Client','taxonomy singular name'),
'search_items' => __('Search for clients'),
'popular_items' => __('Popular clients'),
'all_items' => __('All clients'),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __('Edit client'),
'update_item' => __('Update client'),
'add_new_item' => __('Add new client'),
'new_item_name' => __('New client name'),
'separate_items_with_commas' => __('Separate clients with commas'),
'add_or_remove_items' => __('Add or remove clients'),
'choose_from_most_used' => __('Choose from most used'),
'menu_name' => __('Clients'),
);
register_taxonomy('clients','post',array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array('slug' => 'clients'),
));
}
?>
<?php
// get_terms to display the taxonomy terms inside your theme
// You're not in functions.php anymore !
$taxonomy = 'clients';
$tax_terms = get_terms($taxonomy);
foreach ($tax_terms as $tax_term) {
echo '<a href="#" data-slug="' . $tax_term->slug . '">' . $tax_term->name.'</a>';
}
?>
<?php
// A tumbnail image for your WordPress theme.
// To be used in the current theme's functions.php file.
function my_theme_add_editor_styles() {
add_editor_style('custom-editor-style.css');
}
add_action('init', 'my_theme_add_editor_styles');
?>
<?php
// Filter posts by category name.
// With this, you're able to filter 'blog' posts and 'work' posts (for example)
// This is interesting if you need two (or more) different lists of posts.
$args=array(
'post_type' => 'post', // You want the posts, not pages.
'post_status' => 'publish', // c'mon...
'category_name' => 'blog', // change the category name here.
'posts_per_page' => -1, // loop all posts.
'orderby' => 'post_date' // do you really need me to explain that??
);
$list_of_posts=new WP_Query($args);
while($list_of_posts->have_posts()):$list_of_posts->the_post();
// echo whatever you want !
echo'<a href="'; the_permalink(); echo'" class="blog-post">';
the_title();
echo'</a>';
endwhile;
?>
<?php
$tags = wp_get_post_tags($post->ID);
$what_tag = array();
if($tags){
foreach ($tags as $tag) :
$what_tag[] = $tag->term_id;
endforeach;
}
?>
<?php if($tags){
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'post__not_in' => array($post->ID),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'terms' => array(1,3),
'field' => 'id',
'operator' => 'IN',
),
array(
'taxonomy' => 'post_tag',
'terms' => $what_tag,
'field' => 'id',
'operator' => 'IN',
),
),
);
$my_query = new WP_Query($args);
if($my_query->have_posts()){
while($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink(); ?>"></a>
<?php endwhile;
wp_reset_postdata();
} ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<?php require_once get_template_directory()."/theme_functions.php"; ?>
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, maximum-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta property="og:type" content="website">
<meta property="og:title" content="">
<meta property="og:url" content="">
<meta property="og:image" content="<?php echo get_template_directory_uri(); ?>/includes/images/og_image.png">
<meta property="og:image:width" content="450">
<meta property="og:image:height" content="450">
<meta property="og:description" content="">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="">
<meta name="twitter:title" content="">
<meta name="twitter:description" content="">
<meta name="twitter:creator" content="">
<meta name="twitter:image:src" content="">
<meta name="twitter:domain" content="">
<title><?php global $page, $paged; wp_title('|', true, 'right'); bloginfo('name'); $site_description = get_bloginfo('description','display'); if ($site_description && (is_home() || is_front_page())) echo " | $site_description";if ($paged >= 2 || $page >= 2)echo ' | ' . sprintf( __('Page %s', 'toolbox'), max($paged, $page)); ?></title>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link href="<?php echo get_template_directory_uri(); ?>/includes/images/apple-touch-startup-image-320x480.png" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 1)" rel="apple-touch-startup-image">
<link href="<?php echo get_template_directory_uri(); ?>/includes/images/apple-touch-startup-image-640x960.png" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image">
<link href="<?php echo get_template_directory_uri(); ?>/includes/images/apple-touch-startup-image-640x1096.png" media="(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image">
<link href="<?php echo get_template_directory_uri(); ?>/includes/images/apple-touch-startup-image-768x1004.png" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 1)" rel="apple-touch-startup-image">
<link href="<?php echo get_template_directory_uri(); ?>/includes/images/apple-touch-startup-image-1024x748.png" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 1)" rel="apple-touch-startup-image">
<link href="<?php echo get_template_directory_uri(); ?>/includes/images/apple-touch-startup-image-1536x2008.png" media="(device-width: 768px) and (device-height: 1024px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image">
<link href="<?php echo get_template_directory_uri(); ?>/includes/images/apple-touch-startup-image-2048x1496.png" media="(device-width: 768px) and (device-height: 1024px) and (orientation: landscape) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image">
<link href="<?php echo get_template_directory_uri(); ?>/includes/images/apple-touch-icon-57x57.png" sizes="57x57" rel="apple-touch-icon-precomposed">
<link href="<?php echo get_template_directory_uri(); ?>/includes/images/apple-touch-icon-72x72.png" sizes="72x72" rel="apple-touch-icon-precomposed">
<link href="<?php echo get_template_directory_uri(); ?>/includes/images/apple-touch-icon-114x114.png" sizes="114x114" rel="apple-touch-icon-precomposed">
<link href="<?php echo get_template_directory_uri(); ?>/includes/images/apple-touch-icon-144x144.png" sizes="144x144" rel="apple-touch-icon-precomposed">
<link rel="shortcut icon" href="<?php echo get_template_directory_uri(); ?>/includes/images/favicon.ico">
<?php if (is_singular() && get_option('thread_comments')) wp_enqueue_script('comment-reply'); ?>
<?php wp_head(); ?>
</head>
<body>
<?php
// Include Any File From Your Theme
// http://css-tricks.com/snippets/wordpress/include-any-file-from-your-theme/
include (TEMPLATEPATH . '/your-file.php');
?>
<?php
// A custom, editable menu for your WordPress theme.
// To be used in the current theme's functions.php file.
function register_menu() {
// Add the header-menu to Apparence > Menus
register_nav_menu('header-menu',__('Header'));
}
add_action('init','register_menu');
?>
<?php
// A custom post type.
// To be used in the current theme's functions.php file.
add_action('init','create_post_type');
function create_post_type() {
register_post_type('direct',
array(
'labels' => array(
'name' => __('Direct'),
'singular_name' => __('Direct')
),
'public' => true,
'has_archive' => true,
)
);
}
?>
<?php
// Template for WordPress search.
// First, make the query based on the search.
// Then, if there is results, do the loop.
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array(); // If you want more options... ('post_type' => 'post') [...]
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
}
$search = new WP_Query($search_query); // Make the query.
// Get the number of results.
global $wp_query;
$total_results = $wp_query->found_posts; // echo $total_results;
// The loop, if there is results.
if (have_posts()) : while (have_posts()) : the_post();
// Results! echo whatever you want !
echo '<a href="'.the_permalink().'">'.get_the_title().'</a>';
endwhile; else:
// No results!
echo '<p>Sorry, no posts matched your criteria.</p>';
endif;
?>
<?php
// A tumbnail image for your WordPress theme.
// To be used in the current theme's functions.php file.
if (function_exists('add_theme_support')){
// Add the theme support.
add_theme_support('post-thumbnails',array('post'));
// Set the default featured image dimensions (width, height, crop)
set_post_thumbnail_size(280, 160);
set_post_thumbnail_size(280, 160, true); // Crop mode, true.
// Registers a new image size.
// WordPress will create a copy of the thumbnail with the specified dimensions when you upload a new image.
add_image_size('slide-img', 900, 335);
}
// Same image, different sizes. This is interesting if you have a grid presentation & a slider, for example.
// To use it, simply paste this into your loop:
// echo the_post_thumbnail('slide-img');
?>
@md-shamir
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment