Skip to content

Instantly share code, notes, and snippets.

@alwerner
alwerner / Using Wordpress Tags with Custom Post Types
Last active December 14, 2015 05:59
Using Wordpress Tags with Custom Post Types, goes in functions.php
// Using Tags with Custom Post Types, goes in functions.php
function add_custom_types( $query ) {
if( is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
// this gets all post types:
// $post_types = get_post_types();
// alternately, you can add just specific post types using this line instead of the above:
$post_types = array( 'post', 'news' );
@alwerner
alwerner / Display Wordpress Custom Post Types on home page
Last active December 14, 2015 05:59
Display Wordpress Custom Post Types on home page, add to functions.php
// Display Custom Post Types on home page, add to functions.php
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'news' ) ); //add cpt, in this case 'news' to array
return $query;
@alwerner
alwerner / Display Wordpress categories for Custom Post Types
Last active December 14, 2015 05:59
Display categories for Custom Post Types, add to functions.php
// Display categories for Custom Post Types, add to functions.php
add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
if(is_category() || is_tag()) {
$post_type = get_query_var('post_type');
if($post_type)
$post_type = $post_type;
else
$post_type = array('post','news'); //substitute cpt - in this case 'news' - for post type
@alwerner
alwerner / php server for pdf anchors
Last active December 16, 2015 11:48
Source: http://stackoverflow.com/questions/364946/how-to-make-pdf-file-downloadable-in-html-link "[...]run some sanity checks on the "file" variable to prevent people from stealing your files such as don't accept file extensions, add .pdf to the value."
<a href="pdf_server.php?file=pdffilename">Download my pdf</a>
// pdf_server.php
header("Content-Type: application/octet-stream");
$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
@alwerner
alwerner / Wordpress conditional script loading
Last active December 16, 2015 12:59
Wordpress conditional script loading in functions.php. Source: http://www.organizedthemes.com/loading-scripts-conditionally/
if( !is_admin()){
wp_register_script('thescript', get_template_directory_uri() . '/path/to/thescript.js', array('jquery'), '1.2.3', true );
}
function conditional_scripts() {
if( !is_admin() && is_page('somepage') ){
wp_enqueue_script('thescript');
}
}
@alwerner
alwerner / Conditional Wordpress pagination
Last active December 20, 2015 20:08
Conditional Wordpress pagination
// in functions.php, check to see if there's more than one page:
function show_posts_pag() {
global $wp_query;
return ($wp_query -> max_num_pages > 1);
}
// on page::
<?php if (show_posts_pag()) : ?>
function my_scripts_method() {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', get_template_directory_uri() . '/js/jquery-new.js');
wp_enqueue_script( 'jquery' );
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
// zip -e [archive] [file]
zip -e archivename.zip filetoprotect.txt
@alwerner
alwerner / Pushing a new branch for the first time
Created January 30, 2014 21:48
Pushing a new branch for the first time
// Makes sure the branch gets tracked upstream
git push -u origin my_branch
@alwerner
alwerner / update wordpress submodule
Created April 9, 2014 17:35
update wordpress submodule
cd wordpress
git fetch --tags
git checkout 3.5.1