Skip to content

Instantly share code, notes, and snippets.

@jozefini
Last active October 6, 2022 06:45
Show Gist options
  • Save jozefini/3cd8278d24900c870837f72adf999200 to your computer and use it in GitHub Desktop.
Save jozefini/3cd8278d24900c870837f72adf999200 to your computer and use it in GitHub Desktop.
WordPress CPT, Term & Options page helpers
<?php
/**
* Functions and definitions
*
* @package Theme
* @since 1.0.0
*/
use functions Theme\Helpers\{
get_current_options_slug,
get_current_cpt_slug,
get_current_taxonomy_slug,
};
require_once get_theme_file_path( 'includes/helpers.php' );
add_action( 'init', 'theme_require_custom_fields' );
/**
* Conditionally requires files for custom fields.
*/
function theme_require_custom_fields() {
$options_slug = get_current_options_slug();
$cpt_slug = get_current_cpt_slug();
$taxonomy_slug = get_current_taxonomy_slug();
if ( 'creator' === $taxonomy_slug ) {
require_once get_theme_file_path( 'includes/acf/taxonomy-creator.php' );
}
if ( is_customize_preview() ) {
require_once get_theme_file_path( 'includes/customize.php' );;
}
if ( 'lecture' === $cpt_slug ) {
require_once get_theme_file_path( 'includes/acf/cpt-lecture.php' );
}
if ( 'rgbc_options' === $options_slug ) {
require_once get_theme_file_path( 'includes/acf/options.php' );
}
}
<?php
/**
* Utility functions that help the developmnet process
*
* @package Theme
*/
namespace Theme\Helpers;
/**
* Returns current options page slug if is in view
*
* @since 1.0.0
*
* @return string
*/
function get_current_options_slug(): string {
if ( ! is_admin() ) {
return '';
}
$http_get = wp_unslash( $_GET ); // phpcs:ignore
if ( ! isset( $http_get['page'] ) ) {
return '';
}
$current_page_slug = sanitize_text_field( $http_get['page'] );
if ( ! empty( $current_page_slug ) ) {
return $current_page_slug;
}
return '';
}
/**
* Returns current post type slug if is in view
*
* @since 1.0.0
*
* @return string
*/
function get_current_cpt_slug(): string {
global $pagenow;
$http_get = wp_unslash( $_GET ); // phpcs:ignore
if ( ! is_admin() || empty( $http_get ) ) {
return '';
}
if ( 'post.php' === $pagenow ) {
if ( ! isset( $http_get['post'] ) ) {
return '';
}
$current_post_id = (int) $http_get['post'];
$current_post_type = get_post_type( $current_post_id );
if ( ! empty( $current_post_type ) ) {
return $current_post_type;
}
} elseif ( 'post-new.php' === $pagenow ) {
if ( ! isset( $http_get['post_type'] ) ) {
return '';
}
$current_post_type = sanitize_text_field( $http_get['post_type'] );
if ( ! empty( $current_post_type ) ) {
return $current_post_type;
}
}
return '';
}
/**
* Returns current taxonomy slug if is in view
*
* @since 1.0.0
*
* @param string $taxonomy Optional. Taxonomy name.
*
* @return string
*/
function get_current_taxonomy_slug(): string {
if ( ! is_admin() ) {
return '';
}
$http_get = wp_unslash( $_GET ); // phpcs:ignore
$current_taxonomy = '';
if ( isset( $http_get['taxonomy'] ) ) {
$current_taxonomy = sanitize_text_field( $http_get['taxonomy'] );
} else {
$http_post = wp_unslash( $_POST ); // phpcs:ignore
if ( isset( $http_post['taxonomy'] ) ) {
$current_taxonomy = sanitize_text_field( $http_post['taxonomy'] );
}
}
if ( ! empty( $current_taxonomy ) ) {
return $current_taxonomy;
}
return '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment