Archives Code Snippets

Custom shortcode – random.php

<?php /** * Parses the passed attributes. */ $attributes = (array) cptui_shortcode_atts( $attributes ); $custom_query = new WP_Query( cptui_filter_query( $attributes ) ); ?> <div class="cptui-shortcode-random cptui-random-<?php echo esc_attr( $attributes[‘cptui_shortcode_id’] ); ?>"> <?php if ( $attributes[‘title’] ) { ?> <h2><?php echo…

Custom shortcode – filter_query

public function filter_query( $args = array(), $attributes = array() ) { if ( ‘random’ !== $attributes[‘cptui_shortcode’] ) { return $args; } $args = array( ‘post_type’ => $attributes[‘post_type’], ‘post_status’ => ‘publish’, ‘posts_per_page’ => ‘1’, ‘orderby’ => ‘rand’, ); return $args; }

Custom Shortcode – random method

public function random() { $options = array(); $types = cptuiext_get_post_type_slugs(); foreach ( $types as $type ) { $options[ $type ] = $type; } $shortcode = array( ‘id’ => ‘random’, ‘name’ => __( ‘Random Post’, ‘cptui-extended-random’ ), ‘template’ => ‘random’, ‘template_location’…

Basic Uninstall.php file.

<?php // die when the file is called directly if (!defined(‘WP_UNINSTALL_PLUGIN’)) { die; } //define a vairbale and store an option name as the value. $option_name = ‘plugininze_example_option’; //call delete option and use the vairable inside the quotations delete_option($option_name); //…

Tax Archives – Backfill

/** * Run queries for posts and assign our term to them. * * Conditionally able to run on just one-to-many post types, or all. * * @since 1.0.0 * * @param mixed $chosen Individual post type to potentially use.…

Tax Archives – Pre Get Posts

/** * Add our post types to the query. * * @since 1.0.0 * * @param object $query WP_Query instance. */ function pluginize_amend_term_archive( $query ) { if ( is_admin() || ! $query->is_main_query() ) { return; } if ( ! $query->is_category()…

Tax Archives – Listener

/** * Add publish hooks to all our CPTUI post types. * * @since 1.0.0 */ function pluginize_post_type_listener() { $post_types = cptui_get_post_type_slugs(); // We default to just our CPTUI post types. foreach ( $post_types as $type ) { add_action( "publish_{$type}",…

Tax Archives – Set Term

/** * Automatically add our taxonomy term upon publish. * * Term needs to already be created in the database. * * @since 1.0.0 * * @param int $post_id Published post ID. */ function pluginize_auto_add_taxonomy_terms_on_publish( $post_id = 0 ) {…

edd add images

function cptuiext_edd_axtra_images( $attributes ) { $images = array(); $download_images = get_post_meta( get_the_ID(), ‘edd_download_images’, true ); $images = $download_images; if ( $download_images ) { foreach ( $download_images as $download_image ) { $html = ‘<img class="edd-di-image" src="’ . $download_image[‘image’] . ‘" />’;…

woo rating hook

function cptui_add_woo_rating_below_image( $attributes ) { global $product; if ( isset( $product ) ) { if ( get_option( ‘woocommerce_enable_review_rating’ ) === ‘no’ ) { return; } $rating_count = $product->get_rating_count(); $review_count = $product->get_review_count(); $average = $product->get_average_rating(); if ( $rating_count > 0 )…

cmb2 shortcode field

// This is how you add a CMB2 field per the CMB2 docs $cmb->add_field( array( ‘name’ => ‘Test File’, ‘desc’ => ‘Upload an image or enter an URL.’, ‘id’ => ‘my_custom_test_image’, ‘type’ => ‘file’, // Optionally hide the text input…

shortcode enqueue

function my_custom_shortcode_enqueue() { wp_register_style( ‘my_shortcode_css’, ‘plugin-dir’ ); wp_register_script( ‘my_shortcode_js’, ‘plugin-dir’ ); } add_action( ‘wp_enqueue_scripts’, ‘my_custom_shortcode_enqueue’ );

shortcode callback

function my_custom_shortcode() { $shortcode = array( ‘id’ => ‘my_custom_shortcode’, // make this unique ‘name’ => ‘My Custom’, // name in modal drop down ‘template’ => ‘posts’, // name of the shortcode template ‘template_location’ => ‘plugin-dir/my-templates’, // path to template folder…

cptui_register_shortcode()

function my_custom_cptui_shortcode() { cptui_register_shortcode( ‘my_custom_shortcode’ ); cptui_register_shortcode( ‘my_custom_page_shortcode’ ); cptui_register_shortcode( ‘my_custom_single_post_shortcode’ ); } add_action( ‘cptuiext_loaded’, ‘my_custom_cptui_shortcode’ );