Remove delete button on some categories

Remove delete button on some categoriesThere is another interesting snippet function add-on for your WordPress sites. Currently there is no way to disable or remove/delete button on WordPress Categories or other lists, so for example, your “MAD” or “DRUNK” Editor can suddenly remove categories.

That’s why I come to idea that this snippet should be interesting and thanks to S_HA_DUM, moderator of Stackexchange, there was already available snippet for this.

For those fields WordPress uses WP_List_Table class and some other extension, in this case WP_Terms_List_Table class and that class contains a filter called {$taxonomy}_row_actions.

$actions = apply_filters( “{$taxonomy}_row_actions”, $actions, $tag );

Remove “delete” button on some categories

[php]
/**
* Snippet Name: Remove delete button on some categories
* Snippet URL: http://wperror.com/remove-delete-button-on-some-categories.html
*/

add_filter(
‘bulk_category_remove’,
function($actions, $tag) {
$no_del = array(7,13);
if (in_array($tag->term_id,$no_del)) {
unset($actions[‘delete’]);
}
return $actions;
},
10,2
);
[/php]

Even with this tutorial I believe your WordPress categories are not 100% secured as there is bulk edit >> delete option still available and categories selected trough ID’s from earlier snippet are not protected.

This is why I’m thinking to write another snippet for bulk remove control!

Make WordPress subcategories to use parent category template

subcategories templateWell today I’m going to bring you new snippet. For example, let’s say you are using the posts for various purposes like questions, pools, articles and reviews. To sort it out you will need different categories. Of-course now you need to design different category layout. This is done using the slug preference, like: category-questions.php, category-pools.php, category-reviews.php in your template folder etc.

The whole idea is great and it will work, but things goes wrong when you add subcategories to any of parent (main) categories like on our example questions, pools, reviews… To understand it we need to know WordPress hierarchy

The category/archive template hierarchy in WordPress.

category-{slug}.php
category-{id}.php
category.php
archive.php
index.php

To understand to this “issue” even that is not, we have to look at default get_category_template() function witch is in WordPress since 1.5 update.

Default WordPress GET category template function

[php]
function get_category_template() {
$category = get_queried_object();

$templates = array();

if ( ! empty( $category->slug ) ) {
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
}
$templates[] = ‘category.php';

return get_query_template( ‘category’, $templates );
}
[/php]

If we watch closer to the get_category_template() function we can’t miss that it doesn’t look up hierarchy at all so the new function should do exactly that.

What this means? If there is a category named reviews, and the ID 13, then category archive will first look for the file category-reviews.php , then for the file category-13.php. If those php files are missing it will search for category.php. For example it also doesn’t exist, it will continue to archive.php and if there are lack of archive.php goes for the index.php to display the category content.

With this logic it’s obvious that the subcategory of category reviews, actually hosting (http://website.com/category/reviews/hosting) won’t load category-reviews.php.

So this snippet is telling WordPress to recognize if there are parent’s category template (category-reviews.php) available to use it as child’s template too.

Make subcategories to use parent category template

[php]
/****** Make subcategories to use parent category template ******/
/**
* Snippet Name: Make WordPress subcategories to use parent category template
* Snippet URL: http://wperror.com/make-subcategories-to-use-parent-category-template.html
*/
function wpe_subcategory_template() {
$category = get_queried_object();

$parent_id = $category->category_parent;

$templates = array();

if ( $parent_id == 0 ) {
// Use default values from get_category_template()
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
$templates[] = ‘category.php';
} else {
// Create replacement $templates array
$parent = get_category( $parent_id );

// Current first
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";

// Parent second
$templates[] = "category-{$parent->slug}.php";
$templates[] = "category-{$parent->term_id}.php";
$templates[] = ‘category.php';
}
return locate_template( $templates );
}

add_filter( ‘category_template’, ‘wpe_subcategory_template’ );
[/php]

Display only one category on home page

display only one category on home pageWell nowadays I found that there are many people looking for a plugin, snippet or hack to control displaying posts on the home page. This is snippet focused on displaying posts on the homepage from chosen category or categories.

So here is the snippet code or hack, now you just have to paste the code below in your functions.php file. Please be sure that you paste the code under the php tags.
 

 

 

PHP – Display only one category on home page

[php]
function my_category_queries( $query ) {
// only homepage and is the main query
if ($query->is_home() && $query->is_main_query()){
// display only posts in category with slug ‘articles’
$query->set(‘category_name’, ‘articles’);
// avoid sticky posts
$query->set(‘post__not_in’, get_option( ‘sticky_posts’ ) );
}
}<br />
add_action( ‘pre_get_posts’, ‘my_category_queries’ );

[/php]

Conclusion:
As you can see at the beginning of the function we are using the conditional functions is_home() witch is used for The Main Page and is_main_query() Is Main Query witch affect only to the main loop, and the function will proceed if both of conditions are true.
[php]$query->is_home() && $query->is_main_query()[/php]

The first $query-> ensures that all the posts are displayed with the category slug articles, this includes all Sticky Posts with any category, too.
[php]$query->set(‘category_name’, ‘articles’)[/php]

But then with the second $query-> set it to get all the posts with the category slug articles except for the posts that are sticky posts.
[php]$query->set(‘post__not_in’, get_option( ‘sticky_posts’ ) )[/php]

Additional notes:

Category Parameters
[php]
cat (int) – use category id.
category_name (string) – use category slug (NOT name).
category__and (array) – use category id.
category__in (array) – use category id.
category__not_in (array) – use category id.
[/php]

Let’s explain those parameters on our example now, well the first two commands works pretty same but the difference is that on first one we use Category ID’s and second one slugs. So basically if we wish to query the posts from two categories on the homepage we can simply update the code to ‘articles,affiliate-programs’ and function will call posts from the both categories on the homepage loop.

PHP – Display multiple categories on homepage

[php]
function my_category_queries( $query ) {
// only homepage and is the main query
if ($query->is_home() && $query->is_main_query()){
// display only posts in category with slug ‘articles’
// and slug ‘affiliate-programs’
$query->set(‘category_name’, ‘articles,affiliate-programs’);
// avoid sticky posts
$query->set(‘post__not_in’, get_option( ‘sticky_posts’ ) );
}
}

add_action( ‘pre_get_posts’, ‘my_category_queries’ );
[/php]

The example will be with the cat command instead of category_name will look like example bellow where the 25 and 36 are the category id’s.
[php]$query->set(‘cat’, ‘25,36’);[/php]