Last active
February 5, 2019 16:38
-
-
Save n8kowald/7937f07ccc5e8a6b35fd404a1105786e to your computer and use it in GitHub Desktop.
Wordpress snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// --- Create a shortcode | |
// @Usage: [shortcode_name count="1"] | |
function theme_shortcode_add_name($args) { | |
$html = ''; | |
$count = !empty($args['count']) ? $args['count'] : 5; | |
return $html; | |
} | |
add_shortcode( 'shortcode_name', 'theme_shortcode_add_name' ); | |
// --- Add a menu | |
function theme_add_nav_menu() { | |
register_nav_menus( | |
array( | |
'main-menu' => __( 'Main Menu', 'themename' ) | |
) | |
); | |
} | |
add_action( 'after_setup_theme', 'theme_add_nav_menu' ); | |
// --- Add a widget | |
function theme_widgets_init() { | |
register_sidebar( array ( | |
'name' => __( 'Homepage Widget Area', 'theme' ), | |
'id' => 'homepage-widget-area', | |
'before_widget' => '', | |
'after_widget' => '', | |
'before_title' => '', | |
'after_title' => '', | |
) ); | |
} | |
add_action( 'widgets_init', 'theme_widgets_init' ); | |
// -- Remove WP Emoji | |
remove_action('wp_head', 'print_emoji_detection_script', 7); | |
remove_action('wp_print_styles', 'print_emoji_styles'); | |
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); | |
remove_action( 'admin_print_styles', 'print_emoji_styles' ); | |
// -- REST | |
// Disable unauthenticated REST requests | |
add_filter( 'rest_authentication_errors', function( $result ) { | |
if ( ! empty( $result ) ) { | |
return $result; | |
} | |
if ( ! is_user_logged_in() ) { | |
return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) ); | |
} | |
return $result; | |
}); | |
// --- Add a custom post type to REST | |
function theme_add_events_to_json_api() { | |
$types = get_post_types(array('public' => true, '_builtin' => false)); | |
global $wp_post_types; | |
foreach ($types as $type) { | |
$wp_post_types[$type]->show_in_rest = true; | |
$wp_post_types[$type]->rest_base = $type; | |
$wp_post_types[$type]->rest_controller_class = 'WP_REST_Posts_Controller'; | |
} | |
} | |
add_action( 'init', 'theme_add_events_to_json_api', 30 ); | |
// --- Gravity Forms | |
/** | |
* Adds an option to have an Australian address type with a state dropdown | |
*/ | |
function theme_gravityforms_add_australian_address( $address_types, $form_id ) { | |
$address_types['australia'] = array( | |
'label' => 'Australian', | |
'country' => 'Australia', | |
'zip_label' => 'Postcode', | |
'state_label' => 'State', | |
'states' => array( | |
'' => '-- Select a State --', | |
'ACT' => 'Australian Capital Territory', | |
'NSW' => 'New South Wales', | |
'NT' => 'Northern Territory', | |
'QLD' => 'Queensland', | |
'SA' => 'South Australia', | |
'TAS' => 'Tasmania', | |
'VIC' => 'Victoria', | |
'WA' => 'Western Australia', | |
), | |
); | |
return $address_types; | |
} | |
add_filter( 'gform_address_types', 'theme_gravityforms_add_australian_address', 10, 2 ); | |
function theme_set_default_country( $default_address_type, $form_id ) { | |
return 'australian'; | |
} | |
add_filter( 'gform_default_address_type', 'theme_set_default_country', 10, 2 ); | |
// --- Updated login link text when logged in | |
function theme_update_login_text( $sorted_menu_items, $args ) { | |
$logged_in_text = 'My Account'; | |
$logged_out_text = 'Member Login'; | |
foreach ( $sorted_menu_items as &$menu_item ) { | |
if ( $menu_item->title === $logged_out_text && is_user_logged_in() ) { | |
$menu_item->title = $logged_in_text; | |
} | |
} | |
return $sorted_menu_items; | |
} | |
add_filter( 'wp_nav_menu_objects', 'theme_update_login_text', 10, 2 ); | |
// --- Remove unwanted Wordpress admin menu items (items added by themes for example) | |
function theme_admin_remove_unwanted_admin_menu_items() { | |
global $menu; | |
$remove_menu_titles = array( | |
__('Courses', 'theme'), | |
__('Teachers', 'theme') | |
); | |
foreach ($menu as $menu_index => $menu_item) { | |
$title_parts = explode(' ', $menu_item[0]); | |
$title = !empty( $title_parts[0] ) ? $title_parts[0] : ''; | |
if ( in_array( $title, $remove_menu_titles ) ) { | |
unset ( $menu[$menu_index] ); | |
} | |
} | |
} | |
add_action( 'admin_menu', 'theme_admin_remove_unwanted_admin_menu_items' ); | |
// Add a new ACF toolbar | |
function theme_add_toolbar( $toolbars ) { | |
$toolbars['Very Simple' ] = array( | |
1 => array( | |
'bold' , 'italic' , 'underline' | |
) | |
); | |
return $toolbars; | |
} | |
add_filter( 'acf/fields/wysiwyg/toolbars', 'theme_add_toolbar' ); | |
// Show a warning when a menu is trashed | |
/** | |
* @return array | |
*/ | |
function theme_admin_get_menu_page_post_ids( $menu_name ) { | |
$menu_page_post_ids = array(); | |
if ( empty( $menu_name ) ) { | |
return $menu_page_post_ids; | |
} | |
$locations = get_nav_menu_locations(); | |
if ( !isset( $locations[$menu_name] ) ) { | |
return $menu_page_post_ids; | |
} | |
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] ); | |
$menu_items = wp_get_nav_menu_items( $menu->term_id ); | |
foreach ( $menu_items as $item ) { | |
$menu_page_post_ids[] = get_post_meta( $item->ID, '_menu_item_object_id', true ); | |
} | |
return $menu_page_post_ids; | |
} | |
/** | |
* @return array | |
*/ | |
function theme_admin_get_woocommerce_page_post_ids() { | |
global $wpdb; | |
$post_ids = array(); | |
$query = 'SELECT ID from wp_posts WHERE post_content LIKE "[woocommerce_%"'; | |
$records = $wpdb->get_results( $query ); | |
foreach ( $records as $record ) { | |
$post_ids[] = $record->ID; | |
} | |
return $post_ids; | |
} | |
/** | |
* Prevent certain pages from being trashed | |
* | |
* @param $post_ID | |
*/ | |
function theme_admin_restrict_post_deletion($post_ID){ | |
$menu_pages = theme_admin_get_menu_page_post_ids( 'main-menu' ); | |
// Disallow main menu page deletions | |
if ( in_array( $post_ID, $menu_pages ) ){ | |
wp_die( 'The page you\'re trying to delete is used in the "main-menu" menu. Deleting it will break the menu.<br><br>If you wish to delete it, please delete <a href="/wp-admin/nav-menus.php">the menu item</a> first.', 'Warning' ); | |
} | |
// Disallow WooCommerce page deletions | |
$woocommerce_page_post_ids = theme_admin_get_woocommerce_page_post_ids(); | |
if ( in_array( $post_ID, $woocommerce_page_post_ids ) ) { | |
wp_die( 'You cannot delete a WooCommerce template page.', 'Disallowed' ); | |
} | |
// Disallow required theme pages - update post IDs | |
$theme_post_ids = array(1, 2, 3); | |
if ( in_array( $post_ID, $theme_post_ids ) ) { | |
wp_die( 'You cannot delete this page. It is required by the theme.', 'Disallowed' ); | |
} | |
} | |
add_action('wp_trash_post', 'theme_admin_restrict_post_deletion', 10, 1); | |
add_action('before_delete_post', 'theme_admin_restrict_post_deletion', 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment