Skip to content

Instantly share code, notes, and snippets.

View smileBeda's full-sized avatar
💭
I may be slow to respond.

Beda Schmid smileBeda

💭
I may be slow to respond.
View GitHub Profile
add_filter( 'register_post_type_args', 'rename_registered_post_types', 999, 2 );
function rename_registered_post_types( $args, $post_type ){
if( 'post' == $post_type ){//your post type to rename
$args['rewrite']['slug'] = 'your_custom_slug';
}
return $args;
}
add_filter( 'post_link', 'add_custom_base_to_post_links', 10, 3 );
function add_custom_base_to_post_links( $post_link, $post, $leavename ) {
if ( 'post' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_name . '/', '/your_custom_base/' . $post->post_name . '/', $post_link );
return $post_link;
}
@smileBeda
smileBeda / gist:16fda9d82498f8406fd8e5888d78ff41
Created December 9, 2020 12:34
Remove WordPress native Excerpt Trim
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'allow_html_in_excerpt');
function allow_html_in_excerpt($text) { // Fakes an excerpt if needed
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace('\]\]\>', ']]>', $text);
/*just add all the tags you want to appear in the excerpt --
be sure there are no white spaces in the string of allowed tags */
add_filter( 'init', 'custom_registered_taxonomy_admin_labels' );
function custom_registered_taxonomy_admin_labels() {
global $wp_taxonomies;
$labels = &$wp_taxonomies['your_taxonomy']->labels;//change to your taxonomy slug
$labels->name = 'NAME';
$labels->singular_name = 'SING_NAME';
$labels->add_new = 'Add NAME';
$labels->add_new_item = 'Add NAME';
$labels->edit_item = 'Edit NAME';
add_action( 'wp_enqueue_scripts', 'conditionally_enqueue_styles');
function conditionally_enqueue_styles() {
if ( is_front_page() || !is_archive() ) {
wp_enqueue_style('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css');
wp_enqueue_style('style', get_stylesheet_uri());
wp_enqueue_script( 'bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js', array('jquery'), '3.3.4', true );
}
}
add_action( 'pre_get_posts', 'remove_post_type_from_front_end_search_results');
function remove_post_type_from_front_end_search_results($query){
if(is_admin() || !$query->is_main_query()) return;
if($query->is_search()){
$post_type_to_remove = 'your_post_type';
$searchable_post_types = get_post_types(array('exclude_from_search' => false));
add_action( 'template_redirect', 'change_wp_search_url' );
function change_wp_search_url() {
if ( !empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/your_page/?your_param=" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action('init', 'make_registered_flat_taxonomies_hierarchical');
function make_registered_flat_taxonomies_hierarchical() {
// Maintain the built-in rewrite functionality of WordPress tags
global $wp_rewrite;
$rewrite = array(
'hierarchical' => false, // Maintains tag permalink structure
add_action('init','safe_remove_backend_admin_access_for_role');
function safe_remove_backend_admin_access_for_role(){
if( current_user_can('editor') || current_user_can('administrator') )//Your roles
return;
if( is_admin() && !defined('DOING_AJAX') ){//IMPORTANT! Front end AJAX events are excluded...
if(isset( $_GET[ 'action'] ) && 'trash' == $_GET[ 'action'])//Perhaps you have a call to some WP Admin Operation that needs whitelisting...
return;
wp_redirect( home_url() );
exit;