Skip to content

Instantly share code, notes, and snippets.

@josanua
Last active January 25, 2024 23:54
Show Gist options
  • Select an option

  • Save josanua/2c2bd26bc6765c03fd79ce14fa8eff8c to your computer and use it in GitHub Desktop.

Select an option

Save josanua/2c2bd26bc6765c03fd79ce14fa8eff8c to your computer and use it in GitHub Desktop.
wp admin helper
<?php
https://developer.wordpress.org/reference/functions/add_menu_page/
add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', string $icon_url = '', int $position = null );
/**
* Reset posts counts only for current users
*/
https://wordpress.stackexchange.com/questions/49154/hide-other-users-posts-in-admin-panel
function fix_count_orders( $counts, $type, $perm ) {
global $wpdb;
if ( ! post_type_exists( $type ) ) {
return new stdClass();
}
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
$post_type_object = get_post_type_object( $type );
// adds condition to respect `$perm`. (3)
if ( $perm === 'readable' && is_user_logged_in() ) {
if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
$query .= $wpdb->prepare(
" AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
get_current_user_id()
);
}
}
// limits only author's own posts. (6)
if ( is_admin() && ! current_user_can ( $post_type_object->cap->edit_others_posts ) ) {
$query .= $wpdb->prepare( ' AND post_author = %d', get_current_user_id() );
}
$query .= ' GROUP BY post_status';
$results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
$counts = array_fill_keys( get_post_stati(), 0 );
foreach ( $results as $row ) {
$counts[ $row['post_status'] ] = $row['num_posts'];
}
$counts = (object) $counts;
$cache_key = _count_posts_cache_key( $type, 'readable' );
// caches the result. (2)
// although this is not so efficient because the cache is almost always deleted.
wp_cache_set( $cache_key, $counts, 'counts' );
return $counts;
}
function query_set_only_author( $wp_query ) {
if ( ! is_admin() ) {
return;
}
$allowed_types = [ 'laundries' ];
$current_type = get_query_var( 'post_type', 'laundries' );
if ( in_array( $current_type, $allowed_types, true ) ) {
$post_type_object = get_post_type_object( $type );
if (! current_user_can( $post_type_object->cap->edit_others_posts ) ) { // (6)
$wp_query->set( 'author', get_current_user_id() );
add_filter( 'wp_count_posts', 'fix_count_orders', PHP_INT_MAX, 3 ); // (4)
}
}
}
add_action( 'pre_get_posts', 'query_set_only_author', PHP_INT_MAX ); // (4)
// Admin dashboard custom post type filters, admin filters, post type filters, filter posts, filter tags, filter categories
https://generatewp.com/filtering-posts-by-taxonomies-in-the-dashboard/
function filter_cars_by_taxonomies($post_type, $which)
{
// Apply this only on a specific post type
if ('partners' !== $post_type)
return;
// A list of taxonomy slugs to filter by
$taxonomies = array( 'seller_locality', 'field_of_activity', 'commercial_centers');
foreach ($taxonomies as $taxonomy_slug) {
// Retrieve taxonomy data
$taxonomy_obj = get_taxonomy($taxonomy_slug);
$taxonomy_name = $taxonomy_obj->labels->name;
// Retrieve taxonomy terms
$terms = get_terms($taxonomy_slug);
// Display filter HTML
echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
echo '<option value="">' . sprintf(esc_html__('Show All %s', 'text_domain'), $taxonomy_name) . '</option>';
foreach ($terms as $term) {
printf(
'<option value="%1$s" %2$s>%3$s (%4$s)</option>',
$term->slug,
((isset($_GET[$taxonomy_slug]) && ($_GET[$taxonomy_slug] == $term->slug)) ? ' selected="selected"' : ''),
$term->name,
$term->count
);
}
echo '</select>';
}
}
add_action('restrict_manage_posts', 'filter_cars_by_taxonomies', 10, 2);
/**
* Disable Fullscreen Gutenberg.
*/
add_action( 'enqueue_block_editor_assets', 'wpdd_disable_editor_fullscreen_by_default' );
function wpdd_disable_editor_fullscreen_by_default() {
$script = "window.onload = function() { const isFullscreenMode = wp.data.select( 'core/edit-post' ).isFeatureActive( 'fullscreenMode' ); if ( isFullscreenMode ) { wp.data.dispatch( 'core/edit-post' ).toggleFeature( 'fullscreenMode' ); } }";
wp_add_inline_script( 'wp-blocks', $script );
}
/**
* Hide editor on specific pages.
* https://www.daddydesign.com/wordpress/how-to-hide-the-wordpress-content-editor-on-specific-pages/
*/
add_action( 'admin_init', 'hide_editor' );
function hide_editor() {
// Get the Post ID.
// de prelucrat cu if (isset($_GET['post'])) {}
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
if( !isset( $post_id ) ) return;
// Hide the editor on a page with a specific page template
// Get the name of the Page Template file.
$template_file = get_post_meta($post_id, '_wp_page_template', true);
if($template_file == 'templates/contacts.php' || $template_file == 'templates/cariere.php' ){ // the filename of the page template
remove_post_type_support('page', 'editor');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment