Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
jorpdesigns / gf-populate-dropdown.php
Created July 14, 2021 19:35
Snippet to populate Gravity Forms dropdown field with values from custom taxonomy
<?php
// Replace 8 with your form ID
add_filter( 'gform_pre_render_8', 'populate_region_field' );
add_filter( 'gform_pre_validation_8', 'populate_region_field' );
add_filter( 'gform_pre_submission_filter_8', 'populate_region_field' );
add_filter( 'gform_admin_pre_render_8', 'populate_region_field' );
function populate_region_field( $form ) {
foreach ( $form['fields'] as &$field ) {
@jorpdesigns
jorpdesigns / pagination-rewrite.php
Created July 14, 2021 14:15
Snippet to set pagination rewrite rule for custom post type
<?php
add_action('init', 'custom_post_type_pagination_rewrite');
function custom_post_type_pagination_rewrite() {
$archiveSlug = 'archive-slug'; // Replace with the archive page slug
$archiveID = 55; // Replace with the archive page ID
add_rewrite_rule( $archiveSlug . '/page/?([0-9]{1,})/?$', 'index.php?page_id=' . $archiveID . '&paged=$matches[1]', 'top');
}
?>
@jorpdesigns
jorpdesigns / custom-post-type-taxonomy.php
Last active July 14, 2021 14:09
Snippet for adding custom post type and custom taxonomy
<?php
// REGISTER CUSTOM POST TYPE
add_action( 'init', 'custom_post_type', 0 );
function custom_post_type() {
$labels = array(
'name' => 'Custom Post Types',
'singular_name' => 'Custom Post Type',
'menu_name' => 'Custom Post Types',
'name_admin_bar' => 'Custom Post Type',
@jorpdesigns
jorpdesigns / html-sitemap.php
Last active November 9, 2022 16:48
Shortcode for WordPress HTML sitemap
<?php
add_shortcode( 'html_sitemap', 'html_sitemap' );
function category_has_children( $term_id = 0, $taxonomy = 'category' ) {
$children = get_categories( array(
'child_of' => $term_id,
'taxonomy' => $taxonomy,
'hide_empty' => false,
'fields' => 'ids',
@jorpdesigns
jorpdesigns / post-author.php
Created July 14, 2021 12:11
Function for getting author info on WordPress post page
<?php
function author_info() {
$authorID = get_the_author_meta('ID');
$authorName = get_the_author_meta( 'display_name' );
$authorFirstName = get_the_author_meta( 'first_name' );
$authorLastName = get_the_author_meta( 'last_name' );
$authorDescription = wpautop( get_the_author_meta( 'description' ) );
$authorLinkedin = get_the_author_meta( 'linkedin' );
$authorTwitter = get_the_author_meta( 'twitter' );
@jorpdesigns
jorpdesigns / author-page.php
Last active July 14, 2021 12:11
Function for getting author info on WordPress author page
<?php
function author_info() {
$authorObject = get_user_by( 'slug', get_query_var( 'author_name' ) );
$authorID = $authorObject->ID;
$authorName = get_the_author_meta( 'display_name', $authorID );
$authorFirstName = get_the_author_meta( 'first_name', $authorID );
$authorLastName = get_the_author_meta( 'last_name', $authorID );
$authorDescription = wpautop( get_the_author_meta( 'description', $authorID ) );
$authorLinkedin = get_the_author_meta( 'linkedin', $authorID );
@jorpdesigns
jorpdesigns / table-of-contents.php
Created July 14, 2021 11:43
Shortcode for table of content
<?php
add_shortcode( 'toc', 'toc' );
function toc( $atts ) {
$atts = shortcode_atts(
array(
'levelone' => 'h2',
'leveltwo' => '',
'levelthree' => ''
@jorpdesigns
jorpdesigns / impreza-pageblock-css.php
Created July 14, 2021 11:36
Snippet to output Impreza theme page block along with internal element CSS
<?php
// Example usage: [custom_page_block id="55"]
add_shortcode( 'custom_page_block', 'custom_page_block' );
function custom_page_block( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
@jorpdesigns
jorpdesigns / add-user-nicename.php
Created July 14, 2021 10:37
Snippet to add nicename field to WordPress user profile
<?php
add_action( 'personal_options', 'user_edit_ob_start' );
add_action( 'show_user_profile', 'insert_nicename_input' );
add_action( 'edit_user_profile', 'insert_nicename_input' );
add_action( 'user_profile_update_errors', 'profile_update', 10, 3 );
function user_edit_ob_start() {
ob_start();
}
@jorpdesigns
jorpdesigns / add-other-author-cpt.php
Created July 14, 2021 10:34
Snippet to add other custom post types to WordPress author archive
<?php
add_filter( 'pre_get_posts', 'add_post_types_to_archives' );
function add_post_types_to_archives( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_author() && empty( $query->query_vars['suppress_filters'] ) ) {
$cpts = array( 'page', 'podcasts' ); // Replace with your own custom post types