Skip to content

Instantly share code, notes, and snippets.

@clarklab
clarklab / set_page_template.php
Created November 5, 2012 18:06
set a page template programmatically in WordPress
update_post_meta( $post_id, '_wp_page_template', 'page-contact.php' );
@clarklab
clarklab / block-helpers.php
Last active August 3, 2021 15:58
Wordpress check if block is used first
<?php
/**
* Block helpers
* add special classes when certain blocks appear, put this in your functions.php file or include it somewhere
*/
// add block classes in body and post class
function blocks_body_class( $classes ) {
global $post;
@clarklab
clarklab / enqueue_script_conditional.php
Created November 5, 2012 21:54
WordPress conditional JS enqueue
<?php
if (is_page('Contact')) {
wp_register_script( 'contact-form', 'http://ajax.googleapis.com/contact-form.js');
wp_enqueue_script( 'contact-form' );
}
?>
@clarklab
clarklab / get-shorty.php
Created March 30, 2017 15:07
Get first X words of WordPress post
// toss this in your functions.php (or elsewhere)
function get_shorty($count = 15){
global $post;
$str = get_the_content();
$str = implode(' ', array_slice(explode(' ', $str), 0, $count));
return $str.'...';
}
@clarklab
clarklab / blocks.php
Created January 27, 2020 18:13
WordPress Gutenberg block use only once
<?php
// ... the rest of your file here
acf_register_block_type(array(
'name' => 'hero',
'title' => __('Hero'),
'description' => __('A custom hero block.'),
'render_template' => 'template-parts/blocks/hero/hero.php',
'category' => 'formatting',
@clarklab
clarklab / wp_insert_post_front_end_form.php
Created May 25, 2012 00:12
This is a sample front-end form using wp_insert_post(). It's quickly stripped out of my production code from Android and Me, and hasn't been tested alone, so please take it with a grain of salt.
<?
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['insert_post'] )) { //check that our form was submitted
$title = $_POST['thread_title']; //set our title
if ($_POST['thread_description']=="") { // check if a description was entered
$description = "See thread title..."; // if not, use placeholder
} else {
$description = $_POST['thread_description']; //if so, use it
@clarklab
clarklab / sftp-config.json
Created November 7, 2012 02:04
Sample Sublime SFTP config
{
// The tab key will cycle through the settings when first created
// Visit http://wbond.net/sublime_packages/sftp/settings for help
// sftp, ftp or ftps
"type": "sftp",
"save_before_upload": true,
"upload_on_save": false,
"sync_down_on_open": false,
@clarklab
clarklab / block-whitelist.php
Created January 28, 2020 19:05
WordPress whitelist Gutenberg blocks
<?php
/**
* Block whitelist
* Only allow certain blocks to be used
*/
add_filter( 'allowed_block_types', 'our_allowed_block_types' );
function our_allowed_block_types( $allowed_blocks ) {
@clarklab
clarklab / transient-example.php
Created January 30, 2013 20:00
Caching a simple API call using the WP Transient API
<?php
//let's get some tweets!!
//first, let's see if I've got the data already cached
$tweets = get_transient("tweets");
//dang it! it looks like I might not. let's grab some tweets from the Twitter API
if( !$tweets ) {
@clarklab
clarklab / shortcodes-in-ACF.php
Last active April 9, 2019 13:22
Shortcodes with Advanced Custom Fields
add_filter('acf/format_value/type=textarea', 'do_shortcode');